最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot集成easypoi導(dǎo)出word換行處理過程

 更新時(shí)間:2025年08月16日 08:39:06   作者:濤哥是個(gè)大帥比  
Spring?Boot集成Easypoi導(dǎo)出Word時(shí),換行符\n失效顯示為空格,解決方法包括生成段落或替換模板中\(zhòng)n為回車,同時(shí)需確保變量{{temp}}在Map中設(shè)為"??"(帶空格空字符串),避免空指針或殘留變量

項(xiàng)目場景

springboot集成easypoi導(dǎo)出word

<dependency>
	<groupId>cn.afterturn</groupId>
	<artifactId>easypoi-spring-boot-starter</artifactId>
	<version>4.4.0</version>
</dependency>

問題描述

spring boot集成easypoi導(dǎo)出word時(shí),內(nèi)容包含換行符\n,導(dǎo)出word時(shí)換行符失效,會(huì)將換行符\n識(shí)別為空格。

解決方案

第一種:生成段落的方式

示例代碼:

import com.xinghuo.common.base.ActionResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Cleanup;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;

@RestController
@Api(tags = "測試")
@RequestMapping("/test")
public class TestController {

    @ApiOperation("導(dǎo)出Word")
    @GetMapping("/export")
    public ActionResult export() {
        exportWord();
        return ActionResult.success();
    }

