项目场景:
spring boot集成easypoi导出word
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
问题描述
spring boot集成easypoi导出word时,内容包含换行符\n,导出word时换行符失效,会将换行符\n识别为空格。
解决方案:
第一种:生成段落的方式
示例代码:
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("导出Word")
@GetMapping("/export")
public ActionResult export() {
exportWord();
return ActionResult.success();
}
/**
* 导出Word,支持换行
*/
public void exportWord(){
try{
String content = "第一行\n第二行中文\n"+"第三行";
@Cleanup XWPFDocument doc = new XWPFDocument();
if(content != null && content.contains("\n")) {
//设置换行
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 = "测试换行内容.docx";
String filePath = "F:"+ File.separator + name;
@Cleanup FileOutputStream output = new FileOutputStream(filePath);
doc.write(output);
} catch (Exception e) {
e.printStackTrace();
}
}
}
导出的word内容
第二种:替换模板的情况,换行符替换成回车
示例代码:
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("导出Word")
@GetMapping("/export")
public ActionResult export() {
exportWordTemplate();
return ActionResult.success();
}
/**
* 导出Word替换模板,支持换行
*/
public void exportWordTemplate(){
try{
String content = "第一行\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 = "测试换行内容-替换模板.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对象定义具有一组公共属性的文本区域
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模板,内容为:
导出的内容
导出的本地文件截图