20

Apache POI の使用中に Excel で問題が発生しました。行全体を読み取ることはできますが、特定の列だけを読み取りたい場合があります。

「A」列のみ、または「C」列のみなど、特定の列を読み取ることは可能ですか。

これにはJava言語を使用しています。

4

7 に答える 7

28

heikkim は正しいです。ここに、私が持っているいくつかのコードから適用されたいくつかのサンプル コードがあります。

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
...
for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
  row = sheet.getRow(rowIndex);
  if (row != null) {
    Cell cell = row.getCell(colIndex);
    if (cell != null) {
      // Found column and there is value in the cell.
      cellValueMaybeNull = cell.getStringCellValue();
      // Do something with the cellValueMaybeNull here ...
      // break; ???
    }
  }
}

colCount次のような使用のためにrow.getPhysicalNumberOfCells()

于 2012-10-12T11:21:57.217 に答える
3

わかりました、あなたの質問から、単に特定のコラムを読みたいだけです。したがって、行を反復処理してからそのセルを反復処理するときに、列のインデックスを簡単に確認できます。

Iterator<Row> rowIterator = mySheet.iterator(); // Traversing over each row of XLSX file
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next(); // For each row, iterate through each columns
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                println "column index"+cell.getColumnIndex()//You will have your columns fixed in Excel file
                if(cell.getColumnIndex()==3)//for example of c
                {
                print "done"            
                }
          }
     }     

私はPOI 3.12を使用しています--「org.apache.poi:poi:3.12」乾杯!

于 2015-09-16T17:29:36.260 に答える
2

行をループして、各行から同じセルを読み取ることができます (これは列を構成していませんか?)。

于 2012-10-12T10:56:09.477 に答える
0

Excelデータを列ごとに読み取るコードは次のとおりです。

public ArrayList<String> extractExcelContentByColumnIndex(int columnIndex){
        ArrayList<String> columndata = null;
        try {
            File f = new File("sample.xlsx")
            FileInputStream ios = new FileInputStream(f);
            XSSFWorkbook workbook = new XSSFWorkbook(ios);
            XSSFSheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();
            columndata = new ArrayList<>();

            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();

                    if(row.getRowNum() > 0){ //To filter column headings
                        if(cell.getColumnIndex() == columnIndex){// To match column index
                            switch (cell.getCellType()) {
                            case Cell.CELL_TYPE_NUMERIC:
                                columndata.add(cell.getNumericCellValue()+"");
                                break;
                            case Cell.CELL_TYPE_STRING:
                                columndata.add(cell.getStringCellValue());
                                break;
                            }
                        }
                    }
                }
            }
            ios.close();
            System.out.println(columndata);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return columndata;
    }
于 2014-03-18T07:25:08.677 に答える
0

Iterator<Cell> cellIterator = row.cellIterator();行セル反復子 ( ) を使用して列を反復処理すると、列がサイレントにスキップされる可能性があることに注意してください。そのような振る舞いを暴露している文書に遭遇しました。

for ループでインデックスを使用して繰り返し使用すると、row.getCell(i)列がスキップされず、正しい列インデックスで値が返されていました。

于 2019-03-15T11:52:01.147 に答える