PageRenderTime 68ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/AnsDesk_Indicadoresv0.2a/src/com/bbva/importans/ThExtraerNotas.java

https://github.com/secundinogarcia/indicadoresv2a
Java | 221 lines | 153 code | 21 blank | 47 comment | 18 complexity | fc178bbff538ce61ebf2b7b09755a6c4 MD5 | raw file
  1. /**
  2. *
  3. */
  4. package com.bbva.importans;
  5. import java.io.FileInputStream;
  6. import java.util.Iterator;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import java.util.TreeSet;
  10. import org.apache.poi.hssf.usermodel.HSSFCell;
  11. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  12. import org.apache.poi.hssf.usermodel.HSSFRow;
  13. import org.apache.poi.hssf.usermodel.HSSFSheet;
  14. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  15. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  16. import org.apache.poi.ss.usermodel.Cell;
  17. import org.apache.poi.ss.usermodel.Row;
  18. import org.apache.poi.ss.util.CellRangeAddress;
  19. import com.bbva.ans.tiers.whitebox;
  20. import advit.logger.ToLog;
  21. import advit.poi.CellRangeAddressWrapper;
  22. /**
  23. * @author Secundino Garcia Jimenez (2013 )
  24. */
  25. public class ThExtraerNotas extends ThProcesos {
  26. private String sEntradas;
  27. private String sPath;
  28. @SuppressWarnings("unused")
  29. private String sArchivoUnido;
  30. public ThExtraerNotas(whitebox wb, String sPath, String sEntradas, String sTareas) throws Exception {
  31. super(wb);
  32. this.sPath = sPath;
  33. this.sEntradas = sEntradas;
  34. }
  35. @SuppressWarnings("unused")
  36. @Override
  37. public void doLoop() {
  38. try {
  39. HSSFSheet sheetTareas = openFileTareas();
  40. HSSFSheet sheetJoined = openJoinedFile();
  41. HSSFRow rowJoined = sheetJoined.createRow((short) 0);
  42. Iterator<Row> row = sheetTareas.rowIterator();
  43. while (row.hasNext()) {
  44. Row r = row.next();
  45. Iterator<Cell> cel = r.cellIterator();
  46. // b.append("Registro ").append(i++).append(":\n");
  47. int column = 0;
  48. while (cel.hasNext()) {
  49. // b.append(" Celda ").append(h++).append(": ");
  50. Cell c = cel.next();
  51. ToLog.info(c.getStringCellValue());
  52. HSSFCell cellJoined = rowJoined.createCell(column++);
  53. }
  54. }
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. ToLog.error(e.getMessage());
  58. }
  59. }
  60. private HSSFSheet openJoinedFile(){
  61. HSSFSheet worksheet =null;
  62. try {
  63. sArchivoUnido = String.format("%s\\ArchivoUnido.xls", sPath);
  64. //FileOutputStream fileOut = new FileOutputStream(sArchivoUnido);
  65. HSSFWorkbook workbook = new HSSFWorkbook();
  66. worksheet = workbook.createSheet("ANS");
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. ToLog.error(e.getMessage());
  70. }
  71. return worksheet;
  72. }
  73. private HSSFSheet openFileTareas() {
  74. String strRutaArchivo = String.format("%s\\%s", sPath, sEntradas);
  75. FileInputStream archivoEntrada;
  76. HSSFSheet hoja = null;
  77. try {
  78. archivoEntrada = new FileInputStream(strRutaArchivo);
  79. POIFSFileSystem poiArchivo = new POIFSFileSystem(archivoEntrada);
  80. HSSFWorkbook libro = new HSSFWorkbook(poiArchivo);
  81. hoja = libro.getSheetAt(0);
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. ToLog.error(e.getMessage());
  85. }
  86. return hoja;
  87. }
  88. /**
  89. * @param srcSheet the sheet to copy.
  90. * @param destSheet the sheet to create.
  91. * @param srcRow the row to copy.
  92. * @param destRow the row to create.
  93. * @param styleMap -
  94. */
  95. public static void copyRow(HSSFSheet srcSheet, HSSFSheet destSheet, HSSFRow srcRow, HSSFRow destRow, Map<Integer, HSSFCellStyle> styleMap) {
  96. // manage a list of merged zone in order to not insert two times a merged zone
  97. Set<CellRangeAddressWrapper> mergedRegions = new TreeSet<CellRangeAddressWrapper>();
  98. destRow.setHeight(srcRow.getHeight());
  99. // reckoning delta rows
  100. int deltaRows = destRow.getRowNum()-srcRow.getRowNum();
  101. // pour chaque row
  102. for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {
  103. HSSFCell oldCell = srcRow.getCell(j); // ancienne cell
  104. HSSFCell newCell = destRow.getCell(j); // new cell
  105. if (oldCell != null) {
  106. if (newCell == null) {
  107. newCell = destRow.createCell(j);
  108. }
  109. // copy chaque cell
  110. copyCell(oldCell, newCell, styleMap);
  111. // copy les informations de fusion entre les cellules
  112. //System.out.println("row num: " + srcRow.getRowNum() + " , col: " + (short)oldCell.getColumnIndex());
  113. CellRangeAddress mergedRegion = getMergedRegion(srcSheet, srcRow.getRowNum(), (short)oldCell.getColumnIndex());
  114. if (mergedRegion != null) {
  115. //System.out.println("Selected merged region: " + mergedRegion.toString());
  116. CellRangeAddress newMergedRegion = new CellRangeAddress(mergedRegion.getFirstRow()+deltaRows, mergedRegion.getLastRow()+deltaRows, mergedRegion.getFirstColumn(), mergedRegion.getLastColumn());
  117. //System.out.println("New merged region: " + newMergedRegion.toString());
  118. CellRangeAddressWrapper wrapper = new CellRangeAddressWrapper(newMergedRegion);
  119. if (isNewMergedRegion(wrapper, mergedRegions)) {
  120. mergedRegions.add(wrapper);
  121. destSheet.addMergedRegion(wrapper.range);
  122. }
  123. }
  124. }
  125. }
  126. }
  127. /**
  128. * @param oldCell
  129. * @param newCell
  130. * @param styleMap
  131. */
  132. public static void copyCell(HSSFCell oldCell, HSSFCell newCell, Map<Integer, HSSFCellStyle> styleMap) {
  133. if(styleMap != null) {
  134. if(oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()){
  135. newCell.setCellStyle(oldCell.getCellStyle());
  136. } else{
  137. int stHashCode = oldCell.getCellStyle().hashCode();
  138. HSSFCellStyle newCellStyle = styleMap.get(stHashCode);
  139. if(newCellStyle == null){
  140. newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
  141. newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
  142. styleMap.put(stHashCode, newCellStyle);
  143. }
  144. newCell.setCellStyle(newCellStyle);
  145. }
  146. }
  147. switch(oldCell.getCellType()) {
  148. case HSSFCell.CELL_TYPE_STRING:
  149. newCell.setCellValue(oldCell.getStringCellValue());
  150. break;
  151. case HSSFCell.CELL_TYPE_NUMERIC:
  152. newCell.setCellValue(oldCell.getNumericCellValue());
  153. break;
  154. case HSSFCell.CELL_TYPE_BLANK:
  155. newCell.setCellType(HSSFCell.CELL_TYPE_BLANK);
  156. break;
  157. case HSSFCell.CELL_TYPE_BOOLEAN:
  158. newCell.setCellValue(oldCell.getBooleanCellValue());
  159. break;
  160. case HSSFCell.CELL_TYPE_ERROR:
  161. newCell.setCellErrorValue(oldCell.getErrorCellValue());
  162. break;
  163. case HSSFCell.CELL_TYPE_FORMULA:
  164. newCell.setCellFormula(oldCell.getCellFormula());
  165. break;
  166. default:
  167. break;
  168. }
  169. }
  170. /**
  171. * Récupčre les informations de fusion des cellules dans la sheet source pour les appliquer
  172. * ŕ la sheet destination...
  173. * Récupčre toutes les zones merged dans la sheet source et regarde pour chacune d'elle si
  174. * elle se trouve dans la current row que nous traitons.
  175. * Si oui, retourne l'objet CellRangeAddress.
  176. *
  177. * @param sheet the sheet containing the data.
  178. * @param rowNum the num of the row to copy.
  179. * @param cellNum the num of the cell to copy.
  180. * @return the CellRangeAddress created.
  181. */
  182. public static CellRangeAddress getMergedRegion(HSSFSheet sheet, int rowNum, short cellNum) {
  183. for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
  184. CellRangeAddress merged = sheet.getMergedRegion(i);
  185. if (merged.isInRange(rowNum, cellNum)) {
  186. return merged;
  187. }
  188. }
  189. return null;
  190. }
  191. /**
  192. * Check that the merged region has been created in the destination sheet.
  193. * @param newMergedRegion the merged region to copy or not in the destination sheet.
  194. * @param mergedRegions the list containing all the merged region.
  195. * @return true if the merged region is already in the list or not.
  196. */
  197. private static boolean isNewMergedRegion(CellRangeAddressWrapper newMergedRegion, Set<CellRangeAddressWrapper> mergedRegions) {
  198. return !mergedRegions.contains(newMergedRegion);
  199. }
  200. }