把指定的DataSet中的内容导出到Excel中

  /// <summary>
  /// 把指定的DataSet中的内容导出到Excel中.
  /// </summary>
  /// <param name="ds">ds参数为DateSet对象</param>
  /// <param name="ImpStr">该字符串用来指定一些字段来导出,其使用格式为:aaa|AAA,bbb|BBB   小写字符串为在Excel表中显示该字段的列标题,大写表示字段名.不同字段使用逗号分隔.</param>
  /// <param name="locR">数据导出到Excel中的开始行</param>
  /// <param name="locC">数据导出到Excel中的开始列</param>
  /// <param name="TitleStr">该字符串用来指定要导出的表和表的标题其格式为:aaa|AAA,bbb|BBB   小写为该表中显示的标题,大写为表名称.不同表使用逗号分隔</param>
  /// <param name="TilteLocR">显示标题的行</param>
  /// <param name="TitleLocC">显示标题的列</param>
  /// <param name="ColTitleColorIndex">列标题颜色索引</param>
  /// <example>Import2Excel(this.dsData ,"用户名|USERNAME,登录名|LOGINNAME",3,1,"用户名列表|BILL_USER",1,2 ,12);</example>
  private void Import2Excel(System.Data.DataSet ds,string ImpStr ,int locR,int locC,string TitleStr,int TilteLocR, int TitleLocC,int ColTitleColorIndex )
  {
   //加载Excel
   Microsoft.Office.Interop.Excel.ApplicationClass ex=new Microsoft.Office.Interop.Excel.ApplicationClass();
   ex.Visible=true ;//显示Excel
   if (ex==null)
   {
    //无法显示时报错
   MessageBox.Show("无法启动 Microsoft Office Excel,导出失败!","Import2Excel", MessageBoxButtons.OK,MessageBoxIcon.Stop );
   return ;
   }
   string[] tles=TitleStr.Split(",".ToCharArray());
   string[] Imps=ImpStr.Split(",".ToCharArray()) ;
   string[] tlbs=TitleStr.Split(",".ToCharArray ());
   //获得工作薄
       Microsoft.Office.Interop.Excel.Workbooks   wbs =ex.Workbooks;
   Microsoft.Office.Interop.Excel.Workbook wb= wbs.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
  

   int z=0;//Sheet索引
   foreach (string tblname in tlbs)
   {
    z+=1;
    //得到表和工作表一个工作表对应一个表,
    string[] tbn=tblname.Split("|".ToCharArray());
    System.Data.DataTable dt=ds.Tables[tbn[1].ToUpper() ];
    Microsoft.Office.Interop.Excel.Worksheet ws=
     (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[z];
    //工作表的第一行标题设置为该表的表名.
    //从格式字符串中去表名对应的名称.
//    int tm1=TitleStr.ToUpper().IndexOf(dt.TableName+"|")+dt.TableName.Length +1;
//    string tit=TitleStr.Substring(tm1,TitleStr.IndexOf(",",tm1)-tm1);
    ws.Cells[TilteLocR, TitleLocC]=tbn[0] ;//设置该单元格显示的标题
    ws.Name=dt.TableName ;//设置该Sheet的名称为表名.
    int n1=locC;
    //设置字段名称
    foreach (string imp in Imps)
     {
     string[] im=imp.Split("|".ToCharArray());
      n1+=1 ;
      ws.Cells[locR,  n1]=im[0];//'字段名称
      Microsoft.Office.Interop.Excel.Range rg=
           (Microsoft.Office.Interop.Excel.Range)ws.Cells[locR, n1];
      rg.Interior.ColorIndex=ColTitleColorIndex;
      rg.Font.Bold=true;
     }
    for (int y=0;y<=dt.Rows.Count -1;y+=1)
    {
    System.Data.DataRow dr=dt.Rows[y];
     int n2=1;
     foreach (string imp in Imps)
     {
     string[] im=imp.Split("|".ToCharArray());
     ws.Cells[locR+1+y,locC+n2]=dr[im[1].ToUpper()];
     n2+=1;  
     }
    }
   }
  } 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值