使用C#將數據從excel文件中導出實例代碼
來源:
發布時間:2010/9/27
瀏覽次數:1221
使用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);//合并單元格