1. 程式人生 > >easypoi 操作word和excel

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包,不用一個一個去匯入




}