using (FileStream stream = File.OpenRead(filePath))
{
XWPFDocument doc = new XWPFDocument(stream);
}
判断后缀, .doc用HWPFDocument,.docx用XWPFDocument。如果你的单个文档不超过500个段落,25个表格,也可以用free spire.doc,无需区分后缀,都用Document类,从流读取用Document.LoadFromStream方法
word的版本不同,后缀不一样,doc,docx是不一样的,程序限制下word的版本即可
要使用2.6.0的NPOI
否则会出现以下两种异常
😒模板为docx文件时
FileStream fs = File.OpenRead(_filePath + report.FilePath)
new XWPFDocument(fs)
System.NullReferenceException:“Object reference not set to an instance of an object.”
😒模板为doc文件时
错误内容:Wrong Local header signature: 0xE011CFD0
以上两种异常升级NPOI到2.6.0即可解决