2011-05-22 35 views
2

下面是我使用的代碼:導出到Excel,並改變工作表名稱

HttpContext.Current.Response.Clear(); 
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Single_Raw.xls")); 
HttpContext.Current.Response.ContentType = "application/ms-excel"; 

using (StringWriter sw = new StringWriter()) 
{ 
    using (HtmlTextWriter htw = new HtmlTextWriter(sw)) 
    { 
     Table tb = new Table(); 
     TableRow tr = new TableRow(); 

     tr.Cells.Add((rawRow((lblPOR1.Text.Substring(0, 4)), (lblPOR1.Text.Substring(5, 3)), (lblPOR1.Text.Substring(9, 3)), lblPNL.Text.ToString()))); 

     TableCell cell3 = new TableCell(); 
     cell3.Text = " "; 

     TableRow tr2 = new TableRow(); 
     tr2.Cells.Add((rawRow((lblPOR2.Text.Substring(0, 4)), (lblPOR2.Text.Substring(5, 3)), (lblPOR2.Text.Substring(9, 3)), lblPNL.Text.ToString()))); 

     tb.Rows.Add(tr); 
     tb.Rows.Add(tr2); 

     tb.RenderControl(htw); 

     HttpContext.Current.Response.Write(sw.ToString()); 
     HttpContext.Current.Response.End(); 
    } 
} 

它可以指定文件名而不是表名。可能嗎?

+2

剛一說明,我個人覺得,這是值得它需要寫一個真實的幾個小時Excel導出它創建了一個原生的Excel文件。你可以控制一切,用戶不會對文件類型產生愚蠢的警告。 – tster 2011-05-22 05:41:08

回答

1

我正在使用ExcelPackage來生成Excel表格,您也可以選擇指定表格名稱。

例子:

ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets.Add("Tinned Goods");