如何使用Java SE计算Microsoft Excel文件中的工作表数?
答案 0 :(得分:6)
Java SE中没有与MS Excel连接的标准类/库文件。在Apache POI中,您可以使用HSSFWorkbook.getNumberOfSheets()
方法返回工作簿中工作表的数量。
要打开Excel文件并获取HSSFWorkbook
,请执行以下操作:
String fileName = "C://Excel.xls";
POIFSFileSystem fileSystem = new POIFSFileSystem(new FileInputStream(fileName));
HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);
答案 1 :(得分:1)
在getNumberOfSheets()
课程中使用WritableWorkbook
。
看看这些:
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
答案 2 :(得分:1)
使用以下代码获取工作表数量
FileInputStream file = new FileInputStream(new File(FILE PATH));
XSSFWorkbook workbook = new XSSFWorkbook(file);
System.out.println("number of sheet::"+ workbook.getNumberOfSheets());
答案 3 :(得分:0)
您可以使用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());
}
答案 4 :(得分:0)
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
}
以上是获取xls工作簿中的工作表数量的简单方法