欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java-word导出

程序员文章站 2022-05-17 19:08:42
...

流程:先拿到模板 --》转换成xml——》修改字段——》转换成ftl格式(模板制作完成)——》添加数据下面

package org.jeecg.modules.demo.st.utils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import org.jeecg.modules.demo.st.entity.StAwards;
import org.jeecg.modules.demo.st.entity.StScholarships;
import org.jeecg.modules.demo.st.entity.StStudent;
import org.jeecg.modules.demo.st.service.IStClassService;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysDictService;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class WordUtils {

    @Test
    /* public void test(){*/
    public static void test(List<StStudent> stStudent,
                            ISysDictService sysDictService,
                            IStClassService stClassService,
                            List<StAwards> stAwards,
                            List<StScholarships> stScholarships,
                            ISysDepartService sysDepartService) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {

/*            //编号
            dataMap.put("college", "信息学院");
            //日期
           // dataMap.put("date", new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse("2018-09-19")));
            //附件张数
            dataMap.put("student_id", "123123");
            //受款人
            dataMap.put("st_name", "张三");
            dataMap.put("sex", "男");
            dataMap.put("field_two", "2018-2019");*/

            for (int i = 0; i < stStudent.size(); i++) {
                map.put("college", sysDepartService.queryByIdCollges(stStudent.get(i).getCollege()));
                map.put("student_id", stStudent.get(i).getStudentId());
                map.put("st_name", stStudent.get(i).getStName());
                map.put("sex", sysDictService.queryDictTextByKey("sex", stStudent.get(i).getSex()));
                map.put("face", sysDictService.queryDictTextByKey("face", stStudent.get(i).getFace()));
                map.put("nationality", sysDictService.queryDictTextByKey("nation", stStudent.get(i).getNationality()));
                map.put("st_class", stClassService.queryByIdClas(stStudent.get(i).getStClass()));
                map.put("phone", stStudent.get(i).getPhone());
                map.put("st_card", stStudent.get(i).getStCard());
                map.put("xuezhi", stStudent.get(i).getXuezhi());

   /*             map.put("chushengriqi", null);
                map.put("ruxueshijian", stStudent.get(i).getRuxueshijian());*/


                SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM");
                String time = ft.format(stStudent.get(i).getChushengriqi());
                Date date = new SimpleDateFormat("yyyy-MM").parse(time);
                String now = new SimpleDateFormat("yyyy年MM月").format(date);
                map.put("chushengriqi", now);

                String time1 = ft.format(stStudent.get(i).getRuxueshijian());
                Date date2 = new SimpleDateFormat("yyyy-MM").parse(time1);
                String now2 = new SimpleDateFormat("yyyy年MM月").format(date2);
                map.put("ruxueshijian", now2);
            }
            for (int j = 0; j < stScholarships.size(); j++) {
                map.put("grades_ranking", stScholarships.get(j).getGradesRanking());
                map.put("evaluation", sysDictService.queryDictTextByKey("evaluation", stScholarships.get(j).getEvaluation()));
                map.put("compulsory", stScholarships.get(j).getCompulsory());
                map.put("pass", stScholarships.get(j).getPass());
                map.put("rank", stScholarships.get(j).getRank());
                map.put("application", stScholarships.get(j).getApplication());
                map.put("recommend", stScholarships.get(j).getRecommend());
                map.put("hospital", stScholarships.get(j).getHospital());
                map.put("school", stScholarships.get(j).getSchool());
                map.put("field_two", stScholarships.get(j).getFieldTwo());
            }

            /* List<Map<String, String>> mapList = new ArrayList<>();*/

            if (stAwards.size() == 0) {
                map.put("time0", "");
                map.put("award_name0", "");
                map.put("awarding_unit0", "");
                map.put("time1", "");
                map.put("award_name1", "");
                map.put("awarding_unit1", "");
                map.put("time2", "");
                map.put("award_name2", "");
                map.put("awarding_unit2", "");
                map.put("time3", "");
                map.put("award_name3", "");
                map.put("awarding_unit3", "");
                map.put("time4", "");
                map.put("award_name4", "");
                map.put("awarding_unit4", "");
                map.put("time5", "");
                map.put("award_name5", "");
                map.put("awarding_unit5", "");
            } else if (stAwards.size() == 1) {
                SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM");
                String time2 = ft.format(stAwards.get(0).getTime());
                Date date3 = new SimpleDateFormat("yyyy-MM").parse(time2);
                String now3 = new SimpleDateFormat("yyyy年MM月").format(date3);
                map.put("time0", now3);
                map.put("award_name0", stAwards.get(0).getAwardName());
                map.put("awarding_unit0", stAwards.get(0).getAwardingUnit());
                map.put("time1", "");
                map.put("award_name1", "");
                map.put("awarding_unit1", "");
                map.put("time2", "");
                map.put("award_name2", "");
                map.put("awarding_unit2", "");
                map.put("time3", "");
                map.put("award_name3", "");
                map.put("awarding_unit3", "");
                map.put("time4", "");
                map.put("award_name4", "");
                map.put("awarding_unit4", "");
                map.put("time5", "");
                map.put("award_name5", "");
                map.put("awarding_unit5", "");
            } else if (stAwards.size() == 2) {
                SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM");
                String time1 = ft.format(stAwards.get(0).getTime());
                Date date1 = new SimpleDateFormat("yyyy-MM").parse(time1);
                String now1 = new SimpleDateFormat("yyyy年MM月").format(date1);
                map.put("time0", now1);
                map.put("award_name0", stAwards.get(0).getAwardName());
                map.put("awarding_unit0", stAwards.get(0).getAwardingUnit());
                String time2 = ft.format(stAwards.get(0).getTime());
                Date date2 = new SimpleDateFormat("yyyy-MM").parse(time2);
                String now2 = new SimpleDateFormat("yyyy年MM月").format(date2);
                map.put("time1", now2);
                map.put("award_name1", stAwards.get(1).getAwardingUnit());
                map.put("awarding_unit1", stAwards.get(1).getAwardingUnit());
                map.put("time2", "");
                map.put("award_name2", "");
                map.put("awarding_unit2", "");
                map.put("time3", "");
                map.put("award_name3", "");
                map.put("awarding_unit3", "");
                map.put("time4", "");
                map.put("award_name4", "");
                map.put("awarding_unit4", "");
                map.put("time5", "");
                map.put("award_name5", "");
                map.put("awarding_unit5", "");
            } else if (stAwards.size() == 3) {
                SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM");
                String time1 = ft.format(stAwards.get(0).getTime());
                Date date1 = new SimpleDateFormat("yyyy-MM").parse(time1);
                String now1 = new SimpleDateFormat("yyyy年MM月").format(date1);
                map.put("time0", now1);
                map.put("award_name0", stAwards.get(0).getAwardName());
                map.put("awarding_unit0", stAwards.get(0).getAwardingUnit());

                String time2 = ft.format(stAwards.get(0).getTime());
                Date date2 = new SimpleDateFormat("yyyy-MM").parse(time2);
                String now2 = new SimpleDateFormat("yyyy年MM月").format(date2);
                map.put("time1", now2);
                map.put("award_name1", stAwards.get(1).getAwardingUnit());
                map.put("awarding_unit1", stAwards.get(1).getAwardingUnit());

                String time3 = ft.format(stAwards.get(2).getTime());
                Date date3 = new SimpleDateFormat("yyyy-MM").parse(time3);
                String now3 = new SimpleDateFormat("yyyy年MM月").format(date3);
                map.put("time2", now3);
                map.put("award_name2", stAwards.get(2).getAwardingUnit());
                map.put("awarding_unit2", stAwards.get(2).getAwardingUnit());
                map.put("time3", "");
                map.put("award_name3", "");
                map.put("awarding_unit3", "");
                map.put("time4", "");
                map.put("award_name4", "");
                map.put("awarding_unit4", "");
                map.put("time5", "");
                map.put("award_name5", "");
                map.put("awarding_unit5", "");
            } else if (stAwards.size() == 4) {
                SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM");
                String time1 = ft.format(stAwards.get(0).getTime());
                Date date1 = new SimpleDateFormat("yyyy-MM").parse(time1);
                String now1 = new SimpleDateFormat("yyyy年MM月").format(date1);
                map.put("time0", now1);
                map.put("award_name0", stAwards.get(0).getAwardName());
                map.put("awarding_unit0", stAwards.get(0).getAwardingUnit());

                String time2 = ft.format(stAwards.get(0).getTime());
                Date date2 = new SimpleDateFormat("yyyy-MM").parse(time2);
                String now2 = new SimpleDateFormat("yyyy年MM月").format(date2);
                map.put("time1", now2);
                map.put("award_name1", stAwards.get(1).getAwardingUnit());
                map.put("awarding_unit1", stAwards.get(1).getAwardingUnit());

                String time3 = ft.format(stAwards.get(2).getTime());
                Date date3 = new SimpleDateFormat("yyyy-MM").parse(time3);
                String now3 = new SimpleDateFormat("yyyy年MM月").format(date3);
                map.put("time2", now3);
                map.put("award_name2", stAwards.get(2).getAwardingUnit());
                map.put("awarding_unit2", stAwards.get(2).getAwardingUnit());

                String time4 = ft.format(stAwards.get(3).getTime());
                Date date4 = new SimpleDateFormat("yyyy-MM").parse(time4);
                String now4 = new SimpleDateFormat("yyyy年MM月").format(date4);
                map.put("time3", now4);
                map.put("award_name3", stAwards.get(3).getAwardingUnit());
                map.put("awarding_unit3", stAwards.get(3).getAwardingUnit());


                map.put("time4", "");
                map.put("award_name4", "");
                map.put("awarding_unit4", "");
                map.put("time5", "");
                map.put("award_name5", "");
                map.put("awarding_unit5", "");
            } else if (stAwards.size() == 5) {
                SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM");
                String time1 = ft.format(stAwards.get(0).getTime());
                Date date1 = new SimpleDateFormat("yyyy-MM").parse(time1);
                String now1 = new SimpleDateFormat("yyyy年MM月").format(date1);
                map.put("time0", now1);
                map.put("award_name0", stAwards.get(0).getAwardName());
                map.put("awarding_unit0", stAwards.get(0).getAwardingUnit());

                String time2 = ft.format(stAwards.get(0).getTime());
                Date date2 = new SimpleDateFormat("yyyy-MM").parse(time2);
                String now2 = new SimpleDateFormat("yyyy年MM月").format(date2);
                map.put("time1", now2);
                map.put("award_name1", stAwards.get(1).getAwardingUnit());
                map.put("awarding_unit1", stAwards.get(1).getAwardingUnit());

                String time3 = ft.format(stAwards.get(2).getTime());
                Date date3 = new SimpleDateFormat("yyyy-MM").parse(time3);
                String now3 = new SimpleDateFormat("yyyy年MM月").format(date3);
                map.put("time2", now3);
                map.put("award_name2", stAwards.get(2).getAwardingUnit());
                map.put("awarding_unit2", stAwards.get(2).getAwardingUnit());

                String time4 = ft.format(stAwards.get(3).getTime());
                Date date4 = new SimpleDateFormat("yyyy-MM").parse(time4);
                String now4 = new SimpleDateFormat("yyyy年MM月").format(date4);
                map.put("time3", now4);
                map.put("award_name3", stAwards.get(3).getAwardingUnit());
                map.put("awarding_unit3", stAwards.get(3).getAwardingUnit());

                String time5 = ft.format(stAwards.get(4).getTime());
                Date date5 = new SimpleDateFormat("yyyy-MM").parse(time5);
                String now5 = new SimpleDateFormat("yyyy年MM月").format(date5);
                map.put("time4", now5);
                map.put("award_name4", stAwards.get(4).getAwardingUnit());
                map.put("awarding_unit4", stAwards.get(4).getAwardingUnit());
                map.put("time5", "");
                map.put("award_name5", "");
                map.put("awarding_unit5", "");
            } else if (stAwards.size() == 6) {
                SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM");
                String time1 = ft.format(stAwards.get(0).getTime());
                Date date1 = new SimpleDateFormat("yyyy-MM").parse(time1);
                String now1 = new SimpleDateFormat("yyyy年MM月").format(date1);
                map.put("time0", now1);
                map.put("award_name0", stAwards.get(0).getAwardName());
                map.put("awarding_unit0", stAwards.get(0).getAwardingUnit());

                String time2 = ft.format(stAwards.get(0).getTime());
                Date date2 = new SimpleDateFormat("yyyy-MM").parse(time2);
                String now2 = new SimpleDateFormat("yyyy年MM月").format(date2);
                map.put("time1", now2);
                map.put("award_name1", stAwards.get(1).getAwardingUnit());
                map.put("awarding_unit1", stAwards.get(1).getAwardingUnit());

                String time3 = ft.format(stAwards.get(2).getTime());
                Date date3 = new SimpleDateFormat("yyyy-MM").parse(time3);
                String now3 = new SimpleDateFormat("yyyy年MM月").format(date3);
                map.put("time2", now3);
                map.put("award_name2", stAwards.get(2).getAwardingUnit());
                map.put("awarding_unit2", stAwards.get(2).getAwardingUnit());

                String time4 = ft.format(stAwards.get(3).getTime());
                Date date4 = new SimpleDateFormat("yyyy-MM").parse(time4);
                String now4 = new SimpleDateFormat("yyyy年MM月").format(date4);
                map.put("time3", now4);
                map.put("award_name3", stAwards.get(3).getAwardingUnit());
                map.put("awarding_unit3", stAwards.get(3).getAwardingUnit());

                String time5 = ft.format(stAwards.get(4).getTime());
                Date date5 = new SimpleDateFormat("yyyy-MM").parse(time5);
                String now5 = new SimpleDateFormat("yyyy年MM月").format(date5);
                map.put("time4", now5);
                map.put("award_name4", stAwards.get(4).getAwardingUnit());
                map.put("awarding_unit4", stAwards.get(4).getAwardingUnit());

                String time6 = ft.format(stAwards.get(5).getTime());
                Date date6 = new SimpleDateFormat("yyyy-MM").parse(time6);
                String now6 = new SimpleDateFormat("yyyy年MM月").format(date6);
                map.put("time5", now6);
                map.put("award_name5", stAwards.get(5).getAwardingUnit());
                map.put("awarding_unit5", stAwards.get(5).getAwardingUnit());
            } else {
                SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM");
                String time1 = ft.format(stAwards.get(0).getTime());
                Date date1 = new SimpleDateFormat("yyyy-MM").parse(time1);
                String now1 = new SimpleDateFormat("yyyy年MM月").format(date1);
                map.put("time0", now1);
                map.put("award_name0", stAwards.get(0).getAwardName());
                map.put("awarding_unit0", stAwards.get(0).getAwardingUnit());

                String time2 = ft.format(stAwards.get(0).getTime());
                Date date2 = new SimpleDateFormat("yyyy-MM").parse(time2);
                String now2 = new SimpleDateFormat("yyyy年MM月").format(date2);
                map.put("time1", now2);
                map.put("award_name1", stAwards.get(1).getAwardingUnit());
                map.put("awarding_unit1", stAwards.get(1).getAwardingUnit());

                String time3 = ft.format(stAwards.get(2).getTime());
                Date date3 = new SimpleDateFormat("yyyy-MM").parse(time3);
                String now3 = new SimpleDateFormat("yyyy年MM月").format(date3);
                map.put("time2", now3);
                map.put("award_name2", stAwards.get(2).getAwardingUnit());
                map.put("awarding_unit2", stAwards.get(2).getAwardingUnit());

                String time4 = ft.format(stAwards.get(3).getTime());
                Date date4 = new SimpleDateFormat("yyyy-MM").parse(time4);
                String now4 = new SimpleDateFormat("yyyy年MM月").format(date4);
                map.put("time3", now4);
                map.put("award_name3", stAwards.get(3).getAwardingUnit());
                map.put("awarding_unit3", stAwards.get(3).getAwardingUnit());

                String time5 = ft.format(stAwards.get(4).getTime());
                Date date5 = new SimpleDateFormat("yyyy-MM").parse(time5);
                String now5 = new SimpleDateFormat("yyyy年MM月").format(date5);
                map.put("time4", now5);
                map.put("award_name4", stAwards.get(4).getAwardingUnit());
                map.put("awarding_unit4", stAwards.get(4).getAwardingUnit());

                String time6 = ft.format(stAwards.get(5).getTime());
                Date date6 = new SimpleDateFormat("yyyy-MM").parse(time6);
                String now6 = new SimpleDateFormat("yyyy年MM月").format(date6);
                map.put("time5", now6);
                map.put("award_name5", stAwards.get(5).getAwardingUnit());
                map.put("awarding_unit5", stAwards.get(5).getAwardingUnit());
            }

           /* if (stAwards.size()<1){
                SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM");
                String time2 = ft.format(stAwards.get(0).getTime());
                Date date3 = new SimpleDateFormat("yyyy-MM").parse(time2);
                String now3 = new SimpleDateFormat("yyyy年MM月").format(date3);
                map.put("time0", now3);
                map.put("award_name0","");
                map.put("awarding_unit0","");
            }
            for (int i = 0; i < stAwards.size(); i++) {
                if(stAwards.get(i)!= null){
                    SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM");
                    String time2 = ft.format(stAwards.get(i).getTime());
                    Date date3 = new SimpleDateFormat("yyyy-MM").parse(time2);
                    String now3 = new SimpleDateFormat("yyyy年MM月").format(date3);
                    map.put("time"+i, now3);
                    map.put("award_name"+i, stAwards.get(i).getAwardName());
                    map.put("awarding_unit"+i, stAwards.get(i).getAwardingUnit());
                }
            }*/

/*            //一个人可以有多个stAwards表数据
            List<Map<String, String>> mapList = new ArrayList<>();
            for (int i = 0; i < stAwards.size(); i++) {
                if(stAwards.get(i)!= null){
                    SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM");
                    String time2 = ft.format(stAwards.get(i).getTime());
                    Date date3 = new SimpleDateFormat("yyyy-MM").parse(time2);
                    String now3 = new SimpleDateFormat("yyyy年MM月").format(date3);
                    map.put("time"+i, now3);
                    map.put("award_name"+i, stAwards.get(i).getAwardName());
                    map.put("awarding_unit"+i, stAwards.get(i).getAwardingUnit());
                }
            }
            */

/*                SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM");
                String time2 = ft.format(stAwards.get(1).getTime());
                Date date3 = new SimpleDateFormat("yyyy-MM").parse(time2);
                String now3 = new SimpleDateFormat("yyyy年MM月").format(date3);
                map.put("time", now3);
                map.put("award_name", stAwards.get(1).getAwardName());
                map.put("awarding_unit", stAwards.get(1).getAwardingUnit());*/


/*            List<Map<String, String>> mapList = new ArrayList<>();
            for (int i = 0; i < stAwards.size(); i++) {
                Map<String, String> map2 = new HashMap();

                String time21 = ft.format(stAwards.get(i).getTime());
                Date date31 = new SimpleDateFormat("yyyy-MM").parse(time21);
                String now31 = new SimpleDateFormat("yyyy年MM月").format(date31);
                map2.put("time", now31);
              *//*  map2.put("time", ft.format(stAwards.get(i).getTime());*//*
                map2.put("award_name", stAwards.get(i).getAwardName());
                map2.put("awarding_unit", stAwards.get(i).getAwardingUnit());
                mapList.add(map2);
            }*/

/*            Map<String, Object> o = new HashMap();
            o.put("datemap", map);
            o.put("planList", mapList);  //获奖情况*/

/*            //付款用途
            dataMap.put("use_of_payment", "test");
            //大写金额
            dataMap.put("capitalization_amount", 10);
            //小写金额
            dataMap.put("lowercase_amount", "100");*/
            //Configuration 用于读取ftl文件
            Configuration configuration = new Configuration(new Version("2.3.0"));
            configuration.setDefaultEncoding("utf-8");

            /**
             * 以下是两种指定ftl文件所在目录路径的方式,注意这两种方式都是
             * 指定ftl文件所在目录的路径,而不是ftl文件的路径
             */
            //指定路径的第一种方式(根据某个类的相对路径指定)
//                configuration.setClassForTemplateLoading(this.getClass(), "");

            //指定路径的第二种方式,我的路径是C:/a.ftl
            //configuration.setDirectoryForTemplateLoading(new File("c:/"));
            configuration.setDirectoryForTemplateLoading(new File("C:/Users/fisherman/Desktop/"));

            //输出文档路径及名称
            File outFile = new File("C:/Users/fisherman/Desktop/校长特别奖学金01.doc");

            //以utf-8的编码读取ftl文件
            Template template = configuration.getTemplate("aa.ftl", "utf-8");
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
            template.process(map, out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 @AutoLog(value = "生成学生的校长特别奖学金word")
    @ApiOperation(value = "生成学生的校长特别奖学金word", notes = "生成学生的校长特别奖学金word")
    @GetMapping(value = "/getXZWORD")
    public Result<?> getXZWORD(String stId) {
        System.out.println("===========start=============");

  /*     StStudent student = stStudentService.getStudentId(stId);
        List<StAwards> stAwards = stAwardsService.getStStAwardsId(stId);
        StScholarships stScholarships = stScholarshipsService.getStScholarshipsIds(stId);
      GetStuPdf.getPdf(student, sysDictService, stClassService, stAwards, stScholarships, sysDepartService);*/

        List<StStudent> student = stStudentService.getStudentId(stId);
        List<StAwards> stAwards = stAwardsService.getStStAwardsId(stId);
        List<StScholarships> stScholarships = stScholarshipsService.getStScholarshipsIds(stId);
        WordUtils.test(student, sysDictService, stClassService, stAwards, stScholarships, sysDepartService);
        System.out.println("===========end=============");
        Result result = new Result();
        return result;
    }

java-word导出

相关标签: 项目碎片——java