Count number of worksheets in Excel File

北慕城南 提交于 2020-01-02 10:18:15

问题


How to count number of worksheets in a Microsoft Excel file using Java SE?


回答1:


There's no standard class/library files in Java SE that interfaces with MS Excel. In Apache POI, you can use HSSFWorkbook.getNumberOfSheets() method which returns you the number of worksheet from a workbook.


To open an Excel file and get HSSFWorkbook, do this:

String fileName = "C://Excel.xls";
POIFSFileSystem fileSystem = new POIFSFileSystem(new FileInputStream(fileName));
HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);



回答2:


Use getNumberOfSheets() in the WritableWorkbook class.

Take a look at these:

jxl.Workbook;
jxl.write.Label;
jxl.write.WritableSheet;
jxl.write.WritableWorkbook;

http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/write/WritableWorkbook.html




回答3:


USE THE FOLLOWING CODE TO GET number of worksheets

FileInputStream file = new FileInputStream(new File(FILE PATH));            

XSSFWorkbook workbook = new XSSFWorkbook(file);         

System.out.println("number of sheet::"+ workbook.getNumberOfSheets());



回答4:


You can use xlsx4j to count. This is my code:

public static void main(String[] args) throws InvalidFormatException, Docx4JException {
    SpreadsheetMLPackage spPackage = SpreadsheetMLPackage.load(new File("D:/MyFile.xlsx"));
    List<Sheet> sheetList = spPackage.getWorkbookPart().getJaxbElement().getSheets().getSheet();
    System.out.println("Number of worksheet: "+ sheetList.size());
    System.out.println("Sheet name: ");
    for (Sheet sheet : sheetList) {
        System.out.println(sheet.getName());
    }



回答5:


public int getNumberOfSheets(File)throws Exception{
 FileInputStream fileInputStream = new FileInputStream(file);
 HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
 int number_of_sheets =     workbook.getNumberOfSheets();
 return number_of_sheets
}

Above is a simple method to get the number of sheets in an xls workbook



来源:https://stackoverflow.com/questions/6896435/count-number-of-worksheets-in-excel-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!