Method of batch importing Excel data using POI in Java

1、 Definition

Apache POI is an open source function library of the Apache Software Foundation. POI provides APIs for Java programs to read and write files in Microsoft Office format.

2、 Required jar packages:

3、 A simple demo for reading Excel

1. Read file method

2. Testing

3. Raw data

4. Results

4、 Precautions

1. Using polymorphism, excel mainly has Xls terminated (version 2003) and. Xlsx terminated (version 2007) files need to read. XLS files with hssfworkbook objects and. Xlsx files with xssfworkbook objects respectively. Directly using their common parent workbook to initialize objects is conducive to code ease of use.

2. The workbook object (Workbook) can be initialized by flow. The object can be initialized by new xssfworkbook (file absolute path) and new xssfworkbook (input flow), but if we only modify it The suffix of XLS file is Xlsx. In this way, when we use the new xssfworkbook (file absolute path) to read the file, an error will be reported, because it is not a 2007 version of Excel file, and an error will be reported when reading; If we use streaming, we can avoid this situation. Even if you modify the suffix of the file, we can still get the object at the time of initialization Xls type files can be processed with hssfworkbook objects to get correct results.

5、 Enhanced version

It is added to judge whether the header complies with the specification, allowing different positions of header objects. Some decoupling is carried out.

Abnormal conditions:

The following code should be

Replace with:

Resolution: because the previous exception caused the closure of the flow, you need to re create a flow object.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>