Apache POI 使用java 获取当前页码
时间: 2024-05-09 20:17:01 浏览: 135
您好,感谢您的提问。关于Apache POI在Java中获取当前页码的问题,可以通过如下代码实现:
```
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import java.io.*;
public class PageNumber {
public static void main(String[] args) throws Exception {
FileInputStream input_document = new FileInputStream(new File("input.xlsx"));
Workbook workbook = new XSSFWorkbook(input_document);
Sheet sheet = workbook.getSheetAt(0);
Footer footer = sheet.getFooter();
String[] pageNumber = footer.getLeft().split(" ");
System.out.println("当前页码:" + pageNumber[1]);
input_document.close();
}
}
```
其中,XSSFWorkbook是适用于.xlsx文件的Workbook对象,如果您需要处理.xls文件,则需要使用HSSFWorkbook对象。需要注意的是,获取页码前,需要先将当前工作簿的页脚(Footer)中的页码取出并进行处理,代码中实现的是获取页脚左侧的页码,并将其输出至控制台。希望这能对您有所帮助。
阅读全文
相关推荐


















