java在word设置动态页码

首先要将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);
    }
}

你可以使用 Apache POI 库来生成 Word 文档,并且利用该库提供的 API 来设置页眉页码。 以下是一个简单的示例代码,可以生成一个包含“Hello World”文本、页眉页码Word 文档: ```java import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFHeaderFooterPolicy; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFHeader; import org.apache.poi.xwpf.usermodel.XWPFFooter; import org.apache.poi.xwpf.usermodel.ParagraphAlignment; import org.apache.poi.xwpf.usermodel.HeaderFooterType; public class WordGenerator { public static void main(String[] args) throws Exception { // 创建一个新的 Word 文档对象 XWPFDocument doc = new XWPFDocument(); // 创建一个页眉对象,并设置其中的文本和对齐方式 XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT); XWPFParagraph headerParagraph = header.createParagraph(); headerParagraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun headerRun = headerParagraph.createRun(); headerRun.setText("页眉文本"); // 创建一个页脚对象,并设置其中的文本和对齐方式 XWPFFooter footer = doc.createFooter(HeaderFooterType.DEFAULT); XWPFParagraph footerParagraph = footer.createParagraph(); footerParagraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun footerRun = footerParagraph.createRun(); footerRun.setText("第"); footerRun = footerParagraph.createRun(); footerRun.setPageNumber(1); footerRun = footerParagraph.createRun(); footerRun.setText("页"); // 创建一个正文对象,并设置其中的文本 XWPFParagraph paragraph = doc.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText("Hello World"); // 将 Word 文档保存为文件 FileOutputStream out = new FileOutputStream("example.docx"); doc.write(out); out.close(); } } ``` 在上面的代码中,我们首先创建了一个 XWPFDocument 对象,表示一个新的 Word 文档。然后我们创建了一个页眉对象和一个页脚对象,并分别设置其中的文本和对齐方式。接下来,我们创建了一个正文对象,并设置其中的文本。最后,我们将 Word 文档保存为文件。在页脚中,我们使用 setPageNumber 方法来设置当前页码。 请注意,以上示例代码仅供参考,具体实现可以根据实际需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值