    /**
     * 導(dǎo)出Word,支持換行
     */
    public void exportWord(){
        try{
            String content = "第一行\(zhòng)n第二行中文\n"+"第三行";

            @Cleanup XWPFDocument doc = new XWPFDocument();

            if(content != null && content.contains("\n")) {
                //設(shè)置換行
                String[] text = content.split("\n");
                for (int i = 0; i < text.length; i++) {
                    XWPFParagraph p = doc.createParagraph();
                    p.createRun().setText(text[i]);
                }
            }else{
                XWPFParagraph p = doc.createParagraph();
                p.createRun().setText(content);
            }

            String name = "測試換行內(nèi)容.docx";
            String filePath = "F:"+ File.separator + name;
            @Cleanup FileOutputStream output = new FileOutputStream(filePath);
            doc.write(output);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

導(dǎo)出的word內(nèi)容

第二種:替換模板的情況,換行符替換成回車

示例代碼:

import cn.afterturn.easypoi.word.WordExportUtil;
import com.xinghuo.common.base.ActionResult;
import com.xinghuo.common.util.XSSEscape;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Cleanup;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "測試")
@RequestMapping("/test")
public class Test1Controller {

    @ApiOperation("導(dǎo)出Word")
    @GetMapping("/export")
    public ActionResult export() {
        exportWordTemplate();
        return ActionResult.success();
    }

    /**
     * 導(dǎo)出Word替換模板,支持換行
     */
    public void exportWordTemplate(){
        try{
            String content = "第一行\(zhòng)n第二行中文\n"+"第三行";
            Map<String, Object> map = new HashMap<>();
            map.put("content",content);

            @Cleanup XWPFDocument doc = WordExportUtil.exportWord07("F:/export_template.docx", map);
            //文本換行
            addBreakInCell(doc.getParagraphs());

            String name = "測試換行內(nèi)容-替換模板.docx";
            String filePath = "F:"+ File.separator + name;
            @Cleanup FileOutputStream output = new FileOutputStream(XSSEscape.escapePath(filePath));
            doc.write(output);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文本換行
     */
    public static void addBreakInCell(List<XWPFParagraph> paragraphs) {
        for (XWPFParagraph p : paragraphs) {
            for (XWPFRun run : p.getRuns()) {//XWPFRun對象定義具有一組公共屬性的文本區(qū)域
                if(run.getText(0)!= null && run.getText(0).contains("\n")) {
                    String[] lines = run.getText(0).split("\n");
                    if(lines.length > 0) {
                        run.setText(lines[0], 0); // set first line into XWPFRun
                        for(int i=1;i<lines.length;i++){
                            // add break and insert new text
                            run.addBreak();//中斷
//                              run.addCarriageReturn();//回車符,但是不起作用
                            run.setText(lines[i]);
                        }
                    }
                }
            }
        }
    }
}

 其中export_template.docx文件是word模板,內(nèi)容為:

導(dǎo)出的內(nèi)容

導(dǎo)出的本地文件截圖

總結(jié)

 注意:模板中有變量值{{temp}},參數(shù)Map里面對應(yīng)的temp值是null或者"",導(dǎo)出的word就會(huì)變成拋空指針異?;蛘遻{temp}}、其他帶{{ 的形式,直接在Map中設(shè)置temp的值為" ",就可以導(dǎo)出空白到模板,注意是加了空格的字符串" "。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java compiler沒有1.8怎么解決

    java compiler沒有1.8怎么解決

    這篇文章主要介紹了java compiler沒有1.8的解決方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-08-08
  • MyBatis中<collection>標(biāo)簽的多種用法

    MyBatis中<collection>標(biāo)簽的多種用法

    collection標(biāo)簽是處理一對多關(guān)系的關(guān)鍵工具,它能夠?qū)⒉樵兘Y(jié)果巧妙地映射到Java對象的集合屬性中,本文主要介紹了MyBatis中<collection>標(biāo)簽的多種用法,感興趣的可以了解一下
    2025-04-04
  • 一文詳解如何在Java中自定義異常類

    一文詳解如何在Java中自定義異常類

    這篇文章主要介紹了如何在Java中自定義異常類的相關(guān)資料,在Java編程中開發(fā)者可以通過繼承Exception類或其子類創(chuàng)建自定義異常,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-10-10
  • 深入理解SpringBoot的配置環(huán)境屬性

    深入理解SpringBoot的配置環(huán)境屬性

    SpringBoot的配置環(huán)境屬性是一種強(qiáng)大的工具,可以幫助我們配置和管理我們的應(yīng)用程序,這篇文章主要介紹了SpringBoot的配置環(huán)境屬性,需要的朋友可以參考下
    2023-07-07
  • Java向上轉(zhuǎn)型與向下轉(zhuǎn)型超詳細(xì)圖解

    Java向上轉(zhuǎn)型與向下轉(zhuǎn)型超詳細(xì)圖解

    我們在Java編程中經(jīng)常碰到類型轉(zhuǎn)換,對象類型轉(zhuǎn)換主要包括向上轉(zhuǎn)型和向下轉(zhuǎn)型,這篇文章主要介紹了Java向上轉(zhuǎn)型與向下轉(zhuǎn)型的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-04-04
  • Java實(shí)現(xiàn)簡單樹結(jié)構(gòu)

    Java實(shí)現(xiàn)簡單樹結(jié)構(gòu)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡單樹結(jié)構(gòu)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • 劍指Offer之Java算法習(xí)題精講N叉樹的遍歷及數(shù)組與字符串

    劍指Offer之Java算法習(xí)題精講N叉樹的遍歷及數(shù)組與字符串

    跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化
    2022-03-03
  • 基于openeuler的DataGear部署文檔

    基于openeuler的DataGear部署文檔

    本文詳細(xì)介紹了如何在openEuler操作系統(tǒng)上安裝和配置JDK以及DataGear,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2025-03-03
  • Java文件處理之使用itextpdf實(shí)現(xiàn)excel轉(zhuǎn)pdf

    Java文件處理之使用itextpdf實(shí)現(xiàn)excel轉(zhuǎn)pdf

    在文件處理中,經(jīng)常有文件類型轉(zhuǎn)換的使用場景,本文主要介紹了如何使用poi以及itextpdf完成excel轉(zhuǎn)pdf的操作,需要的小伙伴可以參考一下
    2024-02-02
  • 淺談Thread.sleep()為什么要拋出中斷異常

    淺談Thread.sleep()為什么要拋出中斷異常

    本文主要介紹了淺談Thread.sleep()為什么要拋出中斷異常,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04

最新評論

长宁县| 尤溪县| 扶沟县| 会东县| 滕州市| 二手房| 阜康市| 富阳市| 新平| 远安县| 佛学| 清远市| 吐鲁番市| 永登县| 那坡县| 通化市| 郧西县| 邢台市| 文安县| 临朐县| 南澳县| 保靖县| 岑溪市| 卫辉市| 邹城市| 潍坊市| 溧水县| 遂平县| 阜城县| 灵武市| 皮山县| 肥乡县| 梓潼县| 卢氏县| 河津市| 田东县| 古浪县| 深水埗区| 册亨县| 乌拉特中旗| 盱眙县|