Java生成word[doc格式转docx]

引入依赖


            
                org.freemarker
                freemarker
                2.3.32
            

doc格式转docx格式所需要的依赖


                com.luhuiguo
                aspose-words
                22.4
                pom
            

准备模板

Java生成word[doc格式转docx]_第1张图片

转换为ftl格式

Java生成word[doc格式转docx]_第2张图片

Java生成word[doc格式转docx]_第3张图片

Java生成word[doc格式转docx]_第4张图片

Java生成word[doc格式转docx]_第5张图片

拷贝到idea中,错位自行修改

Java生成word[doc格式转docx]_第6张图片

ftl模板写法示例


                        
                            
                                
                            
                            今天星期:<#if week??>${week}<#else >周一  
                        
                    
                    
                        
                            
                                
                            
                            天气:<#if weather??>${weather}<#else >null
                        
                    
                    
                        
                            
                                
                            
                        
                        
                            
                                
                            
                            心情:<#if mood??>${mood}<#else >null
                        
                    
                    
                        
                            
                            
                            
                        
                        
                            
                            
                            
                        
                        
                            
                                
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                        姓名
                                    
                                
                            
                            
                                
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                        性别
                                    
                                
                            
                            
                                
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                        年龄
                                    
                                
                            
                        

循环表格


                        <#list list![] as val>   
                            
                                
                                    
                                        
                                    
                                    
                                        
                                            
                                                
                                            
                                        
                                        
                                            
                                                
                                            
                                            <#if val.name??>${val.name}<#else >null
                                        
                                    
                                
                                
                                    
                                        
                                    
                                    
                                        
                                            
                                                
                                            
                                        
                                        
                                            
                                                
                                            
                                            <#if val.sex??>${val.sex}<#else >null
                                        
                                    
                                
                                
                                    
                                        
                                    
                                    
                                        
                                            
                                                
                                            
                                        
                                        
                                            
                                                
                                            
                                            <#if val.age??>${val.age}<#else >null
                                        
                                    
                                
                            
                        

java示例

import com.aspose.words.SaveFormat;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import org.junit.Test;
import com.aspose.words.Document;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GenWord {
    @Test
    public void gen() throws Exception {
        Map map = new HashMap<>();
        map.put("week", "周一");
        map.put("weather", "雨夹雪");
        map.put("mood", "开心");
        List> list = new ArrayList<>();
        Map m1 = new HashMap<>();
        m1.put("name", "张三");
        m1.put("sex", "男");
        m1.put("age", "35");
        Map m2 = new HashMap<>();
        m2.put("name", "李四");
        m2.put("sex", "女");
        m2.put("age", "45");
        list.add(m1);
        list.add(m2);
        map.put("list", list);
        Writer writer = null;
        Document document = null;
        FileInputStream fis = null;
        try {
            //对应依赖版本
            Version version = new Version("2.3.32");
            Configuration configuration = new Configuration(version);
            configuration.setTemplateUpdateDelayMilliseconds(0);
            configuration.setDefaultEncoding("utf-8");
            //存放ftl模板的文件夹
            File sourceDir = new File("X:\\IDEA\\my-demo\\src\\main\\resources\\templates");
            configuration.setDirectoryForTemplateLoading(sourceDir);
            //doc格式word
            File targetFile = new File("E:\\word\\测试.doc");
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile),
                    "utf-8"), 1024);
            //ftl模板名称
            Template template = configuration.getTemplate("genWord.ftl", "utf-8");
            template.process(map, writer);
            writer.flush();

            //doc格式转docx
            fis = new FileInputStream(targetFile);
            document = new Document(fis);
            FileOutputStream fos = new FileOutputStream("E:\\word\\测试.docx");
            document.save(fos, SaveFormat.DOCX);
        } finally {
            if (writer != null) writer.close();
            if (document != null) document.cleanup();
            if (fis != null) fis.close();
        }
    }
}

效果展示

Java生成word[doc格式转docx]_第7张图片

Java生成word[doc格式转docx]_第8张图片

