C#读写Excel文件的几个技巧

  • 格式:doc
  • 大小:47.50 KB
  • 文档页数:5

C#读写Excel文件的几个技巧 2009-11-25 来自:CSDN Blog 字体大小:【大 中 小】  摘要:这里将介绍一些C#读写Excel文件的相关技巧,毕竟Excel打印更为方便和实用。希望本文能对大家用好Excel有所帮助。

一直想小结一些C#读写Excel文件的相关技巧,毕竟Excel打印更为方便和实用,一个是Excel打印输出编码比Word文件打印数据简单些,另一个是Excel本身对数据超强计算处理功能;赶巧最近项目又涉及Excel报表统计打印的问题,所以在把其中的一些技术记录下来与大家一起分析讨论,次篇主要涉及两个方面内容:

1、C#读写Excel文件 A、设计Excel模版 B、打开一个目标文件并且读取模版内容 C、目标文件按格式写入需要的数据 D、保存并且输出目标Excel文件 2、 Excel对象资源释放,这个在以前项目没有注意彻底释放使用到Excel对象,对客户计算机资源造成一定浪费,此次得到彻底解决。

下面是一个C#读写Excel文件并打印输出的Demo 1、 创建一个叫DemoExcel的项目 2、 引用COM,包括:Microsoft.Excel.x.0.Object.Library,Microsoft.Office.x.0.Object.Library

建议安装正版OFFICE,而且版本在11.0以上(Office2003以上),引用以上两个Com后,在项目引用栏发现多了Excel、Microsoft.Office.Core,VBIDE三个 Library.

3、 下面建立一些模拟的数据,此处为街镇信息

1. using System; 2. using System.Collections.Generic; 3. using System.ComponentModel; 4. using System.Data; 5. using System.Drawing; 6. using System.Text; 7. using System.Windows.Forms; 8. using Microsoft.Office.Interop.Excel; 9. using Microsoft.Office.Core; 10. using System.IO; 11. using System.Reflection; 12. 13. namespace DemoExcel 14. ...{ 15. public partial class Form1 : Form 16. ...{ 17. private object missing = Missing.Value; 18. private Microsoft.Office.Interop.Excel.Application ExcelRS; 19. private Microsoft.Office.Interop.Excel.Workbook RSbook; 20. private Microsoft.Office.Interop.Excel.Worksheet RSsheet; 21. 22. public Form1() 23. ...{ 24. InitializeComponent(); 25. } 26. 27. private void Form1_Load(object sender, EventArgs e) 28. ...{ 29. // TODO: 这行代码将数据加载到表“dataSet1.STREET”中。 30. //您可以根据需要移动或移除它。 31. this.sTREETTableAdapter.Fill(this.dataSet1.STREET); 32. 33. } 34. 35. private void button1_Click(object sender, EventArgs e) 36. ...{ 37. string OutFilePath = Application.StartupPath + @" emp.xls"; 38. 39. string TemplateFilePath = Application.StartupPath + @"模版.xls"; 40. PrintInit(TemplateFilePath,OutFilePath); 41. } 42. Excle输出前初始化#region Excle输出前初始化 43. /**//// 44. /// 45. /// 46. /// 47. public bool PrintInit(string templetFile, string outputFile) 48. ...{ 49. try 50. ...{ 51. if (templetFile == null) 52. ...{ 53. MessageBox.Show("Excel模板文件路径不能为空!"); 54. return false; 55. } 56. if (outputFile == null) 57. ...{ 58. MessageBox.Show("输出Excel文件路径不能为空!"); 59. return false; 60. } 61. //把模版文件templetFile拷贝到目输出文件outputFile中,并且目标文件可以改写 62. System.IO.File.Copy(templetFile, outputFile, true); 63. if (this.ExcelRS != null) 64. ExcelRS = null; 65. //实例化ExcelRS对象 66. ExcelRS = new Microsoft.Office.Interop.Excel.ApplicationClass(); 67. //打开目标文件outputFile 68. RSbook = ExcelRS.Workbooks.Open(o 69. utputFile, missing, missing, missing, missing, missing, 70. missing, missing, missing, missing, missing, 71. missing, missing, missing, missing); 72. //设置第一个工作溥 73. RSsheet = (Microsoft.Office.Interop.Excel.Worksheet) 74. RSbook.Sheets.get_Item(1); 75. //激活当前工作溥 76. RSsheet.Activate(); 77. 78. 在当前工作溥写入内容#region 在当前工作溥写入内容 79. for (int i = 0; i < this.dataGridView1.RowCount; i++) 80. ...{ 81. RSsheet.Cells[3 + i, 1] = 82. this.dataGridView1[0, i].Value.ToString(); 83. RSsheet.Cells[3 + i, 2] = 84. this.dataGridView1[1, i].Value.ToString(); 85. RSsheet.Cells[3 + i, 3] = 86. this.dataGridView1[2, i].Value.ToString(); 87. } 88. #endregion 89. 90. //保存目标文件 91. RSbook.Save(); 92. //设置DisplayAlerts 93. ExcelRS.DisplayAlerts = false; 94. ExcelRS.Visible = true; 95. //ExcelRS.DisplayAlerts = true; 96. 97. //释放对象 98. RSsheet = null; 99. RSbook = null; 100. ExcelRS = null; 101. //释放内存 102. GcCollect(); 103. } 104. catch (Exception ex) 105. ...{ 106. MessageBox.Show(ex.ToString()); 107. return false; 108. } 109. return true; 110. } 111. #endregion 112. public void GcCollect() 113. ...{ 114. GC.Collect(); 115. GC.WaitForPendingFinalizers(); 116. GC.Collect(); 117. GC.WaitForPendingFinalizers(); 118. }