国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

SpringBoot整合FreeMarker生成word表格文件(使用FTL模板)

這篇具有很好參考價值的文章主要介紹了SpringBoot整合FreeMarker生成word表格文件(使用FTL模板)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

一,什么是FreeMarker,F(xiàn)TL模板?
FreeMarker 是一款 模板引擎: 即一種基于模板和要改變的數(shù)據(jù), 并用來生成輸出文本(HTML網(wǎng)頁,電子郵件,配置文件,源代碼等)的通用工具。 它不是面向最終用戶的,而是一個Java類庫,是一款程序員可以嵌入他們所開發(fā)產(chǎn)品的組件。

模板編寫為FreeMarker Template Language (FTL)。它是簡單的,專用的語言, 不是 像PHP那樣成熟的編程語言。 那就意味著要準備數(shù)據(jù)在真實編程語言中來顯示,比如數(shù)據(jù)庫查詢和業(yè)務運算, 之后模板顯示已經(jīng)準備好的數(shù)據(jù)。在模板中,你可以專注于如何展現(xiàn)數(shù)據(jù), 而在模板之外可以專注于要展示什么數(shù)據(jù)。
freemarker ftl模板,SpringBoot專欄,word,spring boot,java
二,生成FTL模板文件
freemarker ftl模板,SpringBoot專欄,word,spring boot,java

  1. 創(chuàng)建一個word文件,按照要要導出的數(shù)據(jù)進行頁面排版和布局,動態(tài)的信息可以設置為先設置為{xxx}這種格式
  2. 把文件另存為xml文件,然后把里面動態(tài)信息{xxx}前面加上$ , 變?yōu)?${xxx}。
  3. 把文件重命名為 .ftl 結尾的文件。
  4. 把文件放入resources/templates 目錄下

三,引入freemarker依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

四,工具類

package cn.iocoder.yudao.module.tjl.util.word;


import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Map;

/**
 * @ClassName: ExportWord
 * @Description: 導出word工具類
 * @Authror: XQD
 * @Date: 2023/6/16 15:59
 */
public class ExportWord {

    private Configuration configuration;
    private String encoding;
    private String exportPath = "D:\\data";

    /**
     * 構造函數(shù)
     * 配置模板路徑
     * @param encoding
     */
    public ExportWord(String encoding) {
        this.encoding = encoding;
        configuration = new Configuration();
        configuration.setDefaultEncoding(encoding);
        configuration.setClassForTemplateLoading(this.getClass(), "/templates");
    }

    /**
     * 導出word文檔到客戶端
     * @param response
     * @param fileName
     * @param tplName
     * @param data
     * @throws Exception
     */
    public void exportDoc(HttpServletResponse response, String fileName, String tplName, Map<String, Object> data, FreeMarkerConfigurer freeMarkerConfigurer) throws Exception {
        response.reset();
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=" +  URLEncoder.encode(fileName , "UTF-8"));
        // 把本地文件發(fā)送給客戶端
        Writer writer = response.getWriter();
        Template template = getTemplate(tplName, freeMarkerConfigurer);
        template.process(data, writer);
        writer.close();
    }

    /**
     * 導出word文檔到指定目錄
     * @param fileName
     * @param tplName
     * @param data
     * @throws Exception
     */
    public void exportDocFile(String fileName, String tplName, Map<String, Object> data) throws Exception {
        //如果目錄不存在,則創(chuàng)建目錄
        File exportDirs = new File(exportPath);
        if (!exportDirs.exists()) {
            exportDirs.mkdirs();
        }
        Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportPath + fileName), encoding));
        getTemplate(tplName).process(data, writer);
    }

    /**
     * 獲取模板 打成jar包后獲取不到模板的方式 freeMarkerConfigurer
     * @param name
     * @return
     * @throws Exception
     */
    public Template getTemplate(String name, FreeMarkerConfigurer freeMarkerConfigurer) throws Exception {
        freemarker.template.Configuration configuration = freeMarkerConfigurer.getConfiguration();
        freemarker.template.Template template = configuration.getTemplate(name);
        return template;
    }

    /**
     * 獲取模板
     * @param name
     * @return
     * @throws Exception
     */
    public Template getTemplate(String name) throws Exception {
        return configuration.getTemplate(name);
    }
}

五,實例演示

    @Autowired
    FreeMarkerConfigurer freeMarkerConfigurer;

 /**
     * 導出word
     */
    @Override
    public void exportNewValvePressure1(HttpServletResponse response) throws Exception {
        // TODO 獲取數(shù)據(jù)源,查詢需要動態(tài)加入的數(shù)據(jù)
       
        // 添加表單的抬頭信息
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String today = format.format(new Date());
        String fileName = today + ".doc";
        Map<String, Object> dataMap = new HashMap<>();
        // 這里的key要與FTL模板中設置的${xxx}的值對應。
        dataMap.put("name", "尼古拉斯");
        dataMap.put("sex", "男");
        dataMap.put("dizhi", "宇宙的盡頭");
        dataMap.put("phone", "1666666666");
        new ExportWord("UTF-8").exportDoc(response, fileName, "xinxi.ftl", dataMap, freeMarkerConfigurer);
    }

六,效果圖
freemarker ftl模板,SpringBoot專欄,word,spring boot,java
補充:如果導出到表格的內(nèi)容需要換行,可已在內(nèi)容中加入 “<w:br/>”文章來源地址http://www.zghlxwxcb.cn/news/detail-842969.html

