POI导出word
最近有一个项目需要用做导出word的功能,刚开始用的是poi,开始写的时候才发现poi对于word的支持真的是少的可怜,还是推荐大家用xml或者别的来做导出word,本次功能分两篇博客展现如果用需要用poi的可以看看这篇博客,欢迎补充。
- 替换word表格参数
- 动态生成行
根据word模板导出word
public class WordUtil {
/**
* 这是一个工具类
* 替换所有段落中的标记
*
* @param xwpfParagraphList
* @param params
*/
public static void replaceInAllParagraphs(List<XWPFParagraph> xwpfParagraphList, Map<String, String> params) {
for (XWPFParagraph paragraph : xwpfParagraphList) {
if (paragraph.getText() == null || paragraph.getText().equals("")) continue;
for (String key : params.keySet()) {
if (paragraph.getText().contains(key)) {
replaceInParagraph(paragraph, key, params.get(key));
}
}
}
}
/**
* 替换段落中的字符串
*
* @param xwpfParagraph
* @param oldString
* @param newString
*/
public static void replaceInParagraph(XWPFParagraph xwpfParagraph, String oldString, String newString) {
Map<String, Integer> pos_map = findSubRunPosInParagraph(xwpfParagraph, oldString);
if (pos_map != null) {
List<XWPFRun> runs = xwpfParagraph.getRuns();
XWPFRun modelRun = runs.get(pos_map.get("end_pos"));
XWPFRun xwpfRun = xwpfParagraph.insertNewRun(pos_map.get("end_pos") + 1);
xwpfRun.setText(newString);
if (modelRun.getFontSize() != -1)
xwpfRun.setFontSize(modelRun.getFontSize());//默认值是五号字体,但五号字体getFontSize()时,返回-1
xwpfRun.setFontFamily(modelRun.getFontFamily());
for (int i = pos_map.get("end_pos"); i >= pos_map.get("start_pos"); i--) {
xwpfParagraph.removeRun(i);
}
}
}
/**
* 找到段落中子串的起始XWPFRun下标和终止XWPFRun的下标
*
* @param xwpfParagraph
* @param substring
* @return
*/
public static Map<String, Integer> findSubRunPosInParagraph(XWPFParagraph xwpfParagraph, String substring) {
List<XWPFRun> runs = xwpfParagraph.getRuns();
int start_pos = 0;
int end_pos = 0;
String subtemp = "";
for (int i = 0; i < runs.size(); i++) {
subtemp = "";
start_pos = i;
for (int j = i; j < runs.size(); j++) {
if (runs.get(j).getText(runs.get(j).getTextPosition()) == null) continue;
subtemp += runs.get(j).getText(runs.get(j).getTextPosition());
if (subtemp.equals(substring)) {
end_pos = j;
Map<String, Integer> map = new HashMap<>();
map.put("start_pos", start_pos);
map.put("end_pos", end_pos);
return map;
}
}
}
return null;
}
/**
* 替换所有的表格
*
* @param xwpfTableList
* @param params
*/
public static void replaceInTables(List<XWPFTable> xwpfTableList, Map<String, String> params) {
for (XWPFTable table : xwpfTableList) {
replaceInTable(table, params);
}
}
/**
* 替换一个表格中的所有行
*
* @param xwpfTable
* @param params
*/
public static void replaceInTable(XWPFTable xwpfTable, Map<String, String> params) {
List<XWPFTableRow> rows = xwpfTable.getRows();
replaceInRows(rows, params);
}
/**
* 替换表格中的一行
*
* @param rows
* @param params
*/
public static void replaceInRows(List<XWPFTableRow> rows, Map<String, String> params) {
for (int i = 0; i < rows.size(); i++) {
XWPFTableRow row = rows.get(i);
replaceInCells(row.getTableCells(), params);
}
}
/**
* 替换一行中所有的单元格
*
* @param xwpfTableCellList
* @param params
*/
public static void replaceInCells(List<XWPFTableCell> xwpfTableCellList, Map<String, String> params) {
for (XWPFTableCell cell : xwpfTableCellList) {
replaceInCell(cell, params);
}
}
/**
* 替换表格中每一行中的每一个单元格中的所有段落
*
* @param cell
* @param params
*/
public static void replaceInCell(XWPFTableCell cell, Map<String, String> params) {
List<XWPFParagraph> cellParagraphs = cell.getParagraphs();
replaceInAllParagraphs(cellParagraphs, params);
}
}
下面是调用方法 因为map泛型规定了String,所以非String类型的参数都要用""串接起来
public static void main(String[] args) {
Map<String, String> params = new HashMap<>();
params.put("${name}", "请假");
params.put("${user}", "邹某");
params.put("${time}", "" + new SimpleDateFormat("yyyy-MM-dd hh:mm").format(new Date()) + "");
params.put("${type}", "产假");
params.put("${project}", "金融");
params.put("${startTime}", "2018-12-05 12:00");
params.put("${endTime}", "2018-12-05 12:00");
params.put("${dept}", "开发部");
params.put("${numDay}", "1");
params.put("${remark}", "不想上班");
String filepath = "E:\\test.docx";
String destpath = "E:\\test3.docx";
WordUtil wordUtil = new WordUtil();
WordUtil wordUtil = new WordUtil();
try {
OPCPackage pack = POIXMLDocument.openPackage(filepath);//读取模板文件
XWPFDocument document = new XWPFDocument(pack);//创建word文件并将模板导入
//对段落中的标记进行替换
List<XWPFParagraph> parasList = xwpfDocument.getParagraphs();
replaceInAllParagraphs(parasList, map);
//表格标记替换
List<XWPFTable> tables = document.getTables();
wordUtil.replaceInTables(tables, params);
FileOutputStream outStream = new FileOutputStream(destpath);
document.write(outStream);
outStream.flush();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
以上就是poi替换word模板参数的所有内容,欢迎补充