PageRenderTime 102ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/net/firstpartners/spreadsheet/RangeConvertor.java

http://red-piranha.googlecode.com/
Java | 140 lines | 70 code | 36 blank | 34 comment | 11 complexity | 10555513650001cb95bfba63e5f95e55 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0
  1. package net.firstpartners.spreadsheet;
  2. import java.io.IOException;
  3. import java.util.Map;
  4. import java.util.logging.Logger;
  5. import org.apache.poi.hssf.usermodel.HSSFCell;
  6. import org.apache.poi.hssf.usermodel.HSSFName;
  7. import org.apache.poi.hssf.usermodel.HSSFRow;
  8. import org.apache.poi.hssf.usermodel.HSSFSheet;
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  10. import org.apache.poi.hssf.util.AreaReference;
  11. import org.apache.poi.hssf.util.CellReference;
  12. /**
  13. * Read Ranges from Excel, Convert to a format that rules can use
  14. *
  15. * Based on Sample from Apache POI
  16. *
  17. * @author paulbrowne
  18. *
  19. */
  20. public class RangeConvertor {
  21. private static final Logger log = Logger.getLogger(RangeConvertor.class.getName());
  22. /**
  23. * Read an excel file and spit out what we find.
  24. *
  25. * @param args
  26. * Expect one argument that is the file to read.
  27. * @throws IOException
  28. * When there is an error processing the file.
  29. */
  30. public static RangeHolder convertExcelToCells(HSSFWorkbook wb)
  31. throws IOException {
  32. RangeHolder returnValues = new RangeHolder();
  33. // retrieve the named range
  34. int numberOfNames = wb.getNumberOfNames();
  35. // Get all the named ranges in our spreadsheet
  36. for (int namedRangeIdx = 0; namedRangeIdx < numberOfNames; namedRangeIdx++) {
  37. HSSFName aNamedRage = wb.getNameAt(namedRangeIdx);
  38. // retrieve the cell at the named range and test its contents
  39. AreaReference aref = new AreaReference(aNamedRage.getReference());
  40. CellReference[] crefs = aref.getAllReferencedCells();
  41. // A Range that we will put the new cells into
  42. Range redRange = new Range(aNamedRage.getNameName());
  43. for (int thisCellinRange = 0; thisCellinRange < crefs.length; thisCellinRange++) {
  44. HSSFSheet sheet = wb.getSheet(crefs[thisCellinRange]
  45. .getSheetName());
  46. HSSFRow r = sheet.getRow(crefs[thisCellinRange].getRow());
  47. HSSFCell thisExcelCell = null;
  48. if (r != null) {
  49. thisExcelCell = r.getCell(crefs[thisCellinRange].getCol());
  50. // extract the cell contents based on cell type etc.
  51. }
  52. // Create our JavaBean representing the cell
  53. String cellHandle = redRange.getUniqueCellName(thisCellinRange);
  54. net.firstpartners.spreadsheet.Cell redCell = CellConvertor
  55. .convertExcelToCell(cellHandle, thisExcelCell);
  56. //Give the cell information about who is holding it
  57. //and that it should pass on property change events to it
  58. redCell.setHoldingRange(redRange);
  59. redCell.addPropertyChangeListener(redRange);
  60. // Add the list of cells to a range
  61. redRange.put(cellHandle, redCell);
  62. }
  63. returnValues.add(redRange);
  64. }
  65. return returnValues;
  66. }
  67. /**
  68. * Update an excel file with our new values
  69. *
  70. */
  71. public static void convertCellsToExcel(HSSFWorkbook wb, RangeHolder updatedValues) throws IOException {
  72. // retrieve the named range
  73. int numberOfNames = wb.getNumberOfNames();
  74. //Get all names of *all* the cells in *all* the ranges
  75. Map<String,Cell> allCells =updatedValues.getAllCells();
  76. for (int namedCellIdx = 0; namedCellIdx < numberOfNames; namedCellIdx++) {
  77. HSSFName aNamedCell = wb.getNameAt(namedCellIdx);
  78. // retrieve the cell at the named range and test its contents
  79. AreaReference aref = new AreaReference(aNamedCell.getReference());
  80. CellReference[] crefs = aref.getAllReferencedCells();
  81. for (int thisCellinRange = 0; thisCellinRange < crefs.length; thisCellinRange++) {
  82. HSSFSheet sheet = wb.getSheet(crefs[thisCellinRange]
  83. .getSheetName());
  84. HSSFRow r = sheet.getRow(crefs[thisCellinRange].getRow());
  85. // Get the cell that is referred to
  86. HSSFCell excelCell = null;
  87. if (r != null) {
  88. excelCell = r.getCell(crefs[thisCellinRange].getCol());
  89. // Check that the range name is on our list
  90. String cellHandle = Range.getUniqueCellName(aNamedCell
  91. .getNameName(), thisCellinRange);
  92. if (allCells.containsKey(cellHandle)) {
  93. CellConvertor.convertCellToExcel(wb, excelCell,
  94. allCells.get(cellHandle));
  95. } else {
  96. log.finest("Name not found in facts:" + cellHandle);
  97. }
  98. } else {
  99. log.info("Null");
  100. }
  101. }
  102. }
  103. }
  104. }