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");
            }
        }
    }
相关推荐
m0_571957581 小时前
Java | Leetcode Java题解之第543题二叉树的直径
java·leetcode·题解
一点媛艺2 小时前
Kotlin函数由易到难
开发语言·python·kotlin
姑苏风2 小时前
《Kotlin实战》-附录
android·开发语言·kotlin
奋斗的小花生3 小时前
c++ 多态性
开发语言·c++
魔道不误砍柴功3 小时前
Java 中如何巧妙应用 Function 让方法复用性更强
java·开发语言·python
NiNg_1_2343 小时前
SpringBoot整合SpringSecurity实现密码加密解密、登录认证退出功能
java·spring boot·后端
闲晨3 小时前
C++ 继承:代码传承的魔法棒,开启奇幻编程之旅
java·c语言·开发语言·c++·经验分享
老猿讲编程4 小时前
一个例子来说明Ada语言的实时性支持
开发语言·ada
Chrikk5 小时前
Go-性能调优实战案例
开发语言·后端·golang
幼儿园老大*5 小时前
Go的环境搭建以及GoLand安装教程
开发语言·经验分享·后端·golang·go