这是我用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);
没有任何例外.
最佳答案 在java中读取xlsx文件extion使用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);
// ...
}
}