简体   繁体   中英

Export to Excel, and change Sheet name

Here's the code I'm using:

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

It can specify the filename but not the Sheet name. Is it possible?

I am using ExcelPackage to generate excel sheets and you have the option to specify sheet name too.

Example :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM