ftl模板导出word 带多张图片

一.生成ftl文件

        1.创建word 预先放入图片

        2.将word文件另存为.xml文件

        3.将xml文件后缀名改为ftl。

        4.将ftl文件放入编辑器中生成模板代码

主要代码:

        遍历图片

 <#if imageList?? && (imageList?size) &gt; 0>
                            <#list imageList as imageList >
                            <w:r>
                                <w:rPr>
                                    <w:rFonts w:fareast="宋体" w:hint="fareast" />
                                    <w:b />
                                    <w:lang w:fareast="ZH-CN" />
                                </w:rPr>
                                <w:pict>
                                    <w:binData w:name="wordml://${imageList_index}.jpg">${imageList!''}</w:binData>
                                    <v:shape id="_x0000_s1026" o:spt="75" alt="${imageList_index}" type="#_x0000_t75"
                                             style="height:251.85pt;width:308.65pt;" filled="f" o:preferrelative="t"
                                             stroked="f"
                                             coordsize="21600,21600">
                                        <v:path />
                                        <v:fill on="f" focussize="0,0" />
                                        <v:stroke on="f" />
                                        <v:imagedata src="wordml://${imageList_index}.jpg" o:title="" />
                                        <o:lock v:ext="edit" aspectratio="t" />
                                        <w10:wrap type="none" />
                                        <w10:anchorlock />
                                    </v:shape>
                                </w:pict>
                            </w:r>
                           </#list>
                            </#if>

二,方法调用

        1.将图片转为Base64编码

public static String getImagePathStr(String imgFile) {
        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(imgFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            data = new byte[in.available()];
            // 注:FileInputStream.available()方法能够从输入流中阻断由下一个方法调用这个输入流中读取的剩余字节数
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);

    }

        2.将多个图片包装为集合

public static List<String> getAppImageList(String imgURl){
        ArrayList<String> arrayList = new ArrayList<>();
        if(imgURl == null || "".equals(imgURl)){
            return arrayList;
        }
        String[] imgURlList = imgURl.split(",");
        for (String str:imgURlList) {
            if(!"".equals(str)){
                arrayList.add(getImageStr(str));
            }
        }
        return arrayList;
    }

        3.将集合添加到map中 给模板循环用

String enclosure = dto.getEnclosure();
//将图片 放置list集合中在word中遍历展示 候占国
dataMap.put("imageList", BusinessUtils.getAppImageList(enclosure));

        4.下载

WordPrint.downLoad(dataMap, "测试.doc", "测试.ftl", request, response);

        5.下载方法

public static void downLoad(Map dataMap, String fileName, String tempName,
                                HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String outFilePath = createWord(dataMap, fileName, tempName, request);

        File file = new File(outFilePath);

        String realFileName = file.getName();
        realFileName = new String(realFileName.getBytes("GBK"), "ISO-8859-1");

        try {
            InputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.reset();
            response.setContentType("application/file");
            response.setHeader("Content-disposition", "inline;filename=\""
                    + realFileName + "\";");
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(
                    response.getOutputStream());
            toClient.write(buffer);
            toClient.flush();
            toClient.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            file.delete();
        }
    }

示例:

 

希望能帮到你哦!

在使用Java中的Freemarker模板导出Word文件,默认情况下,文件的格式是由文件名后缀决定的。如果你想将其另存为doc格式而不是xml格式,你可以尝试以下方法: 1. 确保文件名的后缀为.doc或.docx。在保存文件,将文件名设置为以.doc或.docx结尾,如"example.doc"或"example.docx"。 2. 确保使用的是正确的输出流。在保存文件,确保你使用正确的输出流类型来保存Word文件。例如,使用FileOutputStream来保存文件。 以下是一个简单的示例代码,展示了如何使用Freemarker模板导出Word文件并将其另存为doc格式: ```java import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import java.io.*; public class WordExport { public static void main(String[] args) { Configuration configuration = new Configuration(Configuration.VERSION_2_3_31); configuration.setDefaultEncoding("UTF-8"); configuration.setClassForTemplateLoading(WordExport.class, "/templates"); try { Template template = configuration.getTemplate("template.ftl"); File outputFile = new File("output.doc"); Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")); template.process(dataModel, writer); writer.close(); } catch (IOException | TemplateException e) { e.printStackTrace(); } } } ``` 在上述代码中,我们将文件名设置为"output.doc",这样保存的就是doc格式的Word文件。 请确保你在代码中适当地替换模板路径、数据模型和文件名,以适应你的具体情况。 希望这能解决你的问题!如果还有其他问题,请随提问。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值