2014-12-22 64 views
0

我想知道如何使用xls文件的片段並使用代碼片段(單元格,行)的元素進行操作。如何在Apache Poi上使用子表格?

我不用整行工作。

這裏的文件尋找:

enter image description here

我只需要與片斷進行操作,如:

enter image description here

下面是代碼片段與JXL工作:

public class AncillariesWithPoi { 

    private Workbook workbook; 
    private EntryParser shortParser; 
    private EntryParser longParser; 
    private Sheet sheet; 

    private Cell lowShortStart; 
    private Cell lowShortEnd; 
    private Cell highShortStart; 
    private Cell highShortEnd; 
    private Cell lowLongStart; 
    private Cell lowLongEnd; 
    private Cell highLongStart; 
    private Cell highLongEnd; 

    public void setDefault() { 
     setRowKey(new CellReference("C30"), new CellReference("AR30")); 
     setLowSeasonShort("D30", "U40"); 
     setHighSeasonShort("W30", "AN40"); 
     setLowSeasonLong("AR30", "BI42"); 
     setHighSeasonLong("BK30", "CC42"); 
    } 

    public void setRowKey(CellReference firstRef, CellReference lastRef) { 
     shortParser.rowKey = firstRef.getCol(); 
     longParser.rowKey = lastRef.getCol(); 
    } 

    public void setLowSeasonShort(String startCell, String endCell) { 
     this.lowShortStart = sheet.getCell(startCell); 
     this.lowShortEnd = sheet.getCell(endCell); 
    } 

    public void setHighSeasonShort(String startCell, String endCell) { 
     this.highShortStart = sheet.getCell(startCell); 
     this.highShortEnd = sheet.getCell(endCell); 
    // ommited rest of 

如何在apachte poi上使用子表格?

Generall我想知道如何實現下一個方法與apache poi API:

public void setLowSeasonShort(String startCell, String endCell) { 
     this.lowShortStart = sheet.getCell(startCell); 
     this.lowShortEnd = sheet.getCell(endCell); 
    } 

一個posibility,我發現在poi

for (Row row : sheet1) { 
     for (Cell cell : row) { 
      CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex()); 
      System.out.print(cellRef.formatAsString()); 
      System.out.print(" - "); 

      switch (cell.getCellType()) { 
       // work with cell content 
      } 
     } 
    } 

它是細胞迭代從表的每一行。如何將setLowSeasonShort(String startCell, String endCell)改寫爲poi api

有什麼建議嗎?

+2

這是不是很清楚你想達到什麼。代碼本身似乎已經使用POI。您已傳入開始和結束單元格引用。你想迭代該範圍(https://poi.apache.org/apidocs/org/apache/poi/ss/util/CellRangeAddress.html)嗎? –

+0

@GáborBakos是的,並與這個子表一起工作。 –

回答

1

我覺得這樣的事情會爲你工作:

CellRangeAddress range = CellRangeAddress.valueOf(startCell + ":" + endCell); 
for (int row = range.getFirstRow(); row <= range.getLastRow(); ++row) { 
    for (int col = range.getFirstColumn(); col <= range.getLastColumn(); ++col) { 
     Cell cell = sheet.getRow(row).getCell(col); 
     //do something with cell 
    } 
} 

當然,你可以創建一個自定義的結構只訪問這一部分。