导出到Excel,然后更改工作表名称

时间:2011-05-22 05:05:21

标签: c# asp.net export-to-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();
    }
}

它可以指定文件名,但不能指定工作表名称。有可能吗?

1 个答案:

答案 0 :(得分:1)

我正在使用ExcelPackage生成Excel工作表,您也可以选择指定工作表名称。

示例:

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