package util;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.openxml4j.opc.OPCPackage;
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;
import org.apache.xmlbeans.XmlCursor;
//import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
//import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
/**
* Word2007工具类
*
* curvesapi-1.03、poi-3.14、poi-ooxml-3.14、poi-ooxml-schemas-3.14、stax-api-1.0.1、xmlbeans-2.6.0
*
* https://mvnrepository.com/artifact/com.github.virtuald/curvesapi/1.03
* https://mvnrepository.com/artifact/org.apache.poi/poi/3.14
* https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml/3.14
* https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas/3.14
* https://mvnrepository.com/artifact/stax/stax-api/1.0.1
* https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans/2.6.0
*
* @author ZengWenFeng
*
*/
public class Word2007Util
{
/**
* 根据指定的参数值、模板,生成 word 文档
*
* @param param 需要替换的变量
* @param template 模板
*/
public static XWPFDocument getWord(String template)
{
XWPFDocument doc = null;
try
{
OPCPackage pack = POIXMLDocument.openPackage(template);
doc = new XWPFDocument(pack);
}
catch (Exception e)
{
e.printStackTrace();
}
return doc;
}
/**
* 在定位的位置插入表格;
*
* @author ZengWenFeng
* @date 2010.10.11
* @email [email protected]
* @param key 定位的变量值
* @param doc 需要替换的DOC
*/
public static void insertTable(XWPFDocument doc, String key)
{
List paragraphList = doc.getParagraphs();
if (paragraphList != null && paragraphList.size() > 0)
{
for (XWPFParagraph paragraph : paragraphList)
{
List runs = paragraph.getRuns();
for (XWPFRun run : runs)
{
String text = run.getText(0);
if (text != null)
{
if (text.indexOf(key) >= 0)
{
//把${table}标签移除了
run.setText("", 0);
//指针移动到添加表格的位置,插入表格
XmlCursor cursor = paragraph.getCTP().newCursor();
XWPFTable table = doc.insertNewTbl(cursor);
//
XWPFTableRow tableOneRowOne = table.getRow(0);
//
tableOneRowOne.getCell(0).setText("ZENGWENFENG1-1");
XWPFTableCell cell12 = tableOneRowOne.createCell();
cell12.setText("ZENGWENFENG1-2");
//
XWPFTableRow tableOneRowTwo = table.createRow();
tableOneRowTwo.getCell(0).setText("ZENGWENFENG2-1");
tableOneRowTwo.getCell(1).setText("ZENGWENFENG2-2");
//
XWPFTableRow tableOneRow3 = table.createRow();
tableOneRow3.addNewTableCell().setText("ZENGWENFENG3-3");
tableOneRow3.addNewTableCell().setText("ZENGWENFENG3-4");
}
}
}
}
}
}
/**
* 在制定位置插入表格
*
* @author ZengWenFeng
* @date 2010.10.11
* @email [email protected]
* @param doc
* @param key
* @param listHeader
* @param listData
*/
public static void insertTable(XWPFDocument doc, String key, String[] listHeader, List listData)
{
List paragraphList = doc.getParagraphs();
if (paragraphList != null && paragraphList.size() > 0)
{
for (XWPFParagraph paragraph : paragraphList)
{
List runs = paragraph.getRuns();
for (XWPFRun run : runs)
{
String text = run.getText(0);
if (text != null)
{
if (text.indexOf(key) >= 0)
{
//把${table}标签移除了
run.setText("", 0);
//指针移动到添加表格的位置,插入表格
XmlCursor cursor = paragraph.getCTP().newCursor();
XWPFTable table = doc.insertNewTbl(cursor);
if (listHeader != null && listHeader.length > 0)
{
// XWPFTableRow rowHeader = table.createRow();//table.getRow(0);
XWPFTableRow rowHeader = table.getRow(0);
XWPFTableCell cell;
for (int i = 0; i < listHeader.length; i++)
{
if (i == 0)
{
cell = rowHeader.getCell(0);
}
else
{
cell = rowHeader.createCell();
}
// cell = rowHeader.getCell(i);
// cell.setText(listHeader[i]);
XWPFParagraph p = cell.getParagraphs().get(0);
XWPFRun r = p.createRun();
r.setText(listHeader[i]);
r.setFontSize(32);
r.setFontFamily("思源宋体");
r.setBold(true);
}
}
if (listData != null && listData.size() > 0)
{
XWPFTableCell cell;
for (int i = 0; i < listData.size(); i++)
{
XWPFTableRow rowData = table.createRow();//table.getRow(1);
// XWPFTableRow rowData = table.getRow(i + 1);
String[] curData = listData.get(i);
if (curData != null && curData.length > 0)
{
for (int j = 0; j < curData.length; j++)
{
// if (j == 0)
// {
// cell = rowData.getCell(0);
// }
// else
// {
// cell = rowData.createCell();
// }
// cell = rowData.createCell();
cell = rowData.getCell(j);
// cell.getCTTc().addNewTcPr().addNewTcFont().setVal(STOnOff.TRUE); // 设置字体为宋体
// cell.getCTTc().addNewTcPr().addNewTcFont().setSz(BigInteger.valueOf(12)); // 设置字体大小为12磅
// cell.getCTTc().addNewTcPr().addNewTcFont().setColor(STColor.RGB); // 设置字体颜色为RGB格式
// cell.getCTTc().addNewTcPr().addNewTcFont().setColor().setVal("000000"); // 设置字体颜色为黑色(RGB值为000000)
// cell.getCTTc().addNewTcPr().addNewTcFont().setVal(STOnOff.TRUE); // 设置字体为粗体
// cell.getCTTc().addNewTcPr().addNewShd().setFill("D0D0D0"); // 设置背景颜色为灰色
// cell.getCTTc().addNewTcPr().addNewTcBorders().addNewTop().setVal(STBorder.SINGLE); // 设置上边框为实线
// cell.getCTTc().addNewTcPr().addNewTcBorders().addNewLeft().setVal(STBorder.SINGLE); // 设置左边框为实线
// cell.getCTTc().addNewTcPr().addNewTcBorders().addNewBottom().setVal(STBorder.SINGLE); // 设置下边框为实线
// cell.getCTTc().addNewTcPr().addNewTcBorders().addNewRight().setVal(STBorder.SINGLE); // 设置右边框为实线
// cell.setText(curData[j]);
XWPFParagraph p = cell.getParagraphs().get(0);
XWPFRun r = p.createRun();
r.setText(curData[j]);
r.setFontSize(32);
r.setFontFamily("思源宋体");
// r.setBold(true);
// r.setItalic(true);
// r.setTextPosition(10);
}
}
}
}
}
}
}
}
}
}
public static final String PATH = "C:/Users/Administrator/Desktop/";
public static void test03() throws Exception
{
//
String[] listHeader = new String[]
{"id", "code", "name"};
//
List listData = new ArrayList();
String[] data_01 = new String[]
{"zwf-0001", "005129", "zwf00000011"};
String[] data_02 = new String[]
{"zwf-0002", "005128", "zengwenfeng"};
String[] data_03 = new String[]
{"zwf-0003", "005127", "zeng00000wf"};
String[] data_04 = new String[]
{"zwf-0004", "005126", "wenfeng3434"};
listData.add(data_01);
listData.add(data_02);
listData.add(data_03);
listData.add(data_04);
//
XWPFDocument doc = Word2007Util.getWord(PATH + "1.docx");
Word2007Util.insertTable(doc, "${table}");
Word2007Util.insertTable(doc, "${table1}", listHeader, listData);
//
FileOutputStream fos = new FileOutputStream(PATH + "2.docx");
doc.write(fos);
fos.close();
}
public static void main(String[] args) throws Exception
{
test03();
System.out.println("ok!");
}
}
https://download.csdn.net/download/spencer_tseng/88670881