我想按程序過濾excel數據。例如用java中的apache poi過濾excel數據
| a b c |
| 1 x x |
| 2 x x |
.............
我要篩選列,其中一個值1和忽略其他, 我有谷歌的Apache POI網站很多,搜索文檔,但沒有效果,有沒有辦法做到這一點?
我想按程序過濾excel數據。例如用java中的apache poi過濾excel數據
| a b c |
| 1 x x |
| 2 x x |
.............
我要篩選列,其中一個值1和忽略其他, 我有谷歌的Apache POI網站很多,搜索文檔,但沒有效果,有沒有辦法做到這一點?
這是一個非常普遍的問題,所以我只能給一個籠統的回答:
我想你應該reading in the file和iterating over the Rows/Cells啓動,那麼你可以query the Cell-Value,並從那裏走。
請參閱指南的其餘部分,地址爲http://poi.apache.org/spreadsheet/quick-guide.html和http://poi.apache.org/spreadsheet/how-to.html,以瞭解可以完成的其他操作。
行和列的索引是從0開始的。因此,如果要掃描列A,則必須使用索引0.
此外,如果要迭代特定列中的單元格,則必須迭代行,然後從每行獲取單元格具體的列號。
由一個給定值過濾單元,以下步驟是必須的:
CELL_TYPE_STRING
/CELL_TYPE_NUMERIC
)針對您的特定問題的示例。代碼迭代第一列(列A)中的所有單元格並打印出相應的座標。
final int firstRowNumber = sheet.getFirstRowNum();
final int lastRowNumber = sheet.getLastRowNum();
// Column A (index = 0)
// final int columnNumber = CellReference.convertColStringToIndex("A")
final int columnNumber = 0;
final int filterValue = 1;
// Iterate rows
for (int rowNumber = firstRowNumber; rowNumber < lastRowNumber; rowNumber++) {
final Row row = sheet.getRow(rowNumber);
if (row != null) {
final Cell cell = row.getCell(columnNumber);
// Filter cells
if (cell != null && cell.getCellType() == Cell.CELL_TYPE_NUMERIC && cell.getNumericCellValue() == filterValue) {
System.out.println("Found cell with value " + filterValue + " at [" + rowNumber + "," + columnNumber + "]");
}
}
}
你可以使用[CellReference.convertColStringToIndex(String)](https://poi.apache.org/apidocs/org/apache/poi/ss/util/CellReference.html#convertColStringToIndex%28java.lang.String%29)轉動列標籤'A'變成'0'使其更清晰 – Gagravarr
@Gagravarr謝謝!順便說一下,是否還有其他方法的功能?從行號和列號生成正確的座標字符串? – winklerrr
是的,只需用[兩個整數](https://poi.apache.org/apidocs/org/apache/poi/ss/util/CellReference.html#CellReference%28int,%20int%29)創建一個CellReference,然後調用[formatAsString](https://poi.apache.org/apidocs/org/apache/poi/ss/util/CellReference.html#formatAsString%28%29)就可以了 – Gagravarr
TKS,但I've看那兒,但它'太一般了,我問這個問題,因爲需要一些例子,最後我做我自己和我一整天費用,無論如何,TKS – Wangbo