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");
            }
        }
    }
相关推荐
LucianaiB3 分钟前
掌握 Rust:从内存安全到高性能服务的完整技术图谱
开发语言·安全·rust
m0_748240257 分钟前
C++ 游戏开发示例:简单的贪吃蛇游戏
开发语言·c++·游戏
QMY5205209 分钟前
爬虫的意义
java·spring·tomcat·maven
兰亭妙微44 分钟前
2026年UX/UI五大趋势:AI、AR与包容性设计将重新定义用户体验
开发语言·ui·1024程序员节·界面设计·设计趋势
lang201509281 小时前
Spring Boot Actuator深度解析与实战
java·spring boot·后端
懒羊羊不懒@1 小时前
Java—枚举类
java·开发语言·1024程序员节
zerolala1 小时前
Java容器常用方法
java·1024程序员节
m0_748240251 小时前
C++智能指针使用指南(auto_ptr, unique_ptr, shared_ptr, weak_ptr)
java·开发语言·c++
lang201509281 小时前
Spring注解配置全解析
java·后端·spring
Evand J1 小时前
【MATLAB例程】自适应渐消卡尔曼滤波,背景为二维雷达目标跟踪,基于扩展卡尔曼(EKF)|附完整代码的下载链接
开发语言·matlab·目标跟踪·1024程序员节