2011-05-03 136 views

回答

4

轉置,如交換A2與B1和A3與C1(所以列成行)?

如果是這樣,就沒有內置任何東西,所以你需要自己做一些編碼。你可能想抓住一對單元格,保存一個單元格的內容(值和樣式),將第二個單元格複製到第一個單元格,然後覆蓋第二個單元格。如果您不確定所有閱讀/書寫部分,請參閱quick guide

+0

感謝,但節約的價值和未來將其粘貼回來後,我得到了一個異常:異常在線程「主」 java.lang.IllegalArgumentException異常:表指數(1)超出範圍(0。 .0).....我能做些什麼來解決這個問題? – Yoni 2011-05-05 19:34:16

+0

我的猜測是要麼不使用相同的工作簿,要麼使用的是尚未創建工作表的新工作簿。在填充工作簿之前,您需要在新工作簿中創建工作表! – Gagravarr 2011-05-05 23:15:10

2

我正在尋找相同的答案,不得不自己編碼。我們已經附上我的解決方案很簡單:

  1. 確定各行的每一列
  2. 保存從細胞數量
  3. 確定列的最大數目的使用
  4. 迭代器在每一行,即行/列到一個簡單的列表作爲「CellModel」型
  5. 一旦這樣做,遍歷所有CellModels
  6. 開關列和行索引和CellModel保存到片

我使用的代碼是:

public static void transpose(Workbook wb, int sheetNum, boolean replaceOriginalSheet) { 
    Sheet sheet = wb.getSheetAt(sheetNum); 

    Pair<Integer, Integer> lastRowColumn = getLastRowAndLastColumn(sheet); 
    int lastRow = lastRowColumn.getFirst(); 
    int lastColumn = lastRowColumn.getSecond(); 

    LOG.debug("Sheet {} has {} rows and {} columns, transposing ...", new Object[] {sheet.getSheetName(), 1+lastRow, lastColumn}); 

    List<CellModel> allCells = new ArrayList<CellModel>(); 
    for (int rowNum = 0; rowNum <= lastRow; rowNum++) { 
     Row row = sheet.getRow(rowNum); 
     if (row == null) { 
      continue; 
     } 
     for (int columnNum = 0; columnNum < lastColumn; columnNum++) { 
      Cell cell = row.getCell(columnNum); 
      allCells.add(new CellModel(cell)); 
     } 
    } 
    LOG.debug("Read {} cells ... transposing them", allCells.size()); 

    Sheet tSheet = wb.createSheet(sheet.getSheetName() + "_transposed"); 
    for (CellModel cm : allCells) { 
     if (cm.isBlank()) { 
      continue; 
     } 

     int tRow = cm.getColNum(); 
     int tColumn = cm.getRowNum(); 

     Row row = tSheet.getRow(tRow); 
     if (row == null) { 
      row = tSheet.createRow(tRow); 
     } 

     Cell cell = row.createCell(tColumn); 
     cm.insertInto(cell); 
    } 

    lastRowColumn = getLastRowAndLastColumn(sheet); 
    lastRow = lastRowColumn.getFirst(); 
    lastColumn = lastRowColumn.getSecond(); 
    LOG.debug("Transposing done. {} now has {} rows and {} columns.", new Object[] {tSheet.getSheetName(), 1+lastRow, lastColumn}); 

    if (replaceOriginalSheet) { 
     int pos = wb.getSheetIndex(sheet); 
     wb.removeSheetAt(pos); 
     wb.setSheetOrder(tSheet.getSheetName(), pos); 
    } 

} 

private static Pair<Integer, Integer> getLastRowAndLastColumn(Sheet sheet) { 
    int lastRow = sheet.getLastRowNum(); 
    int lastColumn = 0; 
    for (Row row : sheet) { 
     if (lastColumn < row.getLastCellNum()) { 
      lastColumn = row.getLastCellNum(); 
     } 
    } 
    return new Pair<Integer, Integer>(lastRow, lastColumn); 
} 

由此CellModel一個持單元格包含數據的包裝(你可以添加更多的屬性,如果你喜歡例如,評論,...) :

static class CellModel { 
    private int rowNum = -1; 
    private int colNum = -1; 
    private CellStyle cellStyle; 
    private int cellType = -1; 
    private Object cellValue; 

    public CellModel(Cell cell) { 
     if (cell != null) { 
      this.rowNum = cell.getRowIndex(); 
      this.colNum = cell.getColumnIndex(); 
      this.cellStyle = cell.getCellStyle(); 
      this.cellType = cell.getCellType(); 
      switch (this.cellType) { 
       case Cell.CELL_TYPE_BLANK: 
        break; 
       case Cell.CELL_TYPE_BOOLEAN: 
        cellValue = cell.getBooleanCellValue(); 
        break; 
       case Cell.CELL_TYPE_ERROR: 
        cellValue = cell.getErrorCellValue(); 
        break; 
       case Cell.CELL_TYPE_FORMULA: 
        cellValue = cell.getCellFormula(); 
        break; 
       case Cell.CELL_TYPE_NUMERIC: 
        cellValue = cell.getNumericCellValue(); 
        break; 
       case Cell.CELL_TYPE_STRING: 
        cellValue = cell.getRichStringCellValue(); 
        break; 
      } 
     } 
    } 

    public boolean isBlank() { 
     return this.cellType == -1 && this.rowNum == -1 && this.colNum == -1; 
    } 

    public void insertInto(Cell cell) { 
     if (isBlank()) { 
      return; 
     } 

     cell.setCellStyle(this.cellStyle); 
     cell.setCellType(this.cellType); 
     switch (this.cellType) { 
      case Cell.CELL_TYPE_BLANK: 
       break; 
      case Cell.CELL_TYPE_BOOLEAN: 
       cell.setCellValue((boolean) this.cellValue); 
       break; 
      case Cell.CELL_TYPE_ERROR: 
       cell.setCellErrorValue((byte) this.cellValue); 
       break; 
      case Cell.CELL_TYPE_FORMULA: 
       cell.setCellFormula((String) this.cellValue); 
       break; 
      case Cell.CELL_TYPE_NUMERIC: 
       cell.setCellValue((double) this.cellValue); 
       break; 
      case Cell.CELL_TYPE_STRING: 
       cell.setCellValue((RichTextString) this.cellValue); 
       break; 
     } 
    } 

    public CellStyle getCellStyle() { 
     return cellStyle; 
    } 

    public int getCellType() { 
     return cellType; 
    } 

    public Object getCellValue() { 
     return cellValue; 
    } 

    public int getRowNum() { 
     return rowNum; 
    } 

    public int getColNum() { 
     return colNum; 
    } 

}