You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.8 KiB

  1. using NPOI.HSSF.UserModel;
  2. using NPOI.SS.UserModel;
  3. using NPOI.XSSF.UserModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Text;
  8. namespace Shentun.Utilities
  9. {
  10. public class NPOIExcelHelper : IDisposable
  11. {
  12. private IWorkbook _workbook;
  13. private ISheet _sheet;
  14. private bool _disposed;
  15. private FileStream _fileStream = null;
  16. public void CreateWorkbook()
  17. {
  18. if (_workbook != null)
  19. {
  20. throw new Exception("已存在一个工作簿,禁止创建");
  21. }
  22. _workbook = new HSSFWorkbook();
  23. }
  24. public void OpenWorkbook(string fileName)
  25. {
  26. if (_workbook != null)
  27. {
  28. throw new Exception("已存在一个工作簿,禁止打开");
  29. }
  30. if (!File.Exists(fileName))
  31. {
  32. throw new Exception(fileName + "不存在,不能打开");
  33. }
  34. _fileStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite);
  35. if (fileName.IndexOf(".xlsx") > 0) // 2007版本
  36. _workbook = new XSSFWorkbook(_fileStream);
  37. else if (fileName.IndexOf(".xls") > 0) // 2003版本
  38. _workbook = new HSSFWorkbook(_fileStream);
  39. else
  40. throw new Exception("Excel文件后缀名必须为xls或xlsx");
  41. }
  42. public void SelectSheet(string sheetName)
  43. {
  44. _sheet = _workbook.GetSheet(sheetName);
  45. if (_sheet == null)
  46. throw new Exception(sheetName + "不存在");
  47. }
  48. public void SelectFirstSheet()
  49. {
  50. if (_workbook.NumberOfSheets == 0)
  51. {
  52. AddSheet("Sheet1");
  53. }
  54. _sheet = _workbook.GetSheetAt(0);
  55. }
  56. public void AddSheet(string sheetName)
  57. {
  58. _sheet = _workbook.CreateSheet(sheetName);
  59. }
  60. public ICell GetCell(int row, int column)
  61. {
  62. IRow sheetRow = (IRow)_sheet.GetRow(row);
  63. if (sheetRow == null)
  64. {
  65. sheetRow = (IRow)_sheet.CreateRow(row);
  66. }
  67. ICell cell = sheetRow.GetCell(column);
  68. if (cell == null)
  69. cell = sheetRow.CreateCell(column);
  70. return cell;
  71. }
  72. public void SetCellValue(int row, int column, string value)
  73. {
  74. ICell cell = GetCell(row, column);
  75. cell.SetCellValue(value);
  76. }
  77. public void SetCellValue(int row, int column, Double value)
  78. {
  79. ICell cell = GetCell(row, column);
  80. cell.SetCellValue(value);
  81. }
  82. public void SetCellValue(int row, int column, DateTime value)
  83. {
  84. ICell cell = GetCell(row, column);
  85. cell.SetCellValue(value);
  86. }
  87. public void SetCellValue(int row, int column, bool value)
  88. {
  89. ICell cell = GetCell(row, column);
  90. cell.SetCellValue(value);
  91. }
  92. public void SetCellValue(int row, int column, IRichTextString value)
  93. {
  94. ICell cell = GetCell(row, column);
  95. cell.SetCellValue(value);
  96. }
  97. public void InsertDataTable(System.Data.DataTable dt)
  98. {
  99. for (int i = 0; i <= dt.Columns.Count - 1; i++)
  100. {
  101. GetCell(0, i).SetCellValue(dt.Columns[i].ColumnName);
  102. }
  103. for (int i = 0; i <= dt.Rows.Count - 1; i++)
  104. {
  105. for (int j = 0; j <= dt.Columns.Count - 1; j++)
  106. {
  107. ICell cell = GetCell(i + 1, j);
  108. cell.SetCellValue(dt.Rows[i][j].ToString());
  109. }
  110. }
  111. }
  112. public void Save()
  113. {
  114. if (_fileStream == null)
  115. {
  116. throw new Exception("_fileStream不能为空");
  117. }
  118. _workbook.Write(_fileStream);
  119. _fileStream.Close();
  120. _workbook.Close();
  121. }
  122. public void Save(string fileName)
  123. {
  124. FileStream fileStream = new FileStream(fileName, FileMode.Create);
  125. try
  126. {
  127. _workbook.Write(fileStream);
  128. }
  129. finally
  130. {
  131. fileStream.Close();
  132. _workbook.Close();
  133. }
  134. }
  135. public void Dispose()
  136. {
  137. Dispose(true);
  138. GC.SuppressFinalize(this);
  139. }
  140. protected virtual void Dispose(bool disposing)
  141. {
  142. if (!this._disposed)
  143. {
  144. if (disposing)
  145. {
  146. if (_fileStream != null)
  147. _fileStream.Close();
  148. }
  149. _fileStream = null;
  150. _disposed = true;
  151. }
  152. }
  153. }
  154. }