Ejemplo n.º 1
0
  private void processCell(Element tr, XSSFCell cell) {

    int num = cell.getSheet().getNumMergedRegions();

    //		System.out.println(cell.getCTCell());
    for (int i = 0; i < num; i++) {

      CellRangeAddress c = cell.getSheet().getMergedRegion(i);

      System.out.println(c.getFirstColumn());
      ;
      System.out.println(c.getLastColumn());
      System.out.println(c.getFirstRow());
      System.out.println(c.getLastRow());
      System.out.println();
      System.out.println(cell.getRowIndex());
      System.out.println(cell.getColumnIndex());

      System.out.println("\n\n\n");

      //		System.out.println(cra);

    }

    //		System.exit(0);

    Element td = htmlDocumentFacade.createTableCell();
    Object value;
    switch (cell.getCellType()) {
      case Cell.CELL_TYPE_BLANK:
        value = "\u00a0";
        break;
      case Cell.CELL_TYPE_NUMERIC:
        value = cell.getNumericCellValue();
        break;
      case Cell.CELL_TYPE_BOOLEAN:
        value = cell.getBooleanCellValue();
        break;
      case Cell.CELL_TYPE_FORMULA:
        value = cell.getNumericCellValue();
        break;
      default:
        value = cell.getRichStringCellValue();
        break;
    }
    if (value instanceof XSSFRichTextString) {
      processCellStyle(td, cell.getCellStyle(), (XSSFRichTextString) value);
      td.setTextContent(value.toString());
    } else {
      processCellStyle(td, cell.getCellStyle(), null);
      td.setTextContent(value.toString());
    }
    //		System.err.println(value);
    tr.appendChild(td);
  }
  @Override
  public void close() {
    try {
      int nbS = wb.getNumberOfSheets();
      for (int i = 0; i < nbS; i++) {
        XSSFSheet s = wb.getSheetAt(i);
        for (int j = 0; j < s.getRow(0).getLastCellNum(); j++) {
          XSSFCell c = s.getRow(0).getCell(j);
          c.getSheet().autoSizeColumn(c.getColumnIndex());
        }

        XSSFCell firstCell = s.getRow(0).getCell(0);
        XSSFCell lastCell = s.getRow(0).getCell((int) s.getRow(0).getLastCellNum() - 1);
        s.setAutoFilter(
            new CellRangeAddress(
                firstCell.getRowIndex(),
                lastCell.getRowIndex(),
                lastCell.getRowIndex(),
                lastCell.getColumnIndex()));
      }
      wb.write(fileOut);
      fileOut.flush();
      fileOut.close();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
 /**
  * @param oldCell
  * @param newCell
  * @param styleMap
  */
 public static void copyCell(
     XSSFCell oldCell, XSSFCell newCell, Map<Integer, XSSFCellStyle> styleMap) {
   if (styleMap != null) {
     if (oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()) {
       newCell.setCellStyle(oldCell.getCellStyle());
     } else {
       int stHashCode = oldCell.getCellStyle().hashCode();
       XSSFCellStyle newCellStyle = styleMap.get(stHashCode);
       if (newCellStyle == null) {
         newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
         newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
         styleMap.put(stHashCode, newCellStyle);
       }
       newCell.setCellStyle(newCellStyle);
     }
   }
   switch (oldCell.getCellType()) {
     case HSSFCell.CELL_TYPE_STRING:
       newCell.setCellValue(oldCell.getStringCellValue());
       break;
     case HSSFCell.CELL_TYPE_NUMERIC:
       newCell.setCellValue(oldCell.getNumericCellValue());
       break;
     case HSSFCell.CELL_TYPE_BLANK:
       newCell.setCellType(HSSFCell.CELL_TYPE_BLANK);
       break;
     case HSSFCell.CELL_TYPE_BOOLEAN:
       newCell.setCellValue(oldCell.getBooleanCellValue());
       break;
     case HSSFCell.CELL_TYPE_ERROR:
       newCell.setCellErrorValue(oldCell.getErrorCellValue());
       break;
     case HSSFCell.CELL_TYPE_FORMULA:
       newCell.setCellFormula(oldCell.getCellFormula());
       break;
     default:
       break;
   }
 }