到了這里,關于SpringBoot整合FreeMarker生成word表格文件(使用FTL模板)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 根據(jù)模板動態(tài)生成word(一)使用freemarker生成word

    根據(jù)模板動態(tài)生成word(一)使用freemarker生成word

    @ 目錄 一、準備模板 1、創(chuàng)建模板文件 2、處理模板 2.1 處理普通文本 2.2 處理表格 2.3 處理圖片 二、項目代碼 1、引入依賴 2、生成代碼 三、驗證生成word 首先先建立一個word文件,輸入模板內(nèi)容freemaker的內(nèi)容,下面是本次演示的word文件。 然后將word文件另存為 .xml 文件,然后再

    2024年02月13日
    瀏覽(30)
  • freemarker模板生成的word文檔優(yōu)化

    freemarker模板生成的word文檔優(yōu)化

    使用壓縮工具打開docx模板,取出document.xml,如下圖: 將用壓縮工具打開后的docx文檔里面的document.xml復制出來,并將document.xml后綴改為.ftl,然后進行參數(shù)預設。 將內(nèi)容格式化后修改需要替換的內(nèi)容為freemarker標簽,對document.ftl進行參數(shù)預設,如下圖: 文件準備好后存放到某個

    2024年02月02日
    瀏覽(28)
  • freemarker學習+集成springboot+導出word

    freemarker學習+集成springboot+導出word

    目錄 一 FreeMarker簡介 二 集成springboot,實現(xiàn)案例導出 三 常見面試題總結 FreeMarker ?是一款 模板引擎: 即一種基于模板和要改變的數(shù)據(jù), 并用來生成輸出文本(HTML網(wǎng)頁,電子郵件,配置文件,源代碼等)的通用工具。 是一個Java類庫。 在本地磁盤隨便準備一個文件,內(nèi)容體如下

    2024年02月10日
    瀏覽(31)
  • 基于Java+freemarker實現(xiàn)動態(tài)賦值以及生成Word文檔

    基于Java+freemarker實現(xiàn)動態(tài)賦值以及生成Word文檔

    有一個需求就是給定一個正確格式的 Word 文檔模板,要求通過動態(tài)賦值方式,寫入數(shù)據(jù)并新生成 該模板格式的 Word 文檔。這很明顯使用 Java+freemarker 方式來實現(xiàn)頗為簡單。 (1)準備好一個正確格式的 Word 文檔(測試文檔 - 原版.docx) (2)將其另存為xml文件(測試文檔?- 原版

    2024年02月09日
    瀏覽(25)
  • JAVA利用Freemarker模版動態(tài)生成并導出word文檔(全網(wǎng)最詳細)

    JAVA利用Freemarker模版動態(tài)生成并導出word文檔(全網(wǎng)最詳細)

    公司的某個需求,需要根據(jù)接口的信息生成一份word接口文檔信息并支持導出功能。以前沒做過這種需求,于是搜羅各種資料,最終發(fā)現(xiàn)java利用freemarker模版可以實現(xiàn)這個功能。 1、需要的環(huán)境 2、創(chuàng)建模板 1)展示word文檔如下所示: 2)將word文檔動態(tài)的參數(shù)替換成占位符,如下

    2024年02月16日
    瀏覽(23)
  • vue 使用docx庫生成word表格文檔

    ????????在Vue.js中生成Word表格文檔,可以通過前端庫來實現(xiàn)。這些庫可以幫助我們輕松地將HTML表格轉換為Word文檔(通常是.docx格式)。以下是一些流行的前端庫,它們可以用于在Vue項目中生成Word表格文檔: ???????? ????????docx是一個流行的JavaScript庫,用于在瀏覽

    2024年02月21日
    瀏覽(20)
  • 使用POI生成word文檔的table表格

    使用POI生成word文檔的table表格

    //生成一行一列的table XWPFTable table = document.createTable(); //添加列 table.getRow(0).addNewTableCell(); //添加行(添加的新行默認就是總共的列數(shù)) table.createRow(); 測試Demo:CreateTableDemo1.java 生成結果: //生成3行5列的table XWPFTable table2 = document.createTable(3, 5); 測試Demo: 生成結果: 創(chuàng)建的兩

    2024年01月25日
    瀏覽(21)
  • freemarker 使用word模板賦值

    freemarker 使用word模板賦值

    這里貼上自己測試的ftl文件 生成的效果 參考: https://blog.csdn.net/weixin_46174854/article/details/116855252 https://blog.csdn.net/weixin_45853881/article/details/129298494 https://blog.csdn.net/qq_42851623/article/details/122879852 https://blog.csdn.net/weixin_45103378/article/details/118395284 https://www.cnblogs.com/ayueC/p/15118381.html

    2024年02月11日
    瀏覽(20)
  • 使用freemarker,數(shù)據(jù)導出word并下載

    1.1 項目背景 最近在開發(fā)一個項目,需要導出一些數(shù)據(jù),然后寫入到word文檔中,然后再導出到本地,這個需求是比較常見的,但是我在網(wǎng)上找了很多資料,都沒有找到一個比較好的解決方案,所以就自己寫了一個,這里分享給大家,希望能幫助到大家。 項目中使用的技術棧:

    2024年02月05日
    瀏覽(21)
  • Freemarker:生成HTML文本文件

    Freemarker:生成HTML文本文件

    前置工作參考:?Freemarker:基本使用_moreCalm的博客-CSDN博客 ? 1、修改application.yml配置文件 2、在test下創(chuàng)建測試類?FreemarkerTest 3、查看結果 ?

    2024年02月14日
    瀏覽(25)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包