poi 设置允许西文在单词中间换行
一、问题描述
poi 设置允许西文在单词中间换行?
// 创建一个新的文档
XWPFDocument document = new XWPFDocument();
// 创建段落
XWPFParagraph firstParagraph = document.createParagraph();
firstParagraph.setSpacingBetween(1.5);
firstParagraph.setStyle("1111");
firstParagraph.setVerticalAlignment(TextAlignment.AUTO);
// 创建文本
XWPFRun run3 = firstParagraph.createRun();
run3.setText(" " + "②网上办:");
run3.setFontFamily("微软雅黑");
run3.setFontSize(16);
run3 = firstParagraph.createRun();
run3.setText("测试测试测试测:https://aaaa.aa.aaa.aa/aaaaaaaaa/item/toDetails/7eecb643-c88b-433e-942e-7c14ae6cd7cc");
run3.setFontSize(16);
run3.setFontFamily("宋体");
try {
// 将文档保存到文件
FileOutputStream out = new FileOutputStream("D:\\example.docx");
document.write(out);
out.close();
System.out.println("文档创建成功!");
} catch (IOException e) {
System.out.println("创建文档时出现错误:" + e.getMessage());
}
}
二、解决问题
问题解决方案:
依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-full</artifactId>
<version>5.2.2</version>
</dependency>
三、解决代码
import org.apache.poi.xwpf.usermodel.TextAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STOnOff1;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author caozhen
* @ClassName FF
* @description: TODO
* @date 2023年10月10日
* @version: 1.0
*/
public class FF {
public static void main(String[] args) {
// 创建一个新的文档
XWPFDocument document = new XWPFDocument();
// 创建段落
XWPFParagraph firstParagraph = document.createParagraph();
firstParagraph.setSpacingBetween(1.5);
firstParagraph.setStyle("1111");
firstParagraph.setVerticalAlignment(TextAlignment.AUTO);
// 设置允许在单词中间换行
setParagraphWrap(firstParagraph, true);
// 创建文本
XWPFRun run3 = firstParagraph.createRun();
run3.setText(" " + "②网上办:");
run3.setFontFamily("微软雅黑");
run3.setFontSize(16);
run3 = firstParagraph.createRun();
run3.setText("测试测试测试测:https://aaaa.aa.aaa.aa/aaaaaaaaa/item/toDetails/7eecb643-c88b-433e-942e-7c14ae6cd7cc");
run3.setFontSize(16);
run3.setFontFamily("宋体");
// 允许在单词中间换行
run3.addCarriageReturn();
try {
// 将文档保存到文件
FileOutputStream out = new FileOutputStream("D:\\example.docx");
document.write(out);
out.close();
System.out.println("文档创建成功!");
} catch (IOException e) {
System.out.println("创建文档时出现错误:" + e.getMessage());
}
}
/**
* 段落设置-中文板式 : 允许西文在单词中间换行
*/
public static void setParagraphWrap(XWPFParagraph paragraph, boolean allow) {
//解释: 允许后(勾选) word展示效果
// | 这是一个段落, | |带来了珍贵的礼物—— |
// |paragraph | | 五彩缤纷的落叶。 |
// 不允许(取消勾选) word展示效果
// | 这是一个段落,par| |带来了珍贵的礼物——五彩缤 |
// |agraph | |纷的落叶。 |
CTP ctp = paragraph.getCTP();
CTPPr ctpPr = ctp.isSetPPr() ? ctp.getPPr() : ctp.addNewPPr();
CTOnOff ctOnOff = ctpPr.isSetWordWrap() ? ctpPr.getWordWrap() : ctpPr.addNewWordWrap();
if (allow) {
//允许
ctOnOff.setVal(STOnOff1.OFF);
} else {
//不允许
ctpPr.unsetWordWrap();
}
}