我创建了一个文档,如下面的代码所示。这很好用。但是当我尝试打开它时,“Open Office”应用程序显示它损坏,而“File Viewer Lite”应用程序显示“文本过滤器错误:未知格式。”. (一个名为“Doc Viewer”的小应用可以打开它。)
知道出了什么问题吗?
谢谢。
public void Test_CreateDocument()
{
MemoryStream objMemoryStream = null;
WordprocessingDocument objWPDoc = null;
MainDocumentPart objMDPart = null;
Body objBody = null;
try
{
using (objMemoryStream = new MemoryStream())
{
using (objWPDoc = WordprocessingDocument.Create(objMemoryStream, WordprocessingDocumentType.MacroEnabledDocument, true))
{
objMDPart = objWPDoc.AddMainDocumentPart();
new Document(new Body()).Save(objMDPart);
objBody = objMDPart.Document.Body;
objBody.Append(new Paragraph(new Run(new DocumentFormat.OpenXml.Wordprocessing.Text("1Hello World!"))));
}
objWPDoc = null;
using (var fileStream = new FileStream(this.txbWordDocument.Text, FileMode.Create, FileAccess.Write))
{
objMemoryStream.Position = 0;
objMemoryStream.CopyTo(fileStream);
}
}
objMemoryStream = null;
}
catch (System.Exception Ex)
{
MessageBox.Show(Ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}