7*24小时应急电话:15927160396
首页 新闻资讯 技术文章
使用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.
53.        finally
54.        {
55.            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
56.            xlApp = null;
57.            GC.Collect();
58.        }
59.    }
60.
61.格式控制
62.xSt.get_Range(excel.Cells[1,1],excel.Cells[1,1]).HorizontalAlignment   =   Excel.XlHAlign.xlHAlignCenter;//对齐方式 
63.      xSt.get_Range(excel.Cells[1,1],excel.Cells[1,1]).Select();//设置选中的部分的颜色 
64.      xSt.get_Range(excel.Cells[1,1],excel.Cells[1,1]).Interior.ColorIndex   =   19;//设置为浅黄色,共计有56种 
65.      xSt.get_Range(excel.Cells[1,1],excel.Cells[1,1]).Font.Bold   =   true;//字体类型 
66.      xSt.get_Range(excel.Cells[1,1],excel.Cells[1,1]).Font.Size   =   22;//字体大小 
67.  
68.      xSt.get_Range("a1","a1").ColumnWidth   =   20;//列宽度 
69.      object   oo   =   false; 
70.      xSt.get_Range("A1","B1").Merge(oo);//合并单元格

版权所有:武汉网福互联科技有限公司    鄂ICP备09022096号
业务QQ:23444550 客服QQ:267052100 电邮:23444550@qq.com  

鄂公网安备 42010602000905号

手机站二维码