2012-02-16 174 views
-2

我尝试用c#打开一个word文档。
当我打开文档时,页面被阻止。asp.net打开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(); 

正如你看到的,我尝试了不同的选择,但无果而终,显示打开或保存文档的窗口,但我不能点击任何按钮的页面后, 。看起来它已停用或停止。

+0

代码已粘贴貌似你试图发送一个消息返回到网页,而不是打开Word文档。你想达到什么目的? – Jeggs 2012-02-16 08:45:49

+0

HttpContext.Current.Response.AddHeader(“content-disposition”,string.Format(“attachment; filename = {0}”,“PIExport.xls”)); HttpContext.Current.Response.ContentType =“application/ms-excel”; 我尝试打开文档。其打开像我这样但页面停止后问题来了.. 谢谢。 – user1213375 2012-02-16 08:47:40

+0

你把我的评论混淆了,他们是评论还是你用它们? – Aristos 2012-02-16 08:54:31

回答

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(); 
} 
+0

尽管从用户名中可以清楚地看到,您与制作该软件的公司相关联,但如果您直接说明,将来会更好。例如,“您可以尝试GemBox.Document组件(由我工作的公司创建)......” – 2015-02-26 01:31:31

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(); 
+0

您应该将'MemoryStream'放入'using'块中, – 2015-02-26 01:29:35