一、引用依赖
xml
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>19.5</version>
<classifier>jdk17</classifier>
</dependency>
二、导出类
java
public class WordTable {
//定义文档接口
public Document doc;
public DocumentBuilder builder;
public WordTable() {
}
/**
* 初始化构造方法
* @param doc 模板地址
*/
public WordTable(String doc) {
try {
// 建立DocumentBuilder物件
this.doc = new Document(doc);
this.builder = new DocumentBuilder(this.doc);
} catch (Exception e) {
System.out.println("初始化失败" + e.toString());
}
}
/**
* 新建文档
* @param doc 新建对象
*/
public WordTable(Document doc) {
try {
// 建立DocumentBuilder物件
this.doc = doc;
this.builder = new DocumentBuilder(this.doc);
} catch (Exception e) {
System.out.println("初始化失败" + e.toString());
}
}
/**
* 横向纸张
*/
public void Portarit(){
builder.getPageSetup().setOrientation(Orientation.LANDSCAPE);
}
/**
* 新增表格
*/
public void createTale(List<OrgWordTarget> orgWordTargets,String orgName) throws Exception {
//水平居中
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
StyleCollection styles= doc.getStyles();
builder.getFont().setName("微软雅黑");
builder.getFont().setBold(true);
builder.getFont().setSize(18);
builder.writeln("标题");
builder.getFont().setName("宋体");
builder.getFont().setSize(12);
builder.getFont().setBold(false);
builder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.MULTIPLE);
//12为一倍行距
builder.getParagraphFormat().setLineSpacing(18);
builder.write(orgName+"(盖章): ");
builder.writeln(" 年 月 日 ");
//创建表格
Table table = builder.startTable();
String[] titles = new String[]{"标题1", "标题2", "标题3", "标题4", "标题5", "标题6", "标题7"};
double[] lens = new double[]{8.8, 18, 14, 15.8, 15.8, 14.6, 12.4};
builder.getFont().setSize(11);
builder.getFont().setName("微软雅黑");
for (int i = 0; i < 7; i++) {
builder.insertCell();//插入单元格
//设置宽度注意要在插入单元格后在设置
builder.getCellFormat().setWidth(lens[i]);
// builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(lens[i]));//列宽-百分比
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.LIGHT_GRAY);//背景色-灰色
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);//垂直居中对齐
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);//水平居中
builder.write(titles[i]);//写入内容
}
builder.endRow();//结束行
int i=1;
builder.getFont().setBold(false);
for (OrgWordTarget o:orgWordTargets) {
insertCell(i+"");
builder.getCellFormat().setWidth(lens[0]);
insertCell(o.getTargetName());
builder.getCellFormat().setWidth(lens[1]);
insertCell(o.getDataSource());
builder.getCellFormat().setWidth(lens[2]);
insertCell(o.getMethod());
builder.getCellFormat().setWidth(lens[3]);
insertCell(o.getExplain());
builder.getCellFormat().setWidth(lens[4]);
insertCell(o.getDataUpdateInterval());
builder.getCellFormat().setWidth(lens[5]);
insertCell("□ 是 □ 否");
builder.getCellFormat().setWidth(lens[6]);
builder.endRow();//结束行
i++;
}
builder.endTable();//结束表格
builder.getFont().setSize(11);
builder.getFont().setBold(false);
builder.getFont().setName("宋体");
builder.writeln();
builder.writeln("备注:无");
builder.getFont().setSize(12);
builder.writeln(" 联系人:");
builder.write(" 联系方式:");
//设置页码
addHeaderFooter(doc);
}
/**
* 插入方法
* @param value
*/
public void insertCell(String value){
builder.insertCell();//插入单元格
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);//背景色-白色
// builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);//对齐-居中
builder.getParagraphFormat().setAlignment(CellVerticalAlignment.CENTER);//垂直-居中
if (value==null){
return;
}
builder.write(value);//写入内容
}
/**
* 设置页码
* @param doc
* @throws Exception
*/
public void addHeaderFooter(Document doc) throws Exception {
//创建页脚 页码
HeaderFooter footer = new HeaderFooter(doc, HeaderFooterType.FOOTER_PRIMARY);
doc.getFirstSection().getHeadersFooters().add(footer);
//页脚段落
Paragraph footerpara = new Paragraph(doc);
footerpara.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
Run footerparaRun = new Run(doc);
footerparaRun.getFont().setName("宋体");
footerparaRun.getFont().setSize(9.0);//小5号字体
footerpara.appendChild(footerparaRun);
footerpara.appendField(FieldType.FIELD_PAGE, true);//当前页码
footerpara.appendChild(footerparaRun);
footer.appendChild(footerpara);
}
/**
* 替换模板中的站位符
*生成站位符方法 选择插入-->文档部件-->域-->类别选择邮箱合并--->MergeField-->自定义域名
* @throws Exception
*/
public void setMap() throws Exception {
String[] landSupplyA = new String[1];
String[] landSupplyB = new String[1];
landSupplyA[0] = "test";
landSupplyB[0] = "测试一下";
doc.getMailMerge().execute(landSupplyA, landSupplyB);
}
}
占位符位置截图: