asp.net打开word文档

时间:2012-02-16 08:43:06

标签: c# asp.net ms-word

我尝试用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();

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

2 个答案:

答案 0 :(得分:0)

您可以尝试使用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();
}

答案 1 :(得分:0)

请尝试以下代码:

//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();