SmartSheet.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package net.lab1024.smartadmin.util.excel;
  2. import org.apache.poi.ss.usermodel.Cell;
  3. import org.apache.poi.ss.usermodel.CellType;
  4. import org.apache.poi.ss.usermodel.Row;
  5. import org.apache.poi.ss.util.CellRangeAddress;
  6. /**
  7. * @author zhuoda
  8. */
  9. public class SmartSheet {
  10. final String name;
  11. private final int rowCount;
  12. private final int columnCount;
  13. private final String[][] datas;
  14. public SmartSheet(org.apache.poi.ss.usermodel.Sheet sheet) {
  15. this.name = sheet.getSheetName();
  16. this.rowCount = sheet.getLastRowNum() + 1;
  17. // 初始化基本数据
  18. int maxColumnCount = 0;
  19. this.datas = new String[rowCount][];
  20. for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
  21. Row row = sheet.getRow(rowIndex);
  22. if (row == null) {
  23. continue;
  24. }
  25. int _columnCount = row.getLastCellNum() + 1;
  26. this.datas[rowIndex] = new String[_columnCount];
  27. for (int colIndex = 0; colIndex < _columnCount; colIndex++) {
  28. this.datas[rowIndex][colIndex] = getCellContents(row.getCell(colIndex));
  29. }
  30. if (maxColumnCount < _columnCount) {
  31. maxColumnCount = _columnCount;
  32. }
  33. }
  34. this.columnCount = maxColumnCount;
  35. // 根据单元格合并情况,填充内容
  36. for (int index = 0; index < sheet.getNumMergedRegions(); index++) {
  37. CellRangeAddress mergedRegion = sheet.getMergedRegion(index);
  38. String upperLeftData = this.datas[mergedRegion.getFirstRow()][mergedRegion.getFirstColumn()];
  39. for (int rowIndex = mergedRegion.getFirstRow(); rowIndex <= mergedRegion.getLastRow(); rowIndex++) {
  40. String[] _rowDatas = this.datas[rowIndex];
  41. if (_rowDatas == null) {
  42. this.datas[rowIndex] = new String[mergedRegion.getLastColumn() + 1];
  43. } else if (_rowDatas.length < mergedRegion.getLastColumn() + 1) {
  44. String[] newStrArray = new String[mergedRegion.getLastColumn() + 1];
  45. System.arraycopy(_rowDatas, 0, newStrArray, 0, _rowDatas.length);
  46. this.datas[rowIndex] = newStrArray;
  47. }
  48. for (int colIndex = mergedRegion.getFirstColumn(); colIndex <= mergedRegion.getLastColumn(); colIndex++) {
  49. this.datas[rowIndex][colIndex] = upperLeftData;
  50. }
  51. }
  52. }
  53. }
  54. private String getCellContents(Cell cell) {
  55. if (cell == null) {
  56. return null;
  57. }
  58. return getCellContents(cell.getCellType(), cell);
  59. }
  60. private String getCellContents(CellType type, Cell cell) {
  61. switch (type) {
  62. case BLANK:
  63. return "";
  64. case NUMERIC:
  65. return cell.getStringCellValue();
  66. case STRING:
  67. return cell.getStringCellValue();
  68. case FORMULA:
  69. return getCellContents(cell.getCachedFormulaResultType(), cell);
  70. case BOOLEAN:
  71. return String.valueOf(cell.getBooleanCellValue());
  72. case ERROR:
  73. default:
  74. throw new IllegalArgumentException(String.format("unsupported cell type:%d, col:%d, row:%d, sheet:%s", cell.getCellType(), cell.getColumnIndex(),
  75. cell.getRowIndex(), getName()));
  76. }
  77. }
  78. public int getRowCount() {
  79. return rowCount;
  80. }
  81. public int getColumnCount() {
  82. return columnCount;
  83. }
  84. public String getValue(int rowIndex, int columnIndex) {
  85. if (rowIndex < 0 || rowIndex >= datas.length) {
  86. return "";
  87. }
  88. if (columnIndex < 0 || datas[rowIndex] == null || columnIndex >= datas[rowIndex].length) {
  89. return "";
  90. }
  91. String value = datas[rowIndex][columnIndex];
  92. return value == null ? "": value;
  93. }
  94. public String getName() {
  95. return name;
  96. }
  97. }