2012-02-16 206 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