# 导入导出

操作Excel目前比较流行的就是Apache POI和阿里巴巴的easyExcel

# 介绍

1648272051898.png

# 导入jar

  • poi提供microsoft office旧版本支持,eg,xls,Excel(2003-2007的版本)
  • poi-ooxml提供microsoft office新版本支持,eg,xlsx Excel(2007以后的版本)
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>4.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>4.1.2</version>
		</dependency>
1
2
3
4
5
6
7
8
9
10

# 写excel

public class TestExcel {
    public static void main(String[] args) throws IOException, IllegalAccessException {
        ArrayList<User> dataList = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            dataList.add(new User(i, "warren" + i));
        }

        doExportOneThread(dataList);
    }

    public static void doExportOneThread(ArrayList<User> dataList) throws IOException, IllegalAccessException {
        //创建一个工作蒲
        SXSSFWorkbook workbook = new SXSSFWorkbook();
        //创建一个sheet页
        SXSSFSheet sheet = workbook.createSheet();
        sheet.setRandomAccessWindowSize(-1);

        Iterator<User> iterator = dataList.iterator();
        //通过反射去拿到属性
        Field[] fields = User.class.getDeclaredFields();

        Integer currentRow = 0;
        while (iterator.hasNext()) {
            User user = iterator.next();
            SXSSFRow row = sheet.createRow(currentRow);
            //属性值去导出
            for (int i = 0; i < fields.length; i++) {
                SXSSFCell cell = row.createCell(i);
                if (currentRow == 0) {
                    cell.setCellValue(fields[i].getName());
                } else {
                    cell.setCellValue(String.valueOf(fields[i].get(user)));
                }
            }
            currentRow++;
        }
        FileOutputStream fileOutputStream = new FileOutputStream(new File("/Users/WWei15/Documents/workspace/learn/hello-world/1.xlsx"));
        workbook.write(fileOutputStream);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

# 读excel

public class TestReadPOI {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("/Users/WWei15/Documents/workspace/learn/hello-world/1.xlsx");
        Workbook workbook = new XSSFWorkbook(fis);
        Sheet sheet = workbook.getSheetAt(0);

        int firstRow = 0;
        int lastRow = sheet.getLastRowNum();
        for (int i = firstRow; i <= lastRow; i++) {
            Row row = sheet.getRow(i);
            int lastCol = row.getLastCellNum();
            for (int j = 0; j < lastCol; j++) {
                Cell cell = row.getCell(j);
                String value = cell.getStringCellValue();
                System.out.print(value + "");
            }
            System.out.println();
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 相关资料

Apache POI 官网 (opens new window)

easyExcel (opens new window)

POI和easyExcel (opens new window)

全量分析

评 论:

上次更新时间: 3/26/2022, 1:54:58 PM