PageRenderTime 57ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/java/com/sim/alfresco/PoiHelper.java

https://github.com/msalinas/tuNomina
Java | 112 lines | 88 code | 22 blank | 2 comment | 4 complexity | 7c039aaaa69dd5da3cbecba4de119103 MD5 | raw file
  1. package com.sim.alfresco;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.InputStream;
  4. import org.activiti.engine.ActivitiException;
  5. import org.activiti.engine.ProcessEngine;
  6. import org.activiti.engine.ProcessEngines;
  7. import org.apache.chemistry.opencmis.client.api.Document;
  8. import org.apache.poi.ss.usermodel.Cell;
  9. import org.apache.poi.ss.usermodel.Row;
  10. import org.apache.poi.ss.usermodel.Sheet;
  11. import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
  12. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  13. public class PoiHelper {
  14. private InputStream sheetInputStream;
  15. private XSSFWorkbook excelWorkbook;
  16. private Sheet sheet;
  17. private XSSFFormulaEvaluator evaluator;
  18. public void openWorkbook(Document doc) {
  19. sheetInputStream = doc.getContentStream().getStream();
  20. try {
  21. excelWorkbook = new XSSFWorkbook(sheetInputStream);
  22. } catch(Exception e) {
  23. throw new ActivitiException("Error opening Excel workbook", e);
  24. }
  25. sheet = excelWorkbook.getSheetAt(0);
  26. evaluator = new XSSFFormulaEvaluator(excelWorkbook);
  27. }
  28. public void setCellValue(String text, int rowNumber, int cellNumber, boolean create) {
  29. Cell cell = getCell(rowNumber, cellNumber, create);
  30. cell.setCellValue(text);
  31. }
  32. public void setCellValue(Long number, int rowNumber, int cellNumber, boolean create) {
  33. Cell cell = getCell(rowNumber, cellNumber, create);
  34. cell.setCellValue(number);
  35. }
  36. public void evaluateFormulaCell(int rowNumber, int cellNumber) {
  37. evaluator.evaluateFormulaCell(getCell(rowNumber, cellNumber));
  38. }
  39. public boolean getBooleanCellValue(int rowNumber, int cellNumber) {
  40. return getCell(rowNumber, cellNumber).getBooleanCellValue();
  41. }
  42. public String getStringCellValue(int rowNumber, int cellNumber) {
  43. return getCell(rowNumber, cellNumber).getStringCellValue();
  44. }
  45. public void recalculateSheetAfterOpening() {
  46. // no disponible en poi 3.7
  47. //sheet.setForceFormulaRecalculation(true);
  48. }
  49. public Cell getCell(int rowNumber, int cellNumber) {
  50. return getCell(rowNumber, cellNumber, false);
  51. }
  52. private Cell getCell(int rowNumber, int cellNumber, boolean create) {
  53. Row row = sheet.getRow(rowNumber);
  54. Cell cell = null;
  55. if(create) {
  56. cell = row.createCell(cellNumber);
  57. } else {
  58. cell = row.getCell(cellNumber);
  59. }
  60. return cell;
  61. }
  62. public void attachDocumentToProcess(String processInstanceId, Document document, String fileSuffix, String fileDescription) {
  63. ProcessEngine processEngine = ProcessEngines.getProcessEngines().get("creditCheck");
  64. if(processEngine==null){
  65. processEngine = ProcessEngines.getProcessEngines().get("activiti-engine-dev");
  66. }
  67. try{
  68. for(String key:ProcessEngines.getProcessEngines().keySet()){
  69. System.out.println("Key: "+ key +" -> "+ ProcessEngines.getProcessEngines().get(key) );
  70. }
  71. }catch(Exception e){
  72. e.printStackTrace();
  73. }
  74. System.out.println("processEngine: " + processEngine);
  75. processEngine.getTaskService().createAttachment(fileSuffix, null, processInstanceId,
  76. document.getName().substring(0, document.getName().lastIndexOf(".")),
  77. fileDescription, document.getContentStream().getStream());
  78. }
  79. public byte[] getBytes(){
  80. ByteArrayOutputStream customerSheetOutputStream = new ByteArrayOutputStream(1024);
  81. try {
  82. excelWorkbook.write(customerSheetOutputStream);
  83. byte[] content = customerSheetOutputStream.toByteArray();
  84. return content;
  85. }catch(Exception e){
  86. e.printStackTrace();
  87. }
  88. return null;
  89. }
  90. }