简单实现POI导入导出excel文件

创建maven项目,然后需要导入poi的依赖

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

测试类如下

package com.it;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Hello world!
 *
 */
public class App {
    public static void main( String[] args ) throws IOException {
//        exportExcel();
        importExcel();
    }
    //导出文件
    public static void exportExcel() throws IOException {
        //创建execl工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建一个工作表
        XSSFSheet sheet = workbook.createSheet("信息");
        //创建标题
        XSSFRow row = sheet.createRow(0);//第一行从0开始计数
        String[] title = {"姓名","成绩"};
        //循环将title写入execl的第一行里
        for (int i = 0; i < title.length; i++) {
            XSSFCell cell = row.createCell(i);
            cell.setCellValue(title[i]);
        }
        List<User> list = new ArrayList<>();
        list.add(new User("张三",99));
        list.add(new User("李四",88));
        list.add(new User("王五",77));
        //循环写入第一行之后的员工数据
        for (int i = 0; i < list.size(); i++) {
            XSSFRow row1 = sheet.createRow(i + 1);

            XSSFCell cell1 = row1.createCell(0);
            cell1.setCellValue(list.get(i).getName());
            XSSFCell cell2 = row1.createCell(1);
            cell2.setCellValue(list.get(i).getScore());
        }
        OutputStream out = new FileOutputStream("E://user.xlsx");
        workbook.write(out);
    }
    //导入文件
    public static void importExcel() throws IOException {
        //输入流读取表格文件
        InputStream in = new FileInputStream("E://user.xlsx");
        //创建工作簿
        XSSFWorkbook workbook = new XSSFWorkbook(in);
        //获取第一张工作表
        XSSFSheet sheet = workbook.getSheetAt(0);
        //获取总行数
        int lastRowNum = sheet.getLastRowNum();
        List<User> list = new ArrayList<>();
        //i从1开始-去除表头,循环取值
        for (int i = 1; i < lastRowNum; i++) {
            User user = new User();
            //获取当前行对象
            XSSFRow row = sheet.getRow(i);
            //获取值-然后存值
            XSSFCell cell1 = row.getCell(0);
            String value = cell1.getStringCellValue();
            user.setName(value);

            XSSFCell cell2 = row.getCell(1);
            int value1 = (int)cell2.getNumericCellValue();
            user.setScore(value1);
            list.add(user);

        }
        //遍历输出
        for (User user:list
             ) {
            System.out.println(user);

        }


    }
}

User类:

public class User {
    private String name;
    private Integer score;

    public User() {
    }

    public User(String name, Integer score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
}

ok

 

上一篇:Vue下使用docxtemplater-POI模式导出word


下一篇:POI读取Word内容及格式,看这一篇就够了!