JAVA工程转化为MAVEN ,Apache POI读写EXCEL

1.新建一个JAVA 工程。
2.转化为MAVEN 工程:
JAVA工程转化为MAVEN ,Apache POI读写EXCEL
JAVA工程转化为MAVEN ,Apache POI读写EXCEL
3.然后会看到多了一个POM.XML :
JAVA工程转化为MAVEN ,Apache POI读写EXCEL
然后右键选择:
JAVA工程转化为MAVEN ,Apache POI读写EXCEL
填入一下:
JAVA工程转化为MAVEN ,Apache POI读写EXCEL
发现自动多了一个:
JAVA工程转化为MAVEN ,Apache POI读写EXCEL

再添加:
JAVA工程转化为MAVEN ,Apache POI读写EXCEL
发现poi-ooxml也加进来了:
JAVA工程转化为MAVEN ,Apache POI读写EXCEL
文字版:

<!-- Used to work with the older excel file format - `.xls` -->
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>

<!-- Used to work with the newer excel file format - `.xlsx` -->
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
  1. 增加如下代码:

JAVA工程转化为MAVEN ,Apache POI读写EXCEL

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ApachePOIExcelWrite {

    private static final String FILE_NAME = "/tmp/MyFirstExcel.xlsx";

    public static void main(String[] args) {

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Datatypes in Java");
        Object[][] datatypes = {
                {"Datatype", "Type", "Size(in bytes)"},
                {"int", "Primitive", 2},
                {"float", "Primitive", 4},
                {"double", "Primitive", 8},
                {"char", "Primitive", 1},
                {"String", "Non-Primitive", "No fixed size"}
        };

        int rowNum = 0;
        System.out.println("Creating excel");

        for (Object[] datatype : datatypes) {
            Row row = sheet.createRow(rowNum++);
            int colNum = 0;
            for (Object field : datatype) {
                Cell cell = row.createCell(colNum++);
                if (field instanceof String) {
                    cell.setCellValue((String) field);
                } else if (field instanceof Integer) {
                    cell.setCellValue((Integer) field);
                }
            }
        }

        try {
            FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
            workbook.write(outputStream);
            workbook.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Done");
    }
}
  1. 说明:Basic definitions for Apache POI library
    This section briefly describe about basic classes used during Excel Read and Write.

HSSF is prefixed before the class name to indicate operations related to a Microsoft Excel 2003 file.
XSSF is prefixed before the class name to indicate operations related to a Microsoft Excel 2007 file or later.
XSSFWorkbook and HSSFWorkbook are classes which act as an Excel Workbook
HSSFSheet and XSSFSheet are classes which act as an Excel Worksheet
Row defines an Excel row
Cell defines an Excel cell addressed in reference to a row.

当然啦,JAR包也可以下载:
https://jar-download.com/artifacts/org.apache.poi/poi-ooxml/3.17/source-code