C#中调用EXCEL和将datagridview数据导出到execle

来源:互联网 发布:如何打开8080端口 编辑:程序博客网 时间:2024/06/05 16:38
 


在Visual C#中调用Excel表格,并不像读取Excel表格中的数据那么容易了,因为在Visual C#中调用Excel表格要使用到Excel的COM组件。如果你安装Office套件在"C"盘,那么在"C:/Program Files/Microsoft Office/Office"可以找到这个COM组件"EXCEL9.OLB",这些COM组件都是非受管代码的,要在Visual C#中使用这些非受管代码的COM组件,就必须把他们转换成受管代码的类库。所以在用Visual C#调用Excel表格之前,必须完成从COM组件的非受管代码到受管代码的类库的转换。
  (1).非受管代码COM组件转换成受管代码的类库:
      首先把COM组件"EXCEL9.OLB"拷贝到C盘的根目录下,在命令提示符窗口(cmd.exe)里输入path %path%;C:/Program Files/Microsoft Visual Studio .NET 2003/SDK/v1.1/bin。(创建新路径)。
           然后输入c:回车,再输入cd/回车,(在c盘根目录下编辑)
           最后输入下列命令:tlbimp excel9.olb
     这样在C盘的根目录下面就产生了三个DLL文件:"Excel.dll"、"Office.dll"、"VBIDE.dll"。在产生了上面的三个文件后,这种转换就成功完成了。在下面的程序中,就可以利用这转换好的三个类库编写和Excel表格相关的各种操作了。

       (2).Visual C#打开Excel表格:
          "Excel.dll"中定义了一个命名空间"Excel",在差命名空间中封装了一个类"Application",这个类和启动Excel表格有非常重要的关系,在Visual C#中,只需要下列三行代码就可以完成打开Excel表格的工作,具体如下:
                 Excel.ApplicationClass Mylxls = new Excel.ApplicationClass();
                 Mylxls.Application.Workbooks.Add(true);
                 Mylxls.Visible = true ;
  但此时的Excel表格是一个空的表格,没有任何内容,下面就来介绍如何往Excel表格中输入数据。

  (3).往Excel表格中输入数据:
   在命名空间"Excel"中,还定义了一个类"Cell",这个类所代表的就是Excel表格中的一个下单元。通过给差"Cell"赋值,从而实现往Excel表格中输入相应的数据,下列代码功能是打开Excel表格,并且往表格输入一些数据。
                 Excel.ApplicationClass Mylxls = new Excel.ApplicationClass();
                 Mylxls.Application.Workbooks.Add(true);
                 Mylxls.Caption = "XX市接收安置复员退伍军人登记表";
                 Mylxls.Cells[1, 1] = "XX市200   年接收安置复员退伍军人登记表";
                 Mylxls.Cells[2, 1] = "XX市人民政府接待安置复员退伍军人办公室";

       (4). Visual C#调用Excel表格,
  了解了上面的这些知识,得到完成上述功能的程序代码就显得比较容易了,函数具体如下:

private void myExcel()
         {
             try
             {
                 Excel.ApplicationClass Mylxls = new Excel.ApplicationClass();
                 Mylxls.Application.Workbooks.Add(true);
                 Mylxls.Caption = "XX市接收安置复员退伍军人登记表";
                 Mylxls.Cells[1, 1] = "XX市200   年接收安置复员退伍军人登记表";
                 Mylxls.Cells[2, 1] = "XX市人民政府接待安置复员退伍军人办公室";
                 Mylxls.Cells[3, 1] = "入伍所在地";
                 Mylxls.Cells[3, 2] = "籍贯";
                 Mylxls.Cells[3, 3] = "退伍所在";
                 Mylxls.Cells[3, 11] = "退伍前";
                 Mylxls.Cells[3, 21] = "工作安排情况";
                 Mylxls.Cells[4, 1] = "省、市、镇(街)";
                 Mylxls.Cells[4, 2] = "省、市(县)";
                 Mylxls.Cells[4, 3] = "镇、街";
                 Mylxls.Cells[4, 4] = "村、居委";
                 Mylxls.Cells[4, 5] = "姓名";
                 Mylxls.Cells[4, 6] = "性别";
                 Mylxls.Cells[4, 7] = "出生年月";
                 Mylxls.Cells[4, 8] = "文化程度";
                 Mylxls.Cells[4, 9] = "政治面貌";
                 Mylxls.Cells[4, 10] = "入伍时间";
                 Mylxls.Cells[4, 11] = "部队番号";
                 Mylxls.Cells[4, 12] = "奖励情况";
                 Mylxls.Cells[4, 13] = "职务";
                 Mylxls.Cells[4, 14] = "有何技术";
                 Mylxls.Cells[4, 15] = "复退证编号(含残疾)";
                 Mylxls.Cells[4, 16] = "身高(cm)";
                 Mylxls.Cells[4, 17] = "联系电话";
                 Mylxls.Cells[4, 18] = "安置证编号";
                 Mylxls.Cells[4, 19] = "档案编号";
                 Mylxls.Cells[4, 20] = "退伍类别";
                 Mylxls.Cells[4, 21] = "单位名称";
                 Mylxls.Cells[4, 22] = "时间";
                 Mylxls.Cells[4, 23] = "档案转换";
                 Mylxls.Cells[4, 24] = "备注";
            //合并单元格
                 Mylxls.get_Range(Mylxls.Cells[1, 1], Mylxls.Cells[1, 24]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[2, 1], Mylxls.Cells[2, 24]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[3, 3], Mylxls.Cells[3, 4]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[3, 5], Mylxls.Cells[4, 5]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[3, 6], Mylxls.Cells[4, 6]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[3, 7], Mylxls.Cells[4, 7]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[3, 8], Mylxls.Cells[4, 8]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[3, 9], Mylxls.Cells[4, 9]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[3, 10], Mylxls.Cells[4, 10]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[3, 15], Mylxls.Cells[4, 15]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[3, 16], Mylxls.Cells[4, 16]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[3, 17], Mylxls.Cells[4, 17]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[3, 18], Mylxls.Cells[4, 18]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[3, 19], Mylxls.Cells[4, 19]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[3, 20], Mylxls.Cells[4, 20]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[3, 24], Mylxls.Cells[4, 24]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[3, 11], Mylxls.Cells[3, 14]).MergeCells = true;
                 Mylxls.get_Range(Mylxls.Cells[3, 21], Mylxls.Cells[3, 23]).MergeCells = true;

                //逐行写入数据,数组中前4行我列标题
                 int rowIndex = 5;//在EXCEL第5行开始写入数据
                 int row_cnt = this.dataViewAll.Rows.Count;
                 int col_cnt = this.dataViewAll.Columns.Count;
                 //这里要注意,由于DataGrid中总是要多显示一行,而最后一行中没有数据,所以在读取的时候不要读最后一行
                 for (int row = 0; row < row_cnt - 1; row++)
                 {
                     for (int col = 0; col < col_cnt; col++)
                     {
                         try
                         {
                             Mylxls.Cells[rowIndex, col + 1] = dataViewAll.Rows[row].Cells[col].Value.ToString();
                             //Mylxls.Cells[rowIndex, col + 1] = drugListDataGridView.Rows[row].Cells[col].Value.ToString();
                         }
                         catch
                         {
                             MessageBox.Show("数据导出出现异常!请重试");
                             return;
                         }
                     }
                     rowIndex++;
                 }
                 Mylxls.Visible = true;
                 AddMessage("登记信息数据导出成功!");
             }                
             catch //(Exception e)
             {
                 AddMessage("登记信息数据导出失败!");
             }
         }