Java parses excel content according to poi

一.HSSFWorkbook、XSSFWorkbook、SXSSFWorkbook

The differences between HSSFWorkbook, XSSFWorkbook and SXSSFWorkbook in the Apache POI package are as follows:

HSSFWorkbook: It is generally used to operate versions before Excel2003 (including 2003), and the extension is .xls.
XSSFWorkbook: Generally used to operate Excel2007 and above versions, the extension is .xlsx.
SXSSFWorkbook (POI 3.8+ version): generally used for exporting large amounts of data. For example, if the amount of data exceeds 5000, you can consider the
first type of worksheet: HSSFWorkbook

For the EXCEL 2003 version, the extension is .xls. The limitation of this type is that the maximum number of exported rows is 65535 rows. Because the number of exported lines is limited, less than 70,000 lines, it generally does not send memory overflow (OOM)

The second type: XSSFWorkbook

The emergence of this form is due to the limitations of the first HSSF. Because of its small number of exported rows, XSSFWorkbook came into being. It corresponds to EXCEL2007+, the extension is .xlsx, and it can export up to 1.04 million rows, but This is accompanied by a problem - OOM memory overflow. Because the book sheet row cell created by using XSSFWorkbook exists in the memory and is not persisted to the disk, then as the amount of data increases, the demand for memory also increases. Then it is very likely that OOM will appear, so how to solve it?

The third type: SXSSFWorkbook poi.jar 3.8+

SXSSFWorkbook can persistently write data in memory to files according to the number of rows.

In this case, set the maximum number of memory bars, for example, set the maximum memory amount to 5000 lines,

new SXSSFWookbook(5000), when the number of rows reaches 5000, write the memory persistence to the file, so as to write gradually to avoid OOM. This perfectly solves the problem of exporting under big data

Another note: the upper limit of the Excel Sheet export number of HSSFWorkbook (<=2003 version) is 65535 rows and 256 columns,

          XSSFWorkbook's Excel Sheet export limit (>=2007 version) is 1048576 rows, 16384 columns,

         If the amount of data exceeds this upper limit, you can use SXSSFWorkbook to export. In fact, you can consider using SXSSFWorkbook for tens of thousands of pieces of data, or even thousands of pieces of data.

Attached apache poi official website address

Apache POI - Component Overview

2. Upper code

1. POI coordinate dependence

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>

2. POI core API overview

2.1 Create workbook object

Workbook workbook=new XSSFWorkbook(path)

2.2 Get the sheet object in the execl table

Sheet sheet = workbook.getSheetAt(0);

2.3 Obtain the effective number of rows of all physical data in the excel file

int rows = sheet.getPhysicalNumberOfRows()

2.4 Get row object

Row row =sheet.getRow(i)

2.5 Get the column object in the row

Cell cell=row.getCell(0)

2.6 Get the string type data of the column

cell.getStringCellValue()

 2.7 Get the numeric type field data of the column

cell.getNumericCellValue()

  2.8 Convert the content of the cell to string type data, and use it with 3.6

cell.setCellType(CellType.STRING);

 3. Practical code

 public R<String> batchSaveUserInfo(MultipartFile multipartFile){
        R<String> result = new R<>();
        result.setCode(200);
        result.setSuccess(false);

        Workbook workbook = null;
        InputStream inputStream = null;
        try {
            //传入的MultipartFile类型的excel文件
            inputStream = multipartFile.getInputStream();
            String originalFilename = multipartFile.getOriginalFilename();

            //判断是否为2017版本,如果是xlsx,就是XSSFWorkbook,如果是xls,就是HSSFWorkbook
            if (originalFilename.matches("^(?i)(xlsx)$")) {
                 workbook = new XSSFWorkbook(inputStream);
            } else {
                  workbook = new HSSFWorkbook(inputStream);
            }
            //得到excel第一个工作表sheet对象
            Sheet sheet = workbook.getSheetAt(0);
            //得到总行数
            int rowNum = sheet.getPhysicalNumberOfRows();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        result.setMessage("文件解析失败");
        return result;
    }


———————————————
Original link: https://blog.csdn.net/wh445306/article/details/103755283

Using POI technology to import excel files, the pro-test is effective! - Zhihu (zhihu.com)

Java reads Excel files cell.setCellType(); expired - Zhihu (zhihu.com)

おすすめ

転載: blog.csdn.net/weixin_70280523/article/details/131969411