以下是我用xlsx
和xls
格式
File customerTemplateFileObj = new File(customerTemplateFullPath);
InputStream inputStream = new FileInputStream(customerTemplateFileObj);
Workbook myWorkBook = null;
try {
***myWorkBook = WorkbookFactory.create(inputStream);***
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int totalSheets = myWorkBook.getNumberOfSheets();
我的代码适用于xls
格式,但xlsx
停止在
myWorkBook = WorkbookFactory.create(inputStream);
没有任何异常。
答案 0 :(得分:1)
要在Java中读取xlsx文件,请使用poi jar中的XSSFWorkbook类
package com.ssaurel.samples.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcel {
public static void main(String[] args) throws IOException {
File excelFile = new File("contacts.xlsx");
FileInputStream fis = new FileInputStream(excelFile);
// we create an XSSF Workbook object for our XLSX Excel File
XSSFWorkbook workbook = new XSSFWorkbook(fis);
// ...
}
}