当前位置:软件学习 > Excel >>

使用C#将数据从excel文件中导出实例代码

使用C#讲数据从excel文件中导出实例代码如下:

  1. private void doExport(DataSet ds1,string[] str)
  2.     {
  3.         System.Reflection.Missing oMissing = System.Reflection.Missing.Value;
  4.         _Application xlApp = null;
  5.         _Workbook xlWorkbook = null;
  6.         _Worksheet xlWorksheet = null;
  7.         try
  8.         {
  9.             xlApp = new ApplicationClass();
  10.             xlApp.Visible = true;
  11.             xlWorkbook = xlApp.Workbooks.Add(oMissing);
  12.             xlWorksheet = xlWorkbook.Worksheets.Add(oMissing, oMissing, 1, oMissing) as _Worksheet;
  13.             xlWorksheet.Name = "福州市盲人易做图单位信息";
  14.             Excel.Range r;
  15.             r = xlWorksheet.get_Range(xlWorksheet.Cells[1, 1], xlWorksheet.Cells[1, 7]); //取得合并的区域
  16.             xlWorksheet.get_Range(xlWorksheet.Cells[1, 1], xlWorksheet.Cells[1, 7]).Font.Size = 20;
  17.             xlWorksheet.get_Range(xlWorksheet.Cells[1, 1], xlWorksheet.Cells[1, 7]).RowHeight = 45;
  18.             xlWorksheet.get_Range(xlWorksheet.Cells[2, 1], xlWorksheet.Cells[2, 1]).ColumnWidth = 5;
  19.             xlWorksheet.get_Range(xlWorksheet.Cells[2, 3], xlWorksheet.Cells[2, 3]).ColumnWidth = 33;
  20.             xlWorksheet.get_Range(xlWorksheet.Cells[2, 6], xlWorksheet.Cells[2, 6]).ColumnWidth = 15;
  21.             xlWorksheet.get_Range(xlWorksheet.Cells[2, 7], xlWorksheet.Cells[2, 7]).ColumnWidth = 50;
  22.             r.MergeCells = true;
  23.             r.HorizontalAlignment = Excel.Constants.xlCenter;
  24.             xlWorksheet.Cells[1, 1] = "福州市盲人易做图单位信息";
  25.             xlWorksheet.Cells[2, 1] = "ID";
  26.             xlWorksheet.Cells[2, 2] = "单位编号";
  27.             xlWorksheet.Cells[2, 3] = "单位名称";
  28.             xlWorksheet.Cells[2, 4] = "单位类型";
  29.             xlWorksheet.Cells[2, 5] = "负责人";
  30.             xlWorksheet.Cells[2, 6] = "联系电话";
  31.             xlWorksheet.Cells[2, 7] = "联系地址";
  32.           
  33.             for (int i = 0; i < ds1.Tables[0].Rows.Count; i )
  34.             {
  35.                 xlWorksheet.Cells[i 3, 1] = i 1;
  36.                 xlWorksheet.Cells[i 3, 2] = ds1.Tables[0].Rows[i][1].ToString();
  37.                 xlWorksheet.Cells[i 3, 3] = ds1.Tables[0].Rows[i][2].ToString();
  38.                 xlWorksheet.Cells[i 3, 4] = ds1.Tables[0].Rows[i][3].ToString();
  39.                 xlWorksheet.Cells[i 3, 5] = ds1.Tables[0].Rows[i][4].ToString();
  40.                 xlWorksheet.Cells[i 3, 6] = ds1.Tables[0].Rows[i][5].ToString();
  41.                 xlWorksheet.Cells[i 3, 7] = ds1.Tables[0].Rows[i][6].ToString();
  42.           
  43.             }
  44.             xlWorkbook.SaveAs(Hiddenfield1.Value, oMissing, oMissing, oMissing, oMissing,
  45.                 oMissing, Excel.XlSaveAsAccessMode.xlShared, oMissing, oMissing, oMissing,
  46.                 oMissing, oMissing);
  47.             xlApp.Quit();
  48.         }
  49.         catch
  50.         {
  51.         }
  52.         finally
  53.         {
  54.             System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
  55.             xlApp = null;
  56.             GC.Collect();
  57.         }
  58.     }
  59. 格式控制
  60. xSt.get_Range(excel.Cells[1,1],excel.Cells[1,1]).HorizontalAlignment   =   Excel.XlHAlign.xlHAlignCenter;//对齐方式 
  61.       xSt.get_Range(excel.Cells[1,1],excel.Cells[1,1]).Select();//设置选中的部分的颜色 
  62.       xSt.get_Range(excel.Cells[1,1],excel.Cells[1,1]).Interior.ColorIndex   =   19;//设置为浅黄色,共计有56种 
  63.       xSt.get_Range(excel.Cells[1,1],excel.Cells[1,1]).Font.Bold   =   true;//字体类型 
  64.       xSt.get_Range(excel.Cells[1,1],excel.Cells[1,1]).Font.Size   =   22;//字体大小 
  65.   
  66.       xSt.get_Range("a1","a1").ColumnWidth   =   20;//列宽度 
  67.       object   oo   =   false; 
  68.       xSt.get_Range("A1","B1").Merge(oo);//合并单元格
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,