关于POI生成word,有两种方式,一是新建文件写入内容,二十使用模版生成。
一、新建文件写入内容(以内容是个),代码如下:
package com.rn.wisdom.utils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.ObjectUtils.Null;
import org.apache.poi.sl.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell.XWPFVertAlign;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
/**
* word 导出
* */
public class WordExporter {
private XWPFDocument document;
private String title;//标题
public WordExporter(String title) {
document = new XWPFDocument();
this.title = title;
setTite();
}
//设置word标题
public void setTite() {
XWPFParagraph paragraph = document.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = paragraph.createRun();
run.setText(title);
run.setBold(true);
run.setFontFamily("宋体");
run.setFontSize(22);
}
//设置表格
public void setContent() {
//写入内容
XWPFParagraph paragraph = document.createParagraph();//段落
XWPFTable table = document.createTable();//表格
}
//下载public void responseExport( String fileName) {try {FileOutputStream out = new FileOutputStream(fileName);document.write(out);out.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String[] args) throws IOException {WordExporter wordExporter = new WordExporter("测试"); wordExporter.setContent();
wordExporter.responseExport("D:\\测试.docx");} }
二、使用模版生成word,HWPFDocument只支持.doc格式的模版
package com.rn.wisdom.utils;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.PropertyNode.EndComparator;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
/**
* word 导出 使用模版导出 版本有限制模版只能是.doc
* */
public class WordExporter3 {
private HWPFDocument document;
private InputStream in;
/**
* 初始化文档
* */
public WordExporter3(String filename) {
try {
in = new FileInputStream(filename);
document = new HWPFDocument(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 替换数据
* @param params
* */
public void replaceData(Map<String, String> params) {
Range range = document.getRange();
for (Map.Entry<String, String> entry : params.entrySet()) {
range.replaceText(entry.getKey(), entry.getValue());
}
}
/**
* 导出
* @param response
* @param fileName 导出文档名
* */
public void responseExport( String fileName) {
try {
FileOutputStream out = new FileOutputStream(fileName);
document.write(out);
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
WordExporter3 wordExporter = new WordExporter3("D:\\测试.doc");
Map<String, String> map = new HashMap<>();
map.put("${title}", "你好");
map.put("${column1}", "helloword");
map.put("${column2}", "你好");
map.put("${column3}", "你好");
map.put("${column4}", "你好");
map.put("${column5}", "你好");
map.put("${column6}", "2行2列");
map.put("${column7}", "2行3列");
map.put("${column8}", "2行4列");
map.put("${column9}", "3行2列");
map.put("${column10}", "3行2列");
wordExporter.replaceData(map);
wordExporter.responseExport("D:\\测试2.docx");
}
}