使用EFP PLUS从SQL导出到Excel的名称表 [英] Name sheets exported from SQL to excel using EFP PLUS

查看:89
本文介绍了使用EFP PLUS从SQL导出到Excel的名称表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将sql存储过程中的数据导出到一个工作簿中的单独工作表上。代码工作,但我的工作表名称在excel文件选项卡上被称为'sheet1','sheet2'等



i想在excel上拥有我自己的工作表名称文件标签例如sheet1是名字'BenCount',sheet2'工资'他们有意义等我的代码:



我尝试过: < br $>


I am exporting data from a sql stored procedure to excel on seperate sheets in one workbook . the code works however my sheet names are called 'sheet1' , 'sheet2' etc. on the excel file tab

i would like to have my own sheet names on the excel file tab e.g. sheet1 be name 'BenCount', sheet2 'Wages'so that they make sense etc. my code:

What I have tried:

      SqlConnection connex = new SqlConnection();

      SqlCommand command = new SqlCommand("spTest", con) { CommandType = System.Data.CommandType.StoredProcedure };

      SqlDataAdapter sda = new SqlDataAdapter();

      command.Connection = con;
      sda.SelectCommand = command;
      command.CommandTimeout = 600;

      DataSet ds = new DataSet();

      sda = new SqlDataAdapter("spTest", con);
      sda.Fill(ds);

//Set Name of DataTables.
      ds.Tables[0].TableName = "Bencount";
      ds.Tables[1].TableName = "Wages";
      ds.Tables[3].TableName = "BeneficiariestobeExctracted";
      ds.Tables[4].TableName = "benificiarieswithnoAccNo";
      ds.Tables[5].TableName = "account number>11 characters/wages<100";
      ds.Tables[6].TableName = "beneficiary appear on more than project";
      ds.Tables[7].TableName = "Duplications by account number";
      ds.Tables[8].TableName = "Check Missing Beneficiaries";





          if (ds.Tables.Count > 0)
          {
              MemoryStream ms = new MemoryStream();
              int i = 1;
              using (ExcelPackage package = new ExcelPackage(ms))
              {
                  foreach (DataTable table in ds.Tables)
                  {
                      ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(table.TableName);



                      worksheet.Cells["A2"].LoadFromDataTable(table, true);
                  }
                  Response.Clear();
                  package.SaveAs(Response.OutputStream);
                  Response.AddHeader("content-disposition", "attachchment; filename=Example.xls");

                  Response.Charset = "";
                  Response.ContentType = "application/vnd.xls";
                  Response.End();
              }
          }
  }
}

推荐答案

如何更改工作表Excel电子表格中的C#名称 - 堆栈溢出 [ ^ ]


您好我用if语句重命名每张表

Hi i used an if statement to rename the each sheet
if (worksheet.Name == "sheet1")
                    { worksheet.Name = "Bencount"; }.



感谢您的建议:)


thanks for the advice :)


这篇关于使用EFP PLUS从SQL导出到Excel的名称表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