Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a single column of excel sheet using java

I have an excel sheet. I want to write a method which takes parameter as column number that to be read and return a array consist of all the data in that column.

And then to place that column element in xml sheet. How can I write a method to do that?

like image 859
Priyanka Thube Avatar asked Sep 14 '26 16:09

Priyanka Thube


1 Answers

Use Apache POI. You can find example in their usage page.

Sheet sheet1 = wb.getSheetAt(0);
for (Row row : sheet1) {
    for (Cell cell : row) {
    // Here you might have to use the cell number to limit what you want.
        CellReference cellRef = new CellReference(row.getRowNum(), cell.getCellNum());
        System.out.print(cellRef.formatAsString());
        System.out.print(" - ");

        switch(cell.getCellType()) {
      case Cell.CELL_TYPE_STRING:
        System.out.println(cell.getRichStringCellValue().getString());
        break;
      case Cell.CELL_TYPE_NUMERIC:
        if(DateUtil.isCellDateFormatted(cell)) {
          System.out.println(cell.getDateCellValue());
        } else {
          System.out.println(cell.getNumericCellValue());
        }
        break;
      case Cell.CELL_TYPE_BOOLEAN:
        System.out.println(cell.getBooleanCellValue());
        break;
      case Cell.CELL_TYPE_FORMULA:
        System.out.println(cell.getCellFormula());
        break;
      default:
        System.out.println();
        }
    }
}
like image 68
uncaught_exceptions Avatar answered Sep 17 '26 04:09

uncaught_exceptions



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!