简体   繁体   中英

Export to Excel using EPPlus worksheet name with space

I have a working program that pull data from my database and export to Excel using EPPlus. However, the user specifically asked the name to be exactly how they want it, which contains white space in the worksheet name. Currently I have the worksheet name as "Reorder_Point" and the program works just fine, but if I change it to "Reorder Point" it would error out. Anyone have any idea how to do this? Thanks!

string cnAPStr = System.Configuration.ConfigurationManager.ConnectionStrings["conAP_SQLWeb"].ConnectionString; ;
string queryReorder = "exec uspDataPull_ReorderPointInfo";
SqlConnection cn = new SqlConnection(cnAPStr);

SqlDataAdapter daReorder = new SqlDataAdapter(queryReorder, cn);
DataSet dsReorder = new DataSet();
daReorder.Fill(dsReorder);
dsReorder.Tables[0].TableName = "Reorder_Point";

DumpToExcel(dsReorder);

private void DumpToExcel(DataSet dsReorder)
{
    try
    {
        using (ExcelPackage pck = new ExcelPackage())
        {
            ExcelWorksheet ws1 = pck.Workbook.Worksheets.Add(dsReorder.Tables[0].TableName);
            ws1.Cells["A1"].LoadFromDataTable(dsReorder.Tables[0], true);
            using (ExcelRange rng = ws1.Cells["A1:AG1"])
            {
                rng.Style.Font.Bold = true;
            }

            string fileName = "RP_DataPull.xlsx";
            Response.Clear();
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
            Response.BinaryWrite(pck.GetAsByteArray());
        }
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}

您可以给工作表命名,而不是依赖源列名称。

ws1.Name = "Reorder Point";

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