完整ftl模板示例




    
        
            
                
                
                
            
        
    
    
        
            
                
                    
                        
                            
                                
                            
                            今天星期:<#if week??>${week}<#else >周一  
                        
                    
                    
                        
                            
                                
                            
                            天气:<#if weather??>${weather}<#else >null
                        
                    
                    
                        
                            
                                
                            
                        
                        
                            
                                
                            
                            心情:<#if mood??>${mood}<#else >null
                        
                    
                    
                        
                            
                            
                            
                        
                        
                            
                            
                            
                        
                        
                            
                                
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                        姓名
                                    
                                
                            
                            
                                
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                        性别
                                    
                                
                            
                            
                                
                                    
                                
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                        年龄
                                    
                                
                            
                        
                        
                        <#list list![] as val>   
                            
                                
                                    
                                        
                                    
                                    
                                        
                                            
                                                
                                            
                                        
                                        
                                            
                                                
                                            
                                            <#if val.name??>${val.name}<#else >null
                                        
                                    
                                
                                
                                    
                                        
                                    
                                    
                                        
                                            
                                                
                                            
                                        
                                        
                                            
                                                
                                            
                                            <#if val.sex??>${val.sex}<#else >null
                                        
                                    
                                
                                
                                    
                                        
                                    
                                    
                                        
                                            
                                                
                                            
                                        
                                        
                                            
                                                
                                            
                                            <#if val.age??>${val.age}<#else >null
                                        
                                    
                                
                            
                        
                    
                    
                        
                            
                                
                            
                        
                    
                    
                        
                        
                        
                        
                    
                
            
        
    
    
        
            
                
                
                
                
                
            
        
    
    
        
            
                
                    
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                        
                    
                    
                        
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                        
                        
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                        
                    
                    
                        
                            
                                
                            
                            
                                
                                    
                                        
                                            
                                            
                                            
                                        
                                    
                                    
                                        
                                            
                                            
                                            
                                        
                                    
                                    
                                        
                                            
                                            
                                            
                                        
                                    
                                
                                
                            
                            
                                
                                    
                                        
                                            
                                            
                                            
                                        
                                    
                                    
                                        
                                            
                                            
                                            
                                        
                                    
                                    
                                        
                                            
                                            
                                            
                                        
                                    
                                
                                
                            
                        
                        
                            
                                
                                    
                                
                                
                                
                            
                            
                                
                                    
                                
                                
                                
                            
                            
                                
                                    
                                
                                
                                
                            
                        
                        
                            
                                
                            
                            
                                
                            
                            
                                
                                    
                                        
                                            
                                        
                                    
                                
                            
                        
                        
                            
                                
                            
                            
                                
                                    
                                    
                                
                            
                            
                                
                                    
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                    
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                    
                                        
                                            
                                            
                                        
                                    
                                
                                
                            
                        
                    
                
                
                
                
                    
                        
                    
                
            
        
    
    
        
            
                
                
                
                
                
                
                
                
                
                
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                
                
                    
                    
                    
                    
                    
                    
                
                
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                
                
                
                
                    
                    
                        
                    
                
                
                
                
                
                
            
        
    
    
        
            
                
                    
                        
                            
                            
                            
                            
                            
                            
                        
                    
                    
                
                
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                
                
                    
                    
                    
                        
                        
                    
                
                
                    
                    
                    
                    
                
                
                    
                    
                    
                    
                    
                        
                        
                            
                            
                            
                            
                        
                    
                
                
                    
                    
                    
                    
                
                
                    
                    
                    
                    
                    
                        
                            
                            
                            
                            
                            
                            
                        
                    
                
            
        
    
    
        
            
                
                
            
        
    
    
        
            
                
                    
                    
                    
                    
                    
                    
                
                
                    
                    
                    
                    
                    
                
                
                    
                    
                    
                    
                    
                
            
        
    
    
        
            
                
                
                猛超 张
                
                
                猛超 张
                2
                2023-12-04T13:35:00Z
                2023-12-04T13:35:00Z
            
        
    
    
        
            
                
                3
                1
                10
                62
                Microsoft Office Word
                0
                1
                1
                false
                
                false
                71
                false
                false
                16.0000
            
        
    

你可能感兴趣的:(java,word)