Java导出word

2023-12-13 23:14:30

原文地址

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

   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");
            }
        }
    }

文章来源:https://blog.csdn.net/lqz1993/article/details/134815505
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。