示例 excel 表(我使用的是更大的 excel 表):
| 第0列 | 第一列 | 第 2 列 | 第 3 列 | 第 4 列 | 第5列 |
|---|---|---|---|---|---|
| 行0 | 紅色的 | X | X | X | |
| 第 1 行 | 橘子 | X | X | ||
| 行 2 | 黃色 | X | X | X | |
| 第 3 行 | 綠色 | X | X | X | |
| 第 4 行 | 藍色 | X | X | ||
| 第5行 | 紫色的 | X | X | ||
| 第6行 | 粉色的 | X |
讓我們提議我想選擇第 3 行。如何檢索存在 x' 的列的名稱?對于 row3,我想在我的控制臺上顯示“column2 column3 column5”。我也試圖從特定的列索引開始,因此我有一列偏離了 x。因此,對于示例,我需要從 column2-->column5 進行迭代,然后在 row3 中確定哪些單元格具有 x,然后確定這些列的名稱。我下面的代碼有我正在使用的實時數字。
//find x's in certain row
//pick row 15
int rowIndex = 15;
//start at column 9 and iterate to column 33
for (int columnIndex = 9; columnIndex<33; columnIndex ){
Row row = CellUtil.getRow(rowIndex, sheet);
Cell cell = CellUtil.getCell(row, columnIndex);
//if row contains x in specific cells, get column name
//progTypes is the name of the columns appearing in row index 0
if(cell.getStringCellValue() == "x") {
for(int progTypes = 9; progTypes < 33; progTypes ) {
for (int firstRow = 0; firstRow < 0; firstRow ) {
Row fRow = CellUtil.getRow(firstRow, sheet);
Cell fColumn = CellUtil.getCell(fRow, progTypes);
String columnName = fColumn.getStringCellValue();
System.out.println(columnName);
}
}
}
uj5u.com熱心網友回復:
使用Apache POI。https://poi.apache.org/components/spreadsheet/examples.html上有許多如何訪問和修改電子表格的示例
uj5u.com熱心網友回復:
如果我理解正確,您的 if 案例中不需要兩個 for 回圈,因為您已經獲得了單元格索引。
if(cell.getStringCellValue() == "x") {
Row fRow = CellUtil.getRow(0, sheet);
Cell fColumn = CellUtil.getCell(fRow, columnIndex);
String columnName = fColumn.getStringCellValue();
System.out.println(columnName);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/340832.html
上一篇:單核中的多執行緒與異步編程
