java读取xlsx转成array

pom文件

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.17</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.17</version>
    </dependency>
  </dependencies>

读取xlsx

package TJU;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.IOException;

public class ExcelReader {
    public static final String SAMPLE_XLSX_FILE_PATH = "/Users/student/Desktop/LUSCvalueT.xlsx";

    public static void main(String[] args) throws IOException, InvalidFormatException {

        // Creating a Workbook from an Excel file (.xls or .xlsx)
        Workbook workbook = WorkbookFactory.create(new File(SAMPLE_XLSX_FILE_PATH));

        // Retrieving the number of sheets in the Workbook
        //System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");

        /*
           =============================================================
           Iterating over all the sheets in the workbook (Multiple ways)
           =============================================================
        */

        // 1. You can obtain a sheetIterator and iterate over it
        //Iterator<Sheet> sheetIterator = workbook.sheetIterator();
        //System.out.println("Retrieving Sheets using Iterator");
        //while (sheetIterator.hasNext()) {
        //  Sheet sheet = sheetIterator.next();
        //  System.out.println("=> " + sheet.getSheetName());
        //}

        // 2. Or you can use a for-each loop
        //System.out.println("Retrieving Sheets using for-each loop");
        //for(Sheet sheet: workbook) {
        //  System.out.println("=> " + sheet.getSheetName());
        //}

        // 3. Or you can use a Java 8 forEach with lambda
        //System.out.println("Retrieving Sheets using Java 8 forEach with lambda");
        //workbook.forEach(sheet -> {
        //  System.out.println("=> " + sheet.getSheetName());
        //});

        /*
           ==================================================================
           Iterating over all the rows and columns in a Sheet (Multiple ways)
           ==================================================================
        */

        // Getting the Sheet at index zero
        Sheet sheet = workbook.getSheetAt(0);

        // Create a DataFormatter to format and get each cell's value as String
        //DataFormatter dataFormatter = new DataFormatter();

        // 1. You can obtain a rowIterator and columnIterator and iterate over them
        //System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
        //Iterator<Row> rowIterator = sheet.rowIterator();
        //while (rowIterator.hasNext()) {
        //  Row row = rowIterator.next();
        //
        //  // Now let's iterate over the columns of the current row
        //  Iterator<Cell> cellIterator = row.cellIterator();
        //
        //  while (cellIterator.hasNext()) {
        //      Cell cell = cellIterator.next();
        //      String cellValue = dataFormatter.formatCellValue(cell);
        //      System.out.print(cellValue + "\t");
        //  }
        //  System.out.println();
        //}

        // 2. Or you can use a for-each loop to iterate over the rows and columns
        //System.out.println("\n\nIterating over Rows and Columns using for-each loop\n");
        //for (Row row: sheet) {
        //  for(Cell cell: row) {
        //      String cellValue = dataFormatter.formatCellValue(cell);
        //      System.out.print(cellValue + "\t");
        //  }
        //  System.out.println();
        //}

        // 3. Or you can use Java 8 forEach loop with lambda
        //System.out.println("\n\nIterating over Rows and Columns using Java 8 forEach with lambda\n");
        //sheet.forEach(row -> {
        //  row.forEach(cell -> {
        //      String cellValue = dataFormatter.formatCellValue(cell);
        //      System.out.print(cellValue + "\t");
        //  });
        //  System.out.println();
        //});

        sheet.forEach(row -> {
            row.forEach(cell -> {
                printCellValue(cell);
            });
            System.out.println();
        });
        // Closing the workbook
        workbook.close();

    }

    private static void printCellValue(Cell cell) {
        switch (cell.getCellTypeEnum()) {
            case BOOLEAN:
                System.out.print(cell.getBooleanCellValue());
                break;
            case STRING:
                System.out.print(cell.getRichStringCellValue().getString());
                break;
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    System.out.print(cell.getDateCellValue());
                } else {
                    System.out.print(cell.getNumericCellValue());
                }
                break;
            case FORMULA:
                System.out.print(cell.getCellFormula());
                break;
            case BLANK:
                System.out.print("");
                break;
            default:
                System.out.print("");
        }

        System.out.print("\t");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值