主要是回填 勾选框和非勾选框 【tip: 模板文件中要替换的字符串,要连着打出来!,否则替换文件的时候会有问题。】
所需jar包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>
//封装参数 0x0052对应的是带√的方框 的十六进制代码【替换后会变成字母R,要设置为Wingdings 2字体】,0x2610反之
Map<String, String> paramMap = new HashMap<String, String>();
int[] code = {0x2610,0x0052};//{0x2610, 0x2611}; {0x002A,0x0052}
paramMap.put("gy",new String(code,1,1));
paramMap.put("rj",new String(code,0,1));
paramMap.put("kx",new String(code,0,1));
paramMap.put("xd",new String(code,0,1));
paramMap.put("sj",new String(code,0,1));
paramMap.put("qt",new String(code,0,1));
InputStream inputStream = this.getClass()
.getResourceAsStream("模板文件的地址");
XWPFDocument document = new XWPFDocument(
inputStream);
// 替换段落中的指定文字
Iterator<XWPFParagraph> itPara = document
.getParagraphsIterator();
while (itPara.hasNext()) {
XWPFParagraph paragraph = itPara.next();
List<XWPFRun> runs = paragraph.getRuns();
for (int i = 0; i < runs.size(); i++) {
String oneparaString = runs.get(i).getText(
runs.get(i).getTextPosition());
System.out.println(oneparaString);
// 此处的map 为上面一个代码段中的paramMap
for (Map.Entry<String, String> entry : map
.entrySet()) {
if(oneparaString!=null){
oneparaString = oneparaString.replace(
entry.getKey(), entry.getValue()==null?"":entry.getValue());
}
}
runs.get(i).setText(oneparaString, 0);
}
}
// 替换表格中的指定文字
Iterator<XWPFTable> itTable = document.getTablesIterator();
while (itTable.hasNext()) {
XWPFTable table = (XWPFTable) itTable.next();
int rcount = table.getNumberOfRows();
for (int i = 0; i < rcount; i++) {
XWPFTableRow row = table.getRow(i);
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
String cellTextString = cell.getText();
for(int p=0;p<cell.getParagraphs().size();p++){
List<XWPFRun> runs = cell.getParagraphs().get(p).getRuns();
for (int ii = 0; ii < runs.size(); ii++) {
String oneparaString = runs.get(ii).getText(
runs.get(ii).getTextPosition());
System.out.println(oneparaString);
for (Map.Entry<String, String> entry : map
.entrySet()) {
if(oneparaString!=null){
oneparaString = oneparaString.replace(
entry.getKey(), entry.getValue()==null?"":entry.getValue());
}
}
if("R".equals(oneparaString)||"*".equals(oneparaString)){
runs.get(ii).setFontFamily("Wingdings 2");
}
runs.get(ii).setText(oneparaString, 0);
}
}
}
}
}
return document;
//上面代码中返回一个XWPFDocument 对象,下面方法 生成word文件
public Integer createWord(XWPFDocument doc, String fileName) {
if (!rootPath.endsWith(File.separator)) {
rootPath += File.separator;
}
String fname = String.valueOf(System.currentTimeMillis()) + ".docx";
String relPath = "export_word" + File.separator + fname;
String path = rootPath + relPath;
File file = new File(path);
file.getParentFile().mkdirs();
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(file);
doc.write(outStream);
//这个对应之前系统里的上传逻辑。不需要的删掉即可。
FileInfo fileUpload = this.saveFileInfo(fileName, relPath, file.length(), null);
return fileUpload.getId();
} catch (IOException e) {
logger.error("文件创建失败" + e.getMessage());
} finally {
if(null != outStream){
try {
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}