easypoi 操作word和excel

package com.example.demo.test;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.util.*;


public class TestExcel {

    public static void main(String[] args) throws   Exception {

        //模拟从数据库获取需要导出的数据
        List<Person> personList = new ArrayList<>();
        Person person1 = new Person("路飞","1",new Date());
        Person person2 = new Person("娜美","2", new Date());
        Person person3 = new Person("索隆","1", new Date());
        Person person4 = new Person("小狸猫","1", new Date());
        personList.add(person1);
        personList.add(person2);
        personList.add(person3);
        personList.add(person4);
        //导出操作

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("计算机一班学生","学生"),
                Person .class, personList);

        FileOutputStream fos = new FileOutputStream("/Users/zhangmuqing/Desktop/test.xlsx");
        workbook.write(fos);
        fos.close();

    }
    @Test
    public void test1() throws   Exception{
        List<StudentEntity> studentList = new ArrayList<>();
        StudentEntity person1 = new StudentEntity("111","张三0",1,new Date(),new Date());
        StudentEntity person2 = new StudentEntity("111","张三2",2,new Date(),new Date());
        StudentEntity person3 = new StudentEntity("111","张三1",1,new Date(),new Date());
        StudentEntity person4 = new StudentEntity("111","张三3",2,new Date(),new Date());

        studentList.add(person1);
        studentList.add(person2);
        studentList.add(person3);
        studentList.add(person4);

        List<CourseEntity> courselist = new ArrayList<>();
        CourseEntity Course1 = new CourseEntity("1","老王0",new TeacherEntity("1","老王0"),studentList);
        CourseEntity Course2 = new CourseEntity("1","老王0",new TeacherEntity("1","老王0"),studentList);
        CourseEntity Course3 = new CourseEntity("1","老王0",new TeacherEntity("1","老王0"),studentList);
        CourseEntity Course4 = new CourseEntity("1","老王0",new TeacherEntity("1","老王0"),studentList);


        courselist.add(Course1);
        courselist.add(Course2);
        courselist.add(Course3);
        courselist.add(Course4);



        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("2412312", "测试", "测试"),
                CourseEntity.class, courselist);


        FileOutputStream fos = new FileOutputStream("/Users/zhangmuqing/Desktop/test4.xlsx");
        workbook.write(fos);
        fos.close();
    }
    @Test
    public void test2() throws Exception{

        TemplateExportParams params = new TemplateExportParams(
                "src/main/resources/upload/test模板.xlsx");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("date", "2014-12-25");
        map.put("money", 2000000.00);
        map.put("upperMoney", "贰佰万");
        map.put("company", "执笔潜行科技有限公司");
        map.put("bureau", "财政局");
        map.put("person", "JueYue");
        map.put("phone", "1879740****");
        List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
        for (int i = 0; i < 4; i++) {
            Map<String, String> lm = new HashMap<String, String>();
            lm.put("id", i + 1 + "");
            lm.put("zijin", i * 10000 + "");
            lm.put("bianma", "A001");
            lm.put("mingcheng", "设计");
            lm.put("xiangmumingcheng", "EasyPoi " + i + "期");
            lm.put("quancheng", "开源项目");
            lm.put("sqje", i * 10000 + "");
            lm.put("hdje", i * 10000 + "");

            listMap.add(lm);
        }
        map.put("maplist", listMap);

        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        File savefile = new File("/Users/zhangmuqing/Desktop/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream("/Users/zhangmuqing/Desktop/专项支出用款申请书_map.xlsx");
        workbook.write(fos);
        fos.close();





    }



<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-spring-boot-starter</artifactId>
    <version>3.3.0</version>
</dependency>

 

springboot中以-spring-boot-starter就会自动导入相关的依赖jar包,不用一个一个去导入



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值