PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/manticore/report/ExcelTools.java

https://bitbucket.org/manticoretrader/etltools
Java | 296 lines | 239 code | 13 blank | 44 comment | 62 complexity | 7585d5862f58929ffd0917b3367eea3f MD5 | raw file
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package com.manticore.report;
  7. import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
  8. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  9. //import org.apache.poi.hssf.usermodel.*;
  10. import org.apache.poi.ss.formula.*;
  11. import org.apache.poi.ss.formula.ptg.AreaPtgBase;
  12. import org.apache.poi.ss.formula.ptg.Ptg;
  13. import org.apache.poi.ss.formula.ptg.RefPtgBase;
  14. import org.apache.poi.ss.usermodel.*;
  15. import org.apache.poi.ss.util.CellRangeAddress;
  16. import org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook;
  17. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  18. /**
  19. *
  20. * @author are
  21. */
  22. public class ExcelTools {
  23. public static void writeDataToSheet(final Workbook workbook, String sheetName, Object[][] data,
  24. FormulaParsingWorkbook parsingWorkbook,
  25. FormulaRenderingWorkbook renderingWorkbook, int rowOffset, int colOffset) {
  26. Sheet sheet = workbook.getSheet(sheetName);
  27. for (int r = 0; r < data.length; r++) {
  28. //if (r > 0) {
  29. shiftRows(workbook, sheet, parsingWorkbook, renderingWorkbook, rowOffset+r);
  30. //}
  31. for (int c = 0; c < data[r].length; c++) {
  32. Row row = sheet.getRow(rowOffset + r);
  33. if (row == null) {
  34. row = sheet.createRow(rowOffset + r);
  35. }
  36. Cell cell = row.getCell(colOffset + c);
  37. if (cell == null) {
  38. cell = row.createCell(colOffset + c);
  39. }
  40. Object value = data[r][c];
  41. if (value instanceof Number) {
  42. cell.setCellValue(((Number) value).doubleValue());
  43. } else if (value instanceof java.util.Date) {
  44. cell.setCellValue((java.util.Date) value);
  45. } else if (value instanceof java.sql.Date) {
  46. java.sql.Date sqlDate = (java.sql.Date) value;
  47. java.util.Date date = new java.util.Date(sqlDate.getTime());
  48. cell.setCellValue(date);
  49. } else if (value instanceof java.sql.Timestamp) {
  50. java.sql.Timestamp timestamp = (java.sql.Timestamp) value;
  51. java.util.Date date = new java.util.Date(timestamp.getTime());
  52. cell.setCellValue(date);
  53. } else {
  54. cell.setCellValue(value != null ? value.toString() : "");
  55. }
  56. }
  57. }
  58. }
  59. public static void shiftRows(Workbook workbook, Sheet worksheet, FormulaParsingWorkbook parsingWorkbook,
  60. FormulaRenderingWorkbook renderingWorkbook, int rowNum) {
  61. for (int r = worksheet.getLastRowNum(); r >= rowNum; r--) {
  62. Row sourceRow = worksheet.getRow(r);
  63. if (sourceRow != null) {
  64. Row newRow = worksheet.createRow(r + 1);
  65. for (int c = 0; c < sourceRow.getLastCellNum(); c++) {
  66. Cell oldCell = sourceRow.getCell(c);
  67. if (oldCell != null) {
  68. Cell newCell = newRow.createCell(c);
  69. newCell.setCellStyle(oldCell.getCellStyle());
  70. newCell.setCellType(oldCell.getCellType());
  71. switch (oldCell.getCellType()) {
  72. case Cell.CELL_TYPE_BLANK:
  73. newCell.setCellValue(oldCell.getStringCellValue());
  74. break;
  75. case Cell.CELL_TYPE_BOOLEAN:
  76. newCell.setCellValue(oldCell.getBooleanCellValue());
  77. break;
  78. case Cell.CELL_TYPE_ERROR:
  79. newCell.setCellErrorValue(oldCell.getErrorCellValue());
  80. break;
  81. case Cell.CELL_TYPE_FORMULA:
  82. String oldFormula = oldCell.getCellFormula();
  83. Ptg[] ptgs
  84. = FormulaParser.parse(oldFormula, parsingWorkbook, FormulaType.CELL,
  85. workbook.getSheetIndex(worksheet));
  86. // iterating through all PTG's
  87. for (Ptg ptg : ptgs) {
  88. if (ptg instanceof RefPtgBase) {
  89. RefPtgBase refPtgBase = (RefPtgBase) ptg;
  90. // if row is relative
  91. if (refPtgBase.isRowRelative()) {
  92. refPtgBase.setRow(
  93. (short) (newCell.getRowIndex() - (oldCell.getRowIndex() - refPtgBase.getRow())));
  94. }
  95. // if col is relative
  96. if (refPtgBase.isColRelative()) {
  97. refPtgBase.setColumn(
  98. (short) (newCell.getColumnIndex() - (oldCell.getColumnIndex() - refPtgBase.getColumn())));
  99. }
  100. }
  101. if (ptg instanceof AreaPtgBase) {
  102. AreaPtgBase areaPtgBase = (AreaPtgBase) ptg;
  103. // if first row is relative
  104. if (areaPtgBase.isFirstRowRelative() && areaPtgBase.getFirstRow() > oldCell.getRowIndex()) {
  105. areaPtgBase.setFirstRow(
  106. (short) (newCell.getRowIndex() - (oldCell.getRowIndex() - areaPtgBase.getFirstRow())));
  107. }
  108. // if last row is relative
  109. if (areaPtgBase.isLastRowRelative()) {
  110. areaPtgBase.setLastRow(
  111. (short) (newCell.getRowIndex() - (oldCell.getRowIndex() - areaPtgBase.getLastRow())));
  112. }
  113. // if first column is relative
  114. if (areaPtgBase.isFirstColRelative()) {
  115. areaPtgBase.setFirstColumn(
  116. (short) (newCell.getColumnIndex() - (oldCell.getColumnIndex() - areaPtgBase.getFirstColumn())));
  117. }
  118. // if last column is relative
  119. if (areaPtgBase.isLastColRelative()) {
  120. areaPtgBase.setLastColumn(
  121. (short) (newCell.getColumnIndex() - (oldCell.getColumnIndex() - areaPtgBase.getLastColumn())));
  122. }
  123. }
  124. }
  125. String newFormula = FormulaRenderer.toFormulaString(renderingWorkbook, ptgs);
  126. newCell.setCellFormula(newFormula);
  127. break;
  128. case Cell.CELL_TYPE_NUMERIC:
  129. newCell.setCellValue(oldCell.getNumericCellValue());
  130. break;
  131. case Cell.CELL_TYPE_STRING:
  132. newCell.setCellValue(oldCell.getRichStringCellValue());
  133. break;
  134. }
  135. }
  136. }
  137. // If there are are any merged regions in the source row, copy to new row
  138. for (int i = 0; i < worksheet.getNumMergedRegions(); i++) {
  139. CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);
  140. if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
  141. CellRangeAddress newCellRangeAddress
  142. = new CellRangeAddress(newRow.getRowNum(),
  143. newRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow()),
  144. cellRangeAddress.getFirstColumn(),
  145. cellRangeAddress.getLastColumn());
  146. worksheet.addMergedRegion(newCellRangeAddress);
  147. }
  148. }
  149. } else {
  150. Row row = worksheet.getRow(r + 1);
  151. if (row!=null) worksheet.removeRow(row);
  152. }
  153. }
  154. }
  155. public static void copyRow(Workbook workbook, Sheet worksheet, int sourceRowNum, int destinationRowNum) {
  156. // Get the source / new row
  157. Row newRow = null; //worksheet.getRow(destinationRowNum);
  158. Row sourceRow = worksheet.getRow(sourceRowNum);
  159. if (sourceRow == null) {
  160. sourceRow = worksheet.createRow(sourceRowNum);
  161. }
  162. // try {
  163. // worksheet.shiftRows(sourceRowNum, sourceRowNum+1, 1);
  164. // } catch (Exception ex) {
  165. // FixFormatReport.logger.log(Level.SEVERE, null, ex);
  166. // }
  167. // If the row exist in destination, push down all rows by 1 else create a new row
  168. newRow = worksheet.getRow(sourceRowNum + 1);
  169. if (newRow == null) {
  170. newRow = worksheet.createRow(sourceRowNum + 1);
  171. }
  172. // Loop through source columns to add to new row
  173. for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
  174. // Grab a copy of the old/new cell
  175. Cell oldCell = sourceRow.getCell(i);
  176. Cell newCell = newRow.createCell(i);
  177. // If the old cell is null jump to next cell
  178. if (oldCell == null) {
  179. newCell = null;
  180. continue;
  181. }
  182. // Copy style from old cell and apply to new cell
  183. //HSSFCellStyle newCellStyle = workbook.createCellStyle();
  184. //newCellStyle.cloneStyleFrom();;
  185. newCell.setCellStyle(oldCell.getCellStyle());
  186. // Set the cell data type
  187. newCell.setCellType(oldCell.getCellType());
  188. // Set the cell data value
  189. switch (oldCell.getCellType()) {
  190. case Cell.CELL_TYPE_BLANK:
  191. newCell.setCellValue(oldCell.getStringCellValue());
  192. break;
  193. case Cell.CELL_TYPE_BOOLEAN:
  194. newCell.setCellValue(oldCell.getBooleanCellValue());
  195. break;
  196. case Cell.CELL_TYPE_ERROR:
  197. newCell.setCellErrorValue(oldCell.getErrorCellValue());
  198. break;
  199. case Cell.CELL_TYPE_FORMULA:
  200. newCell.setCellFormula(getCopyFormula(workbook, worksheet, oldCell, newCell));
  201. break;
  202. case Cell.CELL_TYPE_NUMERIC:
  203. newCell.setCellValue(oldCell.getNumericCellValue());
  204. break;
  205. case Cell.CELL_TYPE_STRING:
  206. newCell.setCellValue(oldCell.getRichStringCellValue());
  207. break;
  208. }
  209. }
  210. // If there are are any merged regions in the source row, copy to new row
  211. for (int i = 0; i < worksheet.getNumMergedRegions(); i++) {
  212. CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);
  213. if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
  214. CellRangeAddress newCellRangeAddress
  215. = new CellRangeAddress(newRow.getRowNum(),
  216. newRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow()),
  217. cellRangeAddress.getFirstColumn(),
  218. cellRangeAddress.getLastColumn());
  219. worksheet.addMergedRegion(newCellRangeAddress);
  220. }
  221. }
  222. }
  223. public static String getCopyFormula(Workbook workbook, Sheet sheet, Cell oldCell,
  224. Cell newCell) {
  225. String oldFormula = oldCell.getCellFormula();
  226. String newFormula = new String();
  227. if (oldFormula != null) {
  228. FormulaParsingWorkbook parsingWorkbook = null;
  229. FormulaRenderingWorkbook renderingWorkbook = null;
  230. if (workbook instanceof HSSFWorkbook) {
  231. parsingWorkbook = HSSFEvaluationWorkbook.create((HSSFWorkbook) workbook);
  232. renderingWorkbook = HSSFEvaluationWorkbook.create((HSSFWorkbook) workbook);
  233. } else if (workbook instanceof XSSFWorkbook) {
  234. parsingWorkbook = XSSFEvaluationWorkbook.create((XSSFWorkbook) workbook);
  235. renderingWorkbook = XSSFEvaluationWorkbook.create((XSSFWorkbook) workbook);
  236. }
  237. // get PTG's in the formula
  238. Ptg[] ptgs
  239. = FormulaParser.parse(oldFormula, parsingWorkbook, FormulaType.CELL, workbook.getSheetIndex(sheet));
  240. // iterating through all PTG's
  241. for (Ptg ptg : ptgs) {
  242. if (ptg instanceof RefPtgBase) {
  243. RefPtgBase refPtgBase = (RefPtgBase) ptg;
  244. // if row is relative
  245. if (refPtgBase.isRowRelative()) {
  246. refPtgBase.setRow(
  247. (short) (newCell.getRowIndex() - (oldCell.getRowIndex() - refPtgBase.getRow())));
  248. }
  249. // if col is relative
  250. if (refPtgBase.isColRelative()) {
  251. refPtgBase.setColumn(
  252. (short) (newCell.getColumnIndex() - (oldCell.getColumnIndex() - refPtgBase.getColumn())));
  253. }
  254. }
  255. if (ptg instanceof AreaPtgBase) {
  256. AreaPtgBase areaPtgBase = (AreaPtgBase) ptg;
  257. // if first row is relative
  258. if (areaPtgBase.isFirstRowRelative()) {
  259. areaPtgBase.setFirstRow(
  260. (short) (newCell.getRowIndex() - (oldCell.getRowIndex() - areaPtgBase.getFirstRow())));
  261. }
  262. // if last row is relative
  263. if (areaPtgBase.isLastRowRelative()) {
  264. areaPtgBase.setLastRow(
  265. (short) (newCell.getRowIndex() - (oldCell.getRowIndex() - areaPtgBase.getLastRow())));
  266. }
  267. // if first column is relative
  268. if (areaPtgBase.isFirstColRelative()) {
  269. areaPtgBase.setFirstColumn(
  270. (short) (newCell.getColumnIndex() - (oldCell.getColumnIndex() - areaPtgBase.getFirstColumn())));
  271. }
  272. // if last column is relative
  273. if (areaPtgBase.isLastColRelative()) {
  274. areaPtgBase.setLastColumn(
  275. (short) (newCell.getColumnIndex() - (oldCell.getColumnIndex() - areaPtgBase.getLastColumn())));
  276. }
  277. }
  278. }
  279. newFormula = FormulaRenderer.toFormulaString(renderingWorkbook, ptgs);
  280. }
  281. return newFormula;
  282. }
  283. }