java 计算 Excel 文件中的工作表数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6896435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:48:03  来源:igfitidea点击:

Count number of worksheets in Excel File

javaexcelexcel-2003

提问by Shumon Saha

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

如何使用 Java SE 计算 Microsoft Excel 文件中的工作表数量?

回答by Buhake Sindi

There's no standardclass/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.

有没有标准类/库文件在Java SE与MS Excel的界面。在Apache POI 中,您可以使用HSSFWorkbook.getNumberOfSheets()从工作簿返回工作表数量的方法。



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

要打开 Excel 文件并获取HSSFWorkbook,请执行以下操作:

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

回答by Sherif elKhatib

Use getNumberOfSheets()in the WritableWorkbookclass.

getNumberOfSheets()WritableWorkbook课堂上使用。

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

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

回答by Nitesh Gadekar

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());

回答by Lê Quang Duy

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

您可以使用xlsx4j进行计数。这是我的代码:

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());
    }

回答by Muyinda Rogers

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

以上是获取xls工作簿中工作表数量的简单方法