网上搜了很多很多资料,都没有实现。机缘巧合之下发现一个特简单的方法。直接接将复选框当成string写进去就可以了。
给个简单的例子,有遇到过这种需求的可以试一下。
导入相关jar包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
上代码
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.File;
import java.io.FileOutputStream;
/**
* @author DaGangCai
* @Date 2020/9/17-18:31
*/
public class MarcCheckWord {
//调用测试
public static void main(String[] args) throws Exception {
String path = "E:/wordCheckBox.docx";
XWPFDocument document = new XWPFDocument();
XWPFParagraph titleParagraph = document.createParagraph();
titleParagraph.setAlignment(ParagraphAlignment.LEFT);
XWPFRun titleFun = titleParagraph.createRun();
titleFun.setText("选中的复选框:☑" + "\r\r\r" + "未选中的复选框:□");
titleFun.setFontSize(12);
titleFun.setFontFamily("宋体");
titleFun.addBreak();
try {
document.createStyles();
System.out.println();
File file = new File(path);
if (file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(path);
document.write(out);
out.close();
System.out.println("生成成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
}