小弟想请教一个问题,我用Microsoft.Office.Interop.Word将服务器上一个doc文件的书签的值改了,然后我想将这个doc当模板用,在打开的地方直接把文件不保存传给客户端代码如下:Word.Application wordApp = new Word.Application();
object fileobj = docFileName;
object nullobj = System.Reflection.Missing.Value;
//打开指定文件(不同版本的COM参数个数有差异,一般而言除第一个外都用nullobj就行了)
Word.Document doc = wordApp.Documents.Open(ref fileobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj
);
string strCase = string.Empty;
foreach (Word.Book BM in doc.Books) //这是最关键的地方:对文档的任何书签进行便利匹配
{
strCase = BM.Name.ToString();
switch (strCase)
{
case "E_ID": //替换Advice书签的内容,其他相同
BM.Select();
BM.Range.Text = "asd";
break;
}
但是怎么才能给用户提供下载呢?请指教
object fileobj = docFileName;
object nullobj = System.Reflection.Missing.Value;
//打开指定文件(不同版本的COM参数个数有差异,一般而言除第一个外都用nullobj就行了)
Word.Document doc = wordApp.Documents.Open(ref fileobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj
);
string strCase = string.Empty;
foreach (Word.Book BM in doc.Books) //这是最关键的地方:对文档的任何书签进行便利匹配
{
strCase = BM.Name.ToString();
switch (strCase)
{
case "E_ID": //替换Advice书签的内容,其他相同
BM.Select();
BM.Range.Text = "asd";
break;
}
但是怎么才能给用户提供下载呢?请指教
解决方案 »
- 请教有关GridView取值问题
- 树控件不刷新
- .NET中水晶報表打印問題,高手請進!!
- 做过Forms身份验证基于角色访问授权的进来,出问题了,急呀
- 如何在网页中显示Word文档
- (倾尽所有的分诚心向各位请教)请问在.net中有几种常用的架构,分别的作用?
- 老板让学.net,我只有一点asp基础,今后要多多麻烦大家,先报个到!!!
- ASP.NET web程序运行的平台问题(在线等)
- 一个很简单的关于命名空间的问题。
- 我是.net新人,请问,我用asp.net做的网站是发布到IIS上吗?那Framework是干什么的呀?
- 未能加载文件或程序集AjaxControlToolkit 3.5的问题
- 请问asp.net如何实现百度贴吧的插入图片功能呢?
下面文章讨论用 DocX
save the document created by docX into response and send it to user for downloading
http://stackoverflow.com/questions/8988959/save-the-document-created-by-docx-into-response-and-send-it-to-user-for-download
http://stackoverflow.com/questions/10667513/streaming-in-memory-word-document-using-openxml-sdk-w-asp-net-results-in-corrup
using (MemoryStream mem = new MemoryStream())
{
// Create Document
using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(mem,
WordprocessingDocumentType.Document, true))
{
// Add a main document part.
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); new Document(new Body()).Save(mainPart); Body body = mainPart.Document.Body;
body.Append(new Paragraph(
new Run(
new Text("Hello World!")))); mainPart.Document.Save();
// Stream it down to the browser // THIS IS PROBABLY THE CRUX OF THE MATTER <---
Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx");
Response.ContentType = "application/vnd.ms-word.document";
mem.WriteTo(Response.OutputStream);
Response.End();
} }Solution for those who stumble on this question:Within the using().. directive of the WordProcessingDocument, you must call:wordDocument.Save();Also to correctly stream the MemoryStream, use this in the outer using block:
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx");
mem.Position = 0;
mem.CopyTo(Response.OutputStream);
Response.Flush();
Response.End();