springboot集成easypoi導(dǎo)出word換行處理過程
項(xiàng)目場(chǎ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 = "測(cè)試")
@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 = "測(cè)試換行內(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 = "測(cè)試")
@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 = "測(cè)試換行內(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對(duì)象定義具有一組公共屬性的文本區(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里面對(duì)應(yīng)的temp值是null或者"",導(dǎo)出的word就會(huì)變成拋空指針異?;蛘遻{temp}}、其他帶{{ 的形式,直接在Map中設(shè)置temp的值為" ",就可以導(dǎo)出空白到模板,注意是加了空格的字符串" "。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot整合EasyPoi實(shí)現(xiàn)復(fù)雜多級(jí)表頭Excel導(dǎo)出的完整方案
- SpringBoot集成EasyPoi實(shí)現(xiàn)Excel模板導(dǎo)出成PDF文件
- SpringBoot+EasyPOI輕松實(shí)現(xiàn)Excel和Word導(dǎo)出PDF
- springboot利用easypoi實(shí)現(xiàn)簡(jiǎn)單導(dǎo)出功能
- SpringBoot EasyPoi動(dòng)態(tài)導(dǎo)入導(dǎo)出的兩種方式實(shí)現(xiàn)方法詳解
- SpringBoot+Vue實(shí)現(xiàn)EasyPOI導(dǎo)入導(dǎo)出的方法詳解
- springboot中EasyPoi實(shí)現(xiàn)自動(dòng)新增序號(hào)的方法
相關(guān)文章
MyBatis中<collection>標(biāo)簽的多種用法
collection標(biāo)簽是處理一對(duì)多關(guān)系的關(guān)鍵工具,它能夠?qū)⒉樵兘Y(jié)果巧妙地映射到Java對(duì)象的集合屬性中,本文主要介紹了MyBatis中<collection>標(biāo)簽的多種用法,感興趣的可以了解一下2025-04-04
Java向上轉(zhuǎn)型與向下轉(zhuǎn)型超詳細(xì)圖解
我們?cè)贘ava編程中經(jīng)常碰到類型轉(zhuǎn)換,對(duì)象類型轉(zhuǎn)換主要包括向上轉(zhuǎn)型和向下轉(zhuǎn)型,這篇文章主要介紹了Java向上轉(zhuǎn)型與向下轉(zhuǎn)型的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-04-04
Java實(shí)現(xiàn)簡(jiǎn)單樹結(jié)構(gòu)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單樹結(jié)構(gòu)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
劍指Offer之Java算法習(xí)題精講N叉樹的遍歷及數(shù)組與字符串
跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03
Java文件處理之使用itextpdf實(shí)現(xiàn)excel轉(zhuǎn)pdf
在文件處理中,經(jīng)常有文件類型轉(zhuǎn)換的使用場(chǎng)景,本文主要介紹了如何使用poi以及itextpdf完成excel轉(zhuǎn)pdf的操作,需要的小伙伴可以參考一下2024-02-02

