如何使用ASP.NET C将XL工作表名称赋予DataTable

时间:2016-01-22 08:44:40

标签: c# asp.net excel webforms

我在单个XL文件中导出三张工作表。但我得到的工作表名称如Table1,Table2,Table3。

以下是没有调用数据库的C#代码,正确处理工作表名称。

   var ds = new DataSet();
   var dt = new DataTable("TableName For Sheet1");
   dt.Columns.Add("col1");
   dt.Columns.Add("col2");
   dt.Rows.Add("Value1", "Value2");

   var dt2 = new DataTable("TableName For Sheet2");
   dt2.Columns.Add("col1");
   dt2.Columns.Add("col2");
   dt2.Rows.Add("Value1", "Value2");
   ds.Tables.Add(dt);
   ds.Tables.Add(dt2);
   ExcelHelper.ToExcel(ds, "test.xls", Page.Response);

下面是用于调用数据库存储过程的C#代码,但是表名为Table1,Table2,Table2。

    DataSe ds = new DataSet();
    DataTable dt = new DataTable("Registration Details");
    DataTable dt1 = new DataTable("Education Details");
    DataTable dt2 = new DataTable("Employeement Details");


    dt = bl.Get_Registrationdetailsbydate(bo);
    gv_Regdetails.DataSource = dt;
    gv_Regdetails.DataBind();
    dt1 = bl.Get_Registrationdetailsbydate1(bo);
    dt2 = bl.Get_Registrationdetailsbydate2(bo);
    DataTable filteredEducation = dt1.AsEnumerable()
                                     .Where(x => dt.AsEnumerable()
                               .Any(z => z.Field<string>("Email").Trim() == x.Field<string>("Email").Trim()))
                               .CopyToDataTable();
    DataTable filteredEmployee = dt2.AsEnumerable()
                                    .Where(x => dt.AsEnumerable()
                              .Any(z => z.Field<string>("Email").Trim() == x.Field<string>("Email").Trim()))
                              .CopyToDataTable();

    ds.Tables.Add(dt);
    ds.Tables.Add(filteredEducation);
    ds.Tables.Add(filteredEmployee);
    ExcelHelper.ToExcel(ds, "DangoteUsers.xls", Page.Response);

我需要工作表名称结果,如注册详情,教育详情和员工详细信息。

我引用此示例:http://www.codeproject.com/Articles/31516/Export-DataSet-to-Multiple-Excel-Sheets

1 个答案:

答案 0 :(得分:1)

初始化后,您将覆盖dtdt1dt2,这就是为什么您没有正确获取名称而是获得表的默认名称。尝试在分配后为这三个表设置名称,或在分配逻辑中包含表名。我更新了您的代码以证明这一点。

DataSet ds = new DataSet();


var dt = bl.Get_Registrationdetailsbydate(bo);
gv_Regdetails.DataSource = dt;
gv_Regdetails.DataBind();
var dt1 = bl.Get_Registrationdetailsbydate1(bo);
var dt2 = bl.Get_Registrationdetailsbydate2(bo);
DataTable filteredEducation = dt1.AsEnumerable()
                                 .Where(x => dt.AsEnumerable()
                           .Any(z => z.Field<string>("Email").Trim() == x.Field<string>("Email").Trim()))
                           .CopyToDataTable();
DataTable filteredEmployee = dt2.AsEnumerable()
                                .Where(x => dt.AsEnumerable()
                          .Any(z => z.Field<string>("Email").Trim() == x.Field<string>("Email").Trim()))
                          .CopyToDataTable();

dt.TableName = "Registration Details";
filteredEducation.TableName = "Education Details";
filteredEmployee.TableName = "Employeement Details";

ds.Tables.Add(dt);
ds.Tables.Add(filteredEducation);
ds.Tables.Add(filteredEmployee);
ExcelHelper.ToExcel(ds, "DangoteUsers.xls", Page.Response);