EasyPoi导出excel

1. 引入依赖:

        <!--EasyPoi导入导出-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.0.3</version>
        </dependency>

2.上代码:

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * easypoi
 *
 * @author zero
 * @review_date 2022-08-19
 */
public class TestEasyExcel {

    public static void main(String[] args) throws Exception {
        // 格式为每行都是 张三========>12========>20220101
        /*
            张三========>12========>20220101
            李四========>22========>20220102
            王五========>32========>20220103
         */
        List<String> txtList = Files.readAllLines(Paths.get("C:\\Users\\Administrator\\Desktop\\1.txt"));
        List<ExcelInfo> list = new ArrayList<>();
        if (txtList.size() <= 0) {
            System.err.println("txt中没有数据,直接退出!!!");
            return;
        }
        for (String line : txtList) {
            if (line != null && !"".equals(line)) {
                line = line.trim();
                if (!"".equals(line)) {
                    String[] tmp1 = line.split("========>");
                    String name = tmp1[0];
                    int age = Integer.parseInt(tmp1[1]);
                    String date = tmp1[2];
                    ExcelInfo excelInfo = new ExcelInfo();
                    excelInfo.setName(name);
                    excelInfo.setAge(age);
                    excelInfo.setDate(date);
                    list.add(excelInfo);
                }
            }
        }
        makeExcel(ExcelInfo.class, list, "C:\\Users\\Administrator\\Desktop\\", "target1.xls");
    }

    private static void makeExcel(Class<?> pojoClass, Collection<?> dataSet, String path, String filename) throws IOException {
        File saveFile = new File(path);
        if (!saveFile.exists()) {
            boolean makeFilesBl = saveFile.mkdirs();
        }
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), pojoClass, dataSet);
        try (FileOutputStream fos = new FileOutputStream(path + filename);) {
            workbook.write(fos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static class ExcelInfo {
        @Excel(name = "name", width = 80)
        private String name;
        @Excel(name = "age", width = 80)
        private int age;
        @Excel(name = "date", width = 80)
        private String date;

        public String getName() {
            return name;
        }

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

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YaseZero90

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值