首页 > 编程语言 >java 按word模板导出,部分字段为富文本内容

java 按word模板导出,部分字段为富文本内容

时间:2024-09-10 09:16:23浏览次数:1  
标签:为富 java String projectInfo renderData poi put word filterNullValue

1、pom引用依赖包

<dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.9.1</version>
            <exclusions>
            <exclusion>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
            </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>io.github.draco1023</groupId>
            <artifactId>poi-tl-ext</artifactId>
            <version>0.3.3</version>
            <exclusions>
                <exclusion>
                    <groupId>com.deepoove</groupId>
                    <artifactId>poi-tl</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>ooxml-schemas</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.w3c.css</groupId>
            <artifactId>sac</artifactId>
            <version>1.3</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.cssparser</groupId>
            <artifactId>cssparser</artifactId>
            <version>0.9.29</version>
        </dependency>

 

 


2、制作word模板,这里的富文本框就是用普通的文本占位符来写:如 {{basis}}

 


3、示例代码

 

/**
     * DOC文档生成 
     **/
    private String generateProjectDocFile(Long projectId, ProjectsInfoRsp projectInfo)
    {
        // 文件基础路径
        String baseDir = RuoYiConfig.getUploadPath();
        
        String dirPath = baseDir + PROJECT_FILEPATH_PRE;
        File dirFile = new File(dirPath);
        if(!dirFile.exists())
        {
            dirFile.mkdir();
        }
        
        String filePath = baseDir + PROJECT_FILEPATH_PRE + projectInfo.getProjectName() + ".docx";
        String absolutePath = PROJECT_FILEPATH_PRE + projectInfo.getProjectName() + ".docx";
        File docFile = new File(filePath);
        if(docFile.exists())
        {
            docFile.delete();
        }
        
        Map<String, Object> renderData = new HashMap<>();

        renderData.put("companyName", filterNullValue(projectInfo.getCompanyName()));
        renderData.put("companyLegalPerson", filterNullValue(projectInfo.getCompanyLegalPerson()));
        renderData.put("legalPersonPhone", filterNullValue(projectInfo.getLegalPersonPhone()));
        renderData.put("legalPersonFixPhone", filterNullValue(projectInfo.getLegalPersonFixPhone()));
        renderData.put("companyAddress", filterNullValue(projectInfo.getCompanyAddress()));
        renderData.put("bankName", filterNullValue(projectInfo.getBankName()));
        renderData.put("bankAccount", filterNullValue(projectInfo.getBankAccount()));

        
        renderData.put("contentAndGoals", filterHtmlValue(filterNullValue(projectInfo.getProjectDetails().getContentAndGoals())));
        renderData.put("feasibilityAnalysis", filterHtmlValue(filterNullValue(projectInfo.getProjectDetails().getFeasibilityAnalysis())));
        renderData.put("basis", filterHtmlValue(filterNullValue(projectInfo.getProjectDetails().getBasis())));
        renderData.put("demonstrationAndInnovation", filterHtmlValue(filterNullValue(projectInfo.getProjectDetails().getDemonstrationAndInnovation())));
        
        String templatePath = baseDir + "/templates/xmxq.docx";
        
        FileOutputStream fos = null;
        try 
        {
            HtmlRenderPolicy htmlRenderPolicy = new HtmlRenderPolicy();
            // bind 为需要解析为html格式的富文本内容
            Configure configure = Configure.builder()
                    .bind("contentAndGoals", htmlRenderPolicy)
                    .bind("feasibilityAnalysis", htmlRenderPolicy)
                    .bind("basis", htmlRenderPolicy)
                    .bind("demonstrationAndInnovation", htmlRenderPolicy)
                    .build();
            XWPFTemplate template = XWPFTemplate.compile(templatePath, configure).render(renderData);
            template.writeAndClose(new FileOutputStream(filePath));
        } 
        catch (Exception e) 
        {
            filePath = null;
            e.printStackTrace();
        } 
        finally 
        {
            try 
            {
                if (fos != null) 
                {
                    fos.close();
                }
            } 
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        
        if(null != filePath)
        {
            return absolutePath;
        }
        return null;
    }
    
    private String filterHtmlValue(String val)
    {
        if(StringUtils.isEmpty(val))
        {
            return "-";
        }
        return val.replaceAll("src=\"/profile", "src=\"http://192.168.128.77:21281/profile");
    }

 

标签:为富,java,String,projectInfo,renderData,poi,put,word,filterNullValue
From: https://www.cnblogs.com/scode2/p/18405762

相关文章

  • Java Lambda 表达式为何无法抛出检查型异常?——函数式接口的限制解析
    JavaLambda表达式为何无法抛出检查型异常?——函数式接口的限制解析假设场景我们需要将一组Employee对象保存到文件中,这可以通过ObjectOutputStream序列化员工对象实现。我们利用forEach方法遍历员工列表,并调用writeObject()方法序列化数据。然而,writeObject()会抛出......
  • 【Java 分支语句详解 之 If 】
    Java分支语句详解之If-else在编程过程中,我们经常需要根据不同的条件执行不同的代码块,这种流程控制被称为分支语句。在Java中,常见的分支控制结构有if-else和switch。本文将详细介绍if分支结构的使用方法以及相关的代码示例。一、单分支控制语句(if)基本语......
  • Java反射
    Java反射在Java编程世界中,反射(Reflection)是一个强大而复杂的特性,它允许程序在运行时检查或修改其自身结构(如类、接口、字段和方法等)的行为。反射API提供了丰富的功能,使得Java程序能够在编译时不知道具体类型的情况下,动态地创建对象、调用方法、访问和修改字段等。尽管反射......
  • (java+Seleniums3)自动化测试实战
    一.web自动化测试基础密码的加密处理--是在前端JavaScript二.seleniumIDE录制打开火狐浏览器:点击寻找更多附加组件输入:选择:跳转:点击安装完成,打开之后是这个页面:录制一个新的测试用例在一个新的工程当中:点击第一个表示正在录制成功:三.......
  • 深入理解 Java 枚举类型及其定义步骤
    深入理解Java枚举类型及其定义步骤1.枚举概述在Java中,enum(枚举)是用来定义一组固定的常量集合的类型。与普通类不同,枚举类型通过简单而清晰的语法结构,使得代码更具可读性,尤其适用于那些值在编译时就固定的场景,例如星期、方向、季节等。枚举不仅仅是常量的集合,还可以拥有字......
  • 2-6Java抽象类
    Java抽象类在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类。抽象类除了不能实例化对象之外,类的其它功能依然存在,成员变量、成员方法和构造方法的访问方式和......
  • Java 结合vue 和 阿里 写一个短信验证码功能
    要实现一个基于Java、Vue和阿里云的短信验证码功能,需要完成几个步骤。这个功能通常包括前端(Vue.js)和后端(JavaSpringBoot)部分,以及阿里云短信服务的集成。以下是一个大致的实现步骤:前提条件阿里云账户:需要有一个阿里云账户,并开通了短信服务。Java开发环境:确保有Java开发环境和......
  • JavaScript知识点轻量版(一)
                                   【学习重点】1.了解JavaScript基础知识2.熟悉常量和变量3.能够使用表达式和运算符4.正确使用语句5.能够掌握数据类型和转换的基本方法6.正确使用函数,对象,数组等核心知识......
  • 标题:探索 HTML 与 JavaScript 实现的选项卡切换效果
    目录一、HTML结构设计二、JavaScript逻辑处理一、HTML结构设计在给定的HTML代码中,整体结构是创建了多个div元素,每个div元素都包含一个ul(无序列表)和一个div(用于展示内容)。每个ul元素中的li元素代表一个选项卡的标题,而与之对应的div元素中的子div元素则是每个选项卡标......
  • 标题:使用 HTML 和 JavaScript 实现简单的待办事项列表
    目录一、HTML结构设计二、JavaScript逻辑处理一、HTML结构设计整体布局:在HTML部分,整体布局通过一个类名为container的div元素来实现,该元素在页面中水平居中(margin:150pxauto;)。其中包含了一个用于添加事项的输入框和按钮(addBox类),以及一个表格(table元素)用于展......