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");
            }
        }
    }
相关推荐
Monodye6 分钟前
【Java】网络编程:TCP_IP协议详解(IP协议数据报文及如何解决IPv4不够的状况)
java·网络·数据结构·算法·系统架构
一丝晨光12 分钟前
逻辑运算符
java·c++·python·kotlin·c#·c·逻辑运算符
元气代码鼠13 分钟前
C语言程序设计(进阶)
c语言·开发语言·算法
霍霍哈嗨26 分钟前
【QT基础】创建项目&项目代码解释
开发语言·qt
friklogff27 分钟前
【C#生态园】从图像到视觉:Emgu.CV、AForge.NET、OpenCvSharp 全面解析
开发语言·c#·.net
无名指的等待71236 分钟前
SpringBoot中使用ElasticSearch
java·spring boot·后端
Tatakai251 小时前
Mybatis Plus分页查询返回total为0问题
java·spring·bug·mybatis
武子康1 小时前
大数据-133 - ClickHouse 基础概述 全面了解
java·大数据·分布式·clickhouse·flink·spark
.生产的驴1 小时前
SpringBoot 消息队列RabbitMQ 消费者确认机制 失败重试机制
java·spring boot·分布式·后端·rabbitmq·java-rabbitmq
虚拟搬运工1 小时前
Python类及元类的创建流程
开发语言·chrome·python