有关java动态操作word文档,上一篇写了如何使用模板动态设置对于内容以及相关单元格的动态合并问题,详细请参看如下文档:
java利用Aspose.Words操作Word动态模板文档并动态设置单元格合并
这篇文档说说,如何利用Aspose.Words动态创建自定义doc文档,和使用office工具编写doc文档是一个思路,详细请认真阅读。
一、创建案例以及文档创建授权
例如要创建如下案例的文档信息:
需要先获取相关授权信息,关键代码如下,详细请参考java操作doc(一)
//获取Aspose授权
WordLib.setUnlimitedLicense();
二、doc文档动态自定义创建
@PostMapping("/test/{proNo}")@ApiOperation("测试使用Aspose创建自定义doc文档")public void testAsposeWordsCreateDoc(@PathVariable("proNo") String proNo,HttpServletResponse response){//获取Aspose授权WordLib.setUnlimitedLicense();try {//创建文档Document doc = new Document();DocumentBuilder docBuild = new DocumentBuilder(doc);//创建文档标题样式Font font = docBuild.getFont();font.setName("宋体");font.setSize(16);docBuild.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);docBuild.writeln("Aspose动态创建自定义doc文档");//换行docBuild.insertBreak(BreakType.PARAGRAPH_BREAK);//创建正文信息docBuild.getFont().setSize(12);docBuild.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);docBuild.writeln("编号:" + proNo);docBuild.getFont().clearFormatting();//设置表格填充颜色Color color = new Color(224, 223, 223);// 创建一个表格并设置整体样式docBuild.startTable();//创建新的一行docBuild.insertCell();docBuild.getCellFormat().setWidth(200);docBuild.getRowFormat().setHeight(50);docBuild.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);docBuild.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);//设置表格内容docBuild.getCellFormat().getShading().setBackgroundPatternColor(color);docBuild.write("Aspose项目");docBuild.insertCell();docBuild.getCellFormat().getShading().setBackgroundPatternColor(Color.white);docBuild.write("Aspose");docBuild.insertCell();docBuild.getCellFormat().getShading().setBackgroundPatternColor(color);docBuild.write("操作人");docBuild.insertCell();docBuild.getCellFormat().getShading().setBackgroundPatternColor(Color.white);docBuild.write("key");//结束当前行docBuild.endRow();docBuild.insertCell();docBuild.getCellFormat().getShading().setBackgroundPatternColor(color);docBuild.write("项目负责人");docBuild.insertCell();docBuild.getCellFormat().getShading().setBackgroundPatternColor(Color.white);docBuild.write("Aspose.Words");docBuild.insertCell();docBuild.getCellFormat().getShading().setBackgroundPatternColor(color);docBuild.write("测试日期");docBuild.insertCell();docBuild.getCellFormat().getShading().setBackgroundPatternColor(Color.white);docBuild.write(DateUtil.daFormat(new Date()));docBuild.endRow();docBuild.insertCell();docBuild.getCellFormat().getShading().setBackgroundPatternColor(color);docBuild.write("测试等级");docBuild.insertCell();docBuild.getCellFormat().getShading().setBackgroundPatternColor(Color.white);docBuild.write("□ A □ B ☑ C");docBuild.insertCell();docBuild.write("阶段");docBuild.insertCell();docBuild.write("test阶段");docBuild.endRow();docBuild.insertCell();docBuild.getCellFormat().getShading().setBackgroundPatternColor(color);docBuild.write("测试过程");docBuild.insertCell();docBuild.getCellFormat().getShading().setBackgroundPatternColor(Color.white);docBuild.write("使用Aspose创建自定义doc文档");docBuild.getCellFormat().setHorizontalMerge(CellMerge.FIRST);docBuild.insertCell();docBuild.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);docBuild.insertCell();docBuild.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);docBuild.endRow();docBuild.insertCell();docBuild.getCellFormat().getShading().setBackgroundPatternColor(color);docBuild.write("最终结果描述");docBuild.insertCell();docBuild.getCellFormat().getShading().setBackgroundPatternColor(Color.white);docBuild.write("全部合格,整体测试通过");docBuild.getCellFormat().setHorizontalMerge(CellMerge.FIRST);docBuild.insertCell();docBuild.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);docBuild.insertCell();docBuild.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);docBuild.endRow();docBuild.insertCell();docBuild.getCellFormat().getShading().setBackgroundPatternColor(color);docBuild.write("经验教训");docBuild.insertCell();docBuild.getCellFormat().getShading().setBackgroundPatternColor(Color.white);docBuild.write("时间是检验真理的唯一标准");docBuild.getCellFormat().setHorizontalMerge(CellMerge.FIRST);docBuild.insertCell();docBuild.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);docBuild.insertCell();docBuild.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);docBuild.endRow();docBuild.endTable();// 保存文档doc.updateFields();ByteArrayOutputStream outStream = new ByteArrayOutputStream();doc.save(outStream, SaveFormat.DOCX);// 建立一个文件的输出的输出流ByteArrayOutputStream byteArrayOutputStream = (ByteArrayOutputStream) outStream;byte[] aByte = byteArrayOutputStream.toByteArray();// 设置响应头信息response.setCharacterEncoding("UTF-8");response.setContentType("application/octet-stream");response.setHeader("Content-Disposition", "attachment; filename=\"" + proNo+".docx" + "\"");String returnType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";response.setContentType(returnType);OutputStream responseOutputStream = response.getOutputStream();responseOutputStream.write(aByte);responseOutputStream.flush();} catch (Exception e) {System.out.println("使用Aspose动态创建自定义doc文档异常,编号:" + proNo);e.printStackTrace();}}