首先要将word的内容全部写入,
public static void read(String url,String saveUrl) throws Exception{ BufferedInputStream fis=new BufferedInputStream(new FileInputStream(url)); Workbook workbook = new XSSFWorkbook(fis); Sheet sheet = workbook.getSheetAt(0); //获取第二个sheet页 int rowNum = sheet.getLastRowNum(); //获取最大行数 //遍历这个行数 for (int i = 1; i <= rowNum; i++) { Row row = sheet.getRow(i); //获取执行时间 String getExecutionDate= Excute.getCellValves(row,18) ; //获取执行编号 String getCycleNo= Excute.getCellValves(row,1) ; //获取执行编号 String getBreakdownNo= Excute.getCellValves(row,2) ; //获取测试编号 String getTestCaseNo= Excute.getCellValves(row,3) ; //获取测试描述 String getTestCaseDescription= Excute.getCellValves(row,12) ; //获取测试结果 String getTestResult= Excute.getCellValves(row,19) ; //获取测试人 String getTester= Excute.getCellValves(row,16) ; //获取功能 String getFunction= Excute.getCellValves(row,7) ; //获取检查时间 String getCheckData= Excute.getCellValves(row,17) ; //获取备注 String getRemark= Excute.getCellValves(row,20) ; //获取word名称 String getTestScriptNo= Excute.getCellValves(row,23) ; BufferedOutputStream fos=new BufferedOutputStream(new FileOutputStream( saveUrl+ getTestScriptNo +".docx")); XWPFDocument document=new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); //设置word,页眉,页脚 SetPageSize.setWordHeader(document); SetPageSize.setPageBackColor(document); // document.createParagraph().setPageBreak(true); //设置测试环境 XWPFParagraph pr2= document.createParagraph(); XWPFRun xr= pr2.createRun(); xr.addCarriageReturn(); xr.setText("Initial setup:"); xr.setBold(true); xr.setFontSize(10); //具体哪个环境 XWPFParagraph pr1= document.createParagraph(); XWPFRun xTestEverValue= pr1.createRun(); xTestEverValue.setFontSize(11); xTestEverValue.setText("Testing Environment: DEVB"); xTestEverValue.addCarriageReturn(); System.out.println(" writer file"); document.write(fos); fos.close(); document.close(); } workbook.close(); System.out.println("word create success!"); }
其次,将生成好的word,进行遍历获取所有word的文件名, 在对应的word中获取页脚, 将页脚增加动态页码
public static void setFood(String fileUrl) throws Exception { List<String> listFile = getFileName(fileUrl); for (int i = 0; i < listFile.size(); i++) { FileInputStream fis =new FileInputStream(fileUrl+listFile.get(i)); XWPFDocument document = new XWPFDocument(fis); // 假设 xwpfDocument 是你已经生成的文档对象 List<XWPFFooter> foot= document.getFooterList(); for(XWPFFooter foots: foot) { XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document); XWPFParagraph footerParagraph = foots.createParagraph(); footerParagraph.setAlignment(ParagraphAlignment.LEFT); // 添加页码到页脚 XWPFRun footerRun = footerParagraph.createRun(); footerRun.setText("Version 1.0 "); footerRun.setText(" Page "); footerRun.getCTR().addNewFldChar().setFldCharType(STFldCharType.Enum.forString("begin")); footerRun.getCTR().addNewInstrText().setStringValue("PAGE \\* MERGEFORMAT"); footerRun.getCTR().addNewInstrText().setSpace(SpaceAttribute.Space.Enum.forString("preserve")); footerRun.getCTR().addNewFldChar().setFldCharType(STFldCharType.Enum.forString("end")); //设置页码的字体 footerRun.setFontFamily("Times New Roman"); footerRun.setFontSize(8); // 将新的页脚添加到所有页面 headerFooterPolicy.createFooter(STHdrFtr.DEFAULT, new XWPFParagraph[]{footerParagraph}); } FileOutputStream fos=new FileOutputStream(fileUrl + listFile.get(i)); document.write(fos); } }