public static void createContent3(Date adtStart, Date adtEnd,
Map<String, Object> aMap,Map<String, DataTable> mapTables) { DataTable content3_t1 = new DataTable("content3_t1"); //
DataColumnCollection dcc = content3_t1.getColumns(); dcc.add("dw");
dcc.add("id"); DataTable content3_t2 = new DataTable("content3_t2"); //row.getTable();
DataColumnCollection dcc2 = content3_t2.getColumns();
dcc2.add("pid");
dcc2.add("wtdw");
dcc2.add("wzmjs");
dcc2.add("wzsdlds");
dcc2.add("wzfs");
for(int i=0;i<3;i++){
DataRow row = content3_t1.newRow(); //新增一行
row.set(0, "dw"+i); for(int j=0;j<3;j++){
DataRow row2 = content3_t2.newRow(); //新增一行
row2.set(0, "id"+i);
row2.set(1, "wtdw"+i+"-"+j);
row2.set(2, "wzmjs"+i+"-"+j);
row2.set(3, "wzsdlds"+i+"-"+j);
row2.set(4, "wzsdlds"+i+"-"+j);
content3_t2.getRows().add( row2 ); //加入此行数据
}
mapTables.put("content3_t2", content3_t2);
row.set(1, "id"+i);
content3_t1.getRows().add( row ); //加入此行数据
}
mapTables.put("content3_t1", content3_t1); } public static void main(String[] args) throws ParseException { String[] list = new String[1];
list[0]="320402000000"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String adtSta="2019-03-01 00:00:00";
String adEnd ="2019-04-01 00:00:00";
Date adtStart = sdf.parse(adtSta);
Date adtEnd = sdf.parse(adEnd); Map<String, Object> mapContents = new HashMap<String, Object>();
Map<String, DataTable> mapTables = new HashMap<String, DataTable>(); createContent3(adtStart, adtEnd, mapContents,mapTables); String path = "D:\\PEES2015\\hop-web\\src\\main\\webapp\\report\\dkt\\dkt-tjfx-zgqk.docx";
String webPath = "\\documents\\rptXCDC_"+HsFileUtil.getUUID()+".doc";
String tPath = "D:\\PEES2015\\hop-web\\src\\main\\webapp" + webPath; System.out.println("Start create Image..."); try {
com.aspose.words.License license = new com.aspose.words.License();
String s = "D:\\PEES2015\\hop-web\\src\\main\\webapp\\report\\";
license.setLicense( s + "lic.xml" );
System.out.println("授权成功");
} catch (Exception e) {
e.printStackTrace();
} //模板文件和将要创建的新文件
String template = path; //可以是doc或docx
String destdoc = tPath; //可以是doc或docx
Document doc;
try{
//第二步 读取word模板文件,可以是.doc或者.docx
doc = new Document( template );
System.out.println("读取word模板文件,可以是.doc或者.docx");
//合并模版,相当于页面的渲染
MailMerge mm = doc.getMailMerge(); // 将 t1 和 t2 关联起来
DataSet ds = new DataSet();
DataTable content3_t1 = mapTables.get("content3_t1");
DataTable content3_t2 = mapTables.get("content3_t2");
ds.getTables().add( content3_t1 );
ds.getTables().add( content3_t2 );
String[] contCols = {"id"};
String[] lstCols = {"pid"};
ds.getRelations().add( new DataRelation("content3_t1t2", content3_t1, content3_t2, contCols, lstCols) );
mm.executeWithRegions(ds); //调用接口 //提供数据源
String[] ks = mapContents.keySet().toArray(new String[0]);//.toArray();//
Object[] vs = mapContents.values().toArray();//.toArray();//
mm.execute(ks, vs);
doc.save( destdoc );
System.out.println("End create Word...");
}catch (Exception e) {
e.printStackTrace();
} Map<String, Object> rlts = Maps.newHashMap();
rlts.put("data", webPath);
System.out.println(rlts);
}