我尝试过POI版本 1.poi-src-3.9-20121203也有 2.poi彬3.10-beta2-20130904
但无法读取.xls文件 低于错误 “ org.apache.poi.POIXMLException:org.apache.poi.openxml4j.exceptions.InvalidFormatException:包应包含内容类型部分[M1.13]“
注意:我能够阅读.xlsx文件。
答案 0 :(得分:0)
这适用于.xlsx
try {
// Get the workbook object for XLSX file
XSSFWorkbook wBook = new XSSFWorkbook(new FileInputStream(path));
// Get first sheet from the workbook
XSSFSheet sheet = (XSSFSheet) wBook.getSheetAt(0);
XSSFRow row;
XSSFCell cell;
// Iterate through each rows from first sheet
Iterator<org.apache.poi.ss.usermodel.Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
StringBuffer data = new StringBuffer();
row = (XSSFRow) rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
int count = 0;
while (cellIterator.hasNext()) {
count++;
cell = (XSSFCell) cellIterator.next();
if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
data.append(cell.getRawValue()+ ";");
}else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN)
{
data.append(cell.getBooleanCellValue() + ";");
} else if(cell.getCellType() == Cell.CELL_TYPE_STRING)
{
data.append(cell.getStringCellValue() + ";");
}
else if(cell.getCellType() == Cell.CELL_TYPE_BLANK)
{
data.append("" + ";");
}
else
{
data.append(cell + ";");
}
}
String finalStr = data.toString().substring(0, (data.length()-1));
System.out.print(finalStr);
}
} catch (Exception ioe) {
ioe.printStackTrace();
}
这适用于.xls
try {
// Get the workbook object for XLS file
HSSFWorkbook wBook = new HSSFWorkbook(new FileInputStream(path));
// Get first sheet from the workbook
HSSFSheet sheet = (HSSFSheet) wBook.getSheetAt(0);
HSSFRow row;
HSSFCell cell;
// Iterate through each rows from first sheet
Iterator<org.apache.poi.ss.usermodel.Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
StringBuffer data = new StringBuffer();
row = (HSSFRow) rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
int count = 0;
while (cellIterator.hasNext()) {
count++;
cell = (HSSFCell) cellIterator.next();
if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
data.append(cell.getRawValue()+ ";");
}else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN)
{
data.append(cell.getBooleanCellValue() + ";");
} else if(cell.getCellType() == Cell.CELL_TYPE_STRING)
{
data.append(cell.getStringCellValue() + ";");
}
else if(cell.getCellType() == Cell.CELL_TYPE_BLANK)
{
data.append("" + ";");
}
else
{
data.append(cell + ";");
}
}
String finalStr = data.toString().substring(0, (data.length()-1));
System.out.print(finalStr);
}
} catch (Exception ioe) {
ioe.printStackTrace();
}
答案 1 :(得分:0)
尝试使用org.apache.poi.ss.usermodel.Workbook
。它可以用于.xls和.xlsx文件
Workbook workbook = null;
if (suffix.equalsIgnoreCase("xls")) {
workbook = (Workbook) new HSSFWorkbook(new POIFSFileSystem(
new FileInputStream(assetFile)));
} else if (suffix.equalsIgnoreCase("xlsx")) {
InputStream inp = new FileInputStream(assetFile);
workbook = new XSSFWorkbook(inp);
inp.close();
}
答案 2 :(得分:0)
你需要poi-ooxml来读取xlsx格式。 添加此Maven依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
对于xls使用
Workbook = new HSSFWorkbook(new FileInputStream(new File("fileName")));
对于xlsx使用
Workbook = workbook = new XSSFWorkbook(new FileInputStream(new File("fileName")));
如果您在阅读之前不知道文件格式,有时查找.xls或.xlsx文件扩展名可能不总是足以找到实际的文件格式。在这种情况下,请这样做
Workbook workbook;
try {
workbook = new HSSFWorkbook(new FileInputStream(new File("fileName")));
System.out.println(" Reading XLS file");
} catch (OfficeXmlFileException e) {
System.out.println(" Reading XLSX file");
workbook = new XSSFWorkbook(new FileInputStream(new File("fileName")));
}
休息是完全相同的.xls和.xlsx
通过自动检测输入创建工作簿的更简单方法是
Workbook workbook = WorkbookFactory.create(new File("fileName"));