Java导出word

原文地址

传入的值不能为null,否则会报错,IXDocReport 有自己的判null规则,比较麻烦,建议代码直接把null替换成""

java 复制代码
   public void exportWord1(WeeklyMeetDataDto dto, HttpServletResponse response) {
        ServletOutputStream downLoadOutput = null;
        try {
            WeeklyMeetDataVo.TextData partOneData = getPartOneData(dto);
            String text = dto.getRqb().split("-")[1] + "月" + dto.getRqb().split("-")[2] + "日~" + dto.getRq().split("-")[1] + "月" + dto.getRq().split("-")[2] + "日," +
                    "全国日均发电量" + partOneData.getQkjfsdl() + "亿千瓦时,同比" + partOneData.getQkjfsdltb() + ";" +
                    "国网经营区日均调度用电量" + partOneData.getDdfsdl_new() + "亿千瓦时,同比" + partOneData.getDdfsdl_newtb() + ";";
            ClassPathResource classPathResource = new ClassPathResource("template/DDZB1.docx");
            InputStream ins = classPathResource.getInputStream();
            //注册xdocreport实例并加载FreeMarker模板引擎
            IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins, TemplateEngineKind.Freemarker);
            //创建xdocreport上下文对象
            IContext context = report.createContext();
            //创建要替换的文本变量
            context.put("text", text);
            List<WeeklyMeetDataVo.EachRegion> eachRegion = getEachRegion(dto);
            List<WeeklyMeetDataVo.PullPower> pullPower = getPullPower(dto);
            List<WeeklyMeetDataVo.DirectPowerPlant> directPowerPlant = getDirectPowerPlant(dto);
            String lxdText = "";
            if (CollectionUtils.isEmpty(pullPower)) {
                lxdText = "所有电网均未发生拉路限电现象。";
            }
            context.put("lxdText", lxdText);
            context.put("ddydlqk", eachRegion);
            context.put("zdfhqk", eachRegion);
            context.put("lxd", pullPower);
            context.put("zd", directPowerPlant);
            //表格
            FieldsMetadata fieldsMetadata = report.createFieldsMetadata();
            fieldsMetadata.load("ddydlqk", WeeklyMeetDataVo.EachRegion.class, true);
            fieldsMetadata.load("zdfhqk", WeeklyMeetDataVo.EachRegion.class, true);
            fieldsMetadata.load("lxd", WeeklyMeetDataVo.PullPower.class, true);
            fieldsMetadata.load("zd", WeeklyMeetDataVo.DirectPowerPlant.class, true);
            //为null会报错
            Map<String, Object> contextMap = context.getContextMap();
            for (Map.Entry<String, Object> map : contextMap.entrySet()) {
                Class<?> aClass = map.getValue().getClass();
                String name = aClass.getName();
                if ("java.util.ArrayList".equals(name)) {
                    Object value = map.getValue();
                    List list = (List) value;
                    for (Object object : list) {
                        Class<?> aClass2 = object.getClass();
                        Field[] declaredFields = aClass2.getDeclaredFields();
                        for (Field field1 : declaredFields) {
                            field1.setAccessible(true);
                            Object o = field1.get(object);
                            if (o == null) {
                                field1.set(object, "");
                            }
                        }
                    }
                } else if (ObjectUtils.isEmpty(map.getValue())) {
                    contextMap.put(map.getKey(), "");
                }
            }
            //替换图片
            fieldsMetadata.addFieldAsImage("img");
            byte[] byteImage = ImageUtils.base64ToFile(dto.getImageBase64Code1());
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteImage);
            context.put("img", inputStream);

            response.setContentType("application/msword");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("调度周报.docx", "utf-8"));
            response.setHeader("Content-Type", "application/octet-stream;charset=utf-8");
            downLoadOutput = response.getOutputStream();
            report.process(context, downLoadOutput);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (downLoadOutput != null) {
                    downLoadOutput.flush();
                    downLoadOutput.close();
                    downLoadOutput = null;
                }
            } catch (IOException e1) {
                e1.printStackTrace();
                System.out.println("Close ServletOutputStream error. type 55");
            }
        }
    }
相关推荐
愚润求学28 分钟前
【Linux】动静态库链接原理
linux·运维·服务器·开发语言·笔记
想躺平的小农36 分钟前
EasyExcel详解
java
慧一居士36 分钟前
EasyExcel集成使用总结与完整示例
java·excel
呦呦彬37 分钟前
【问题排查】easyexcel日志打印Empty row!
java·开发语言·log4j
Tummer83631 小时前
C#+WPF+prism+materialdesign创建工具主界面框架
开发语言·c#·wpf
九章云极AladdinEdu1 小时前
GPU与NPU异构计算任务划分算法研究:基于强化学习的Transformer负载均衡实践
java·开发语言·人工智能·深度学习·测试工具·负载均衡·transformer
佩奇的技术笔记1 小时前
Java学习手册:客户端负载均衡
java·负载均衡
好吃的肘子1 小时前
MongoDB 应用实战
大数据·开发语言·数据库·算法·mongodb·全文检索
ghost1431 小时前
C#学习第23天:面向对象设计模式
开发语言·学习·设计模式·c#
小白学大数据1 小时前
Scrapy框架下地图爬虫的进度监控与优化策略
开发语言·爬虫·python·scrapy·数据分析