【问题标题】:asp.net open word documentasp.net 打开word文档
【发布时间】:2012-02-16 08:43:06
【问题描述】:

我尝试用c#打开一个word文档。
当我打开文档后,页面被屏蔽了。

代码如下:

HttpContext.Current.Response.Write(temp);
//HttpContext.Current.Response.End();

//HttpContext.Current.Response.Flush();

//HttpContext.Current.Response.Write(sw.ToString());
//HttpContext.Current.Response.clear();
//HttpContext.Current.Response.End();
//HttpContext.Current.Response.SuppressContent = true;
//HttpContext.Current.Response.Close();
//Response.Redirect(Page.Request.Url.AbsolutePath.Substring(0, Page.Request.Url.AbsolutePath.LastIndexOf("/")) + "/PIEditor.aspx?PostID=" + Request.Params["PostID"], true);`
//HttpContext.Current.Response.End();

如您所见,我尝试了不同的选项,但没有结果,显示打开或保存文档的窗口,但我无法单击页面之后的任何按钮。它似乎已停用或停止。

【问题讨论】:

  • 您粘贴的代码看起来像是在尝试将消息发送回网页,而不是打开 Word 文档。你想达到什么目的?
  • HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "PIExport.xls")); HttpContext.Current.Response.ContentType = "应用程序/ms-excel";我尝试打开文档。它像我一样打开,但问题出现在页面停止后......谢谢。
  • 你把我和 cmets 搞混了,他们是 cmets 还是你用他们?
  • 我试过了,但没有结果
  • 如您所见,它们现在已被评论

标签: c# asp.net ms-word


【解决方案1】:

您可以尝试GemBox.Document 组件从 ASP.NET 应用程序中导出 Word 文档,如果您正在尝试这样做的话。

这是一个示例 C# 代码,应该放在后面的 ASPX 页面代码中:

// Create a new empty document.
DocumentModel document = new DocumentModel();

// Add document content.
document.Sections.Add(new Section(document, new Paragraph(document, "Hello World!")));

// Microsoft Packaging API cannot write directly to Response.OutputStream.
// Therefore we use temporary MemoryStream.
using (MemoryStream documentStream = new MemoryStream())
{
    document.Save(documentStream, SaveOptions.DocxDefault);

    // Stream file to browser.
    Response.Clear();
    Response.ContentType = "application/vnd.openxmlformats";
    Response.AddHeader("Content-Disposition", "attachment; filename=Document.docx");

    documentStream.WriteTo(Response.OutputStream);

    Response.End();
}

【讨论】:

  • 虽然从您的用户名中可以清楚地看出您与制作该软件的公司有关联,但如果您直接说明这一点,将来会更好。例如,“你可以试试 GemBox.Document 组件(由我工作的公司创建)......”
  • 非常棒且文档齐全的软件!
【解决方案2】:

试试下面的代码:

//create new MemoryStream object and add PDF file’s content to outStream.
MemoryStream outStream = new MemoryStream();

//specify the duration of time before a page cached on a browser expires
Response.Expires = 0;

//specify the property to buffer the output page
Response.Buffer = true;

//erase any buffered HTML output
Response.ClearContent();

//add a new HTML header and value to the Response sent to the client
Response.AddHeader(“content-disposition”, “inline; filename=” + “output.doc”);

//specify the HTTP content type for Response as Pdf
Response.ContentType = “application/msword”;

//write specified information of current HTTP output to Byte array
Response.BinaryWrite(outStream.ToArray());

//close the output stream
outStream.Close();

//end the processing of the current page to ensure that no other HTML content is sent
Response.End();

【讨论】:

  • 您应该将MemoryStream 放入using 块中,
猜你喜欢
  • 2020-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多