2012-12-14 73 views
0

我已經創建Excel工作表,並保存使用發送excelsheet爲響應

xlWorkBook.SaveCopyAs("abc.xls"); 

然後我使用

byte[] byteArray = File.ReadAllBytes("abc.xls"); 
File.Delete("abc.xls"); 
Response.ContentType = "application/vnd.ms-excel"; 
Response.AppendHeader("Content-Disposition", "attachment; filename=abc.xls"); 
Response.BinaryWrite(byteArray); 
Response.End(); 

到Excel工作表作爲發送響應。

但問題是在客戶端它首先提示保存excel工作簿「Book1」,然後它提示保存「abc.xls」。如何防止呢?

早些時候,我使用

xlWorkBook.SaveAs("abc.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); 

,它工作正常(直接提示保存 「abc.xls」)。但我改xlWorkBook.SaveCopyAs()因爲IIS

+0

_because了'xlWorkBook.SaveAs()'被扔例外IIS_部署時:沒有你嘗試先解決該異常?似乎與訪問權限或類似這樣的事情... –

+0

@Yannick Blondeau,如果訪問權限引起的問題,與.SaveCopyAs方法具有相同的路徑?:) 當然,異常應該修復,而不是尋找解決方法。 –

+0

早些時候我將它部署在Windows Server 2003 R2 - IIS 6.0中,它在那裏工作的很好。現在我將它部署在Windows Server 2008 R2 - IIS 7.0中,並且我正面臨問題 – Rajneesh

回答

0

部署時xlWorkBook.SaveAs()被扔例外,我有我的代碼

string fileName = "xyz.xls"; 

    HttpContext.Current.Response.Clear(); 
    HttpContext.Current.Response.ClearHeaders(); 
    HttpContext.Current.Response.ClearContent(); 
    HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName)); 
    HttpContext.Current.Response.ContentType = "application/vnd.xls";   

    /*  
    using (StringWriter sw = new StringWriter()) 
    { 
     using (HtmlTextWriter htw = new HtmlTextWriter(sw)) 
     { 
      //extract 
     } 
    } 
    */ 


    HttpContext.Current.Response.Write(sw.ToString()); 
    HttpContext.Current.Response.Flush(); 
    HttpContext.Current.Response.End(); 
+0

另外我還有 HttpContext.Current.Response.ContentType =「application/vnd.xls」; 在我的code.Also試試它而不是應用程序/ vnd.ms-excel –

+0

我試着改變代碼,因爲你已經告訴,但它不工作:( – Rajneesh

+0

已編輯的代碼 你只需要做的更改 Response.BinaryWrite(byteArray); inplace of Response.Write of my comment code。 可能會在您身邊刷新或清除標頭問題 –