OpenXML:读取一个 word 文件,添加一些文本并打开它而不将其保存在 ASP.Net Web 应用程序中
OpenXML: Read a word file , Add some text and open it without saving it in ASP.Net Web application
我正在尝试从磁盘读取模板文件并向其中添加一段。完成后,我想将其作为新文件打开。模板不应将更改保存到其中。我尝试了下面的代码,结果是,模板正在随着更改进行修改,但在浏览器中打开的文件没有这些更改。显然我没有得到修改后的流作为响应。我该怎么做,同时避免更改模板文件。
public class DocumentCreator
{
public static void CreateDoc()
{
string strDoc = @"C:\Ash\foo.docx";
string txt = "Bruno Rovani";
using (Stream stream = File.Open(strDoc, FileMode.Open))
{
OpenAndAddToWordprocessingStream(stream, txt);
}
}
public static void OpenAndAddToWordprocessingStream(Stream stream, string txt)
{
// Open a WordProcessingDocument based on a stream.
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream, true))
{
// Assign a reference to the existing document body.
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
// Add new text.
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(txt));
// HTTP response
HttpResponse Response = HttpContext.Current.Response;
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");
//stream = wordprocessingDocument.MainDocumentPart.GetStream();
stream.Position = 0;
stream.CopyTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
已解决
已添加wordprocessingDocument.MainDocumentPart.Document.Save();
PS: 模板还在修改中。不要认为有办法避免这种情况,但由于我将在实际场景中进行查找和替换,所以这对我来说不是什么大问题。
所以函数看起来像
public static void OpenAndAddToWordprocessingStream(Stream stream, string txt)
{
// Open a WordProcessingDocument based on a stream.
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream, true))
{
// Assign a reference to the existing document body.
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
// Add new text.
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(txt));
**wordprocessingDocument.MainDocumentPart.Document.Save();**
// HTTP response
HttpResponse Response = HttpContext.Current.Response;
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
我正在尝试从磁盘读取模板文件并向其中添加一段。完成后,我想将其作为新文件打开。模板不应将更改保存到其中。我尝试了下面的代码,结果是,模板正在随着更改进行修改,但在浏览器中打开的文件没有这些更改。显然我没有得到修改后的流作为响应。我该怎么做,同时避免更改模板文件。
public class DocumentCreator
{
public static void CreateDoc()
{
string strDoc = @"C:\Ash\foo.docx";
string txt = "Bruno Rovani";
using (Stream stream = File.Open(strDoc, FileMode.Open))
{
OpenAndAddToWordprocessingStream(stream, txt);
}
}
public static void OpenAndAddToWordprocessingStream(Stream stream, string txt)
{
// Open a WordProcessingDocument based on a stream.
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream, true))
{
// Assign a reference to the existing document body.
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
// Add new text.
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(txt));
// HTTP response
HttpResponse Response = HttpContext.Current.Response;
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");
//stream = wordprocessingDocument.MainDocumentPart.GetStream();
stream.Position = 0;
stream.CopyTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
已解决
已添加wordprocessingDocument.MainDocumentPart.Document.Save();
PS: 模板还在修改中。不要认为有办法避免这种情况,但由于我将在实际场景中进行查找和替换,所以这对我来说不是什么大问题。
所以函数看起来像
public static void OpenAndAddToWordprocessingStream(Stream stream, string txt)
{
// Open a WordProcessingDocument based on a stream.
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream, true))
{
// Assign a reference to the existing document body.
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
// Add new text.
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(txt));
**wordprocessingDocument.MainDocumentPart.Document.Save();**
// HTTP response
HttpResponse Response = HttpContext.Current.Response;
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}