java读取excel报错
importjava.io.FileInputStream;importorg.apache.poi.xssf.usermodel.XSSFCell;importorg....
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelTest {
public static void main(String[] args) {
String fileToBeRead = "F:\\excel.xls";
try {
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileToBeRead)); // 创建对Excel工作簿文件的引用
XSSFSheet sheet = workbook.getSheet("test"); // 创建对工作表的引用
int rows = sheet.getPhysicalNumberOfRows();//获取表格的
for (int r = 0; r < rows; r++) { //循环遍历表格的行
String value = "";
XSSFRow row = sheet.getRow(r); //获取单元格中指定的行对象
if (row != null) {
int cells = row.getPhysicalNumberOfCells();//获取单元格中指定列对象
for (short c = 0; c < cells; c++) { //循环遍历单元格中的列
XSSFCell cell = row.getCell((short) c); //获取指定单元格中的列
if (cell != null) {
if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) { //判断单元格的值是否为字符串类型
value += cell.getStringCellValue() + ",";
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { //判断单元格的值是否为数字类型
value += cell.getNumericCellValue() + ",";
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) { //判断单元格的值是否为布尔类型
value += cell.getStringCellValue() + ",";
}
}
}
}
String[] str = value.split(",");
System.out.println(value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
报如下错误:The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF) 展开
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelTest {
public static void main(String[] args) {
String fileToBeRead = "F:\\excel.xls";
try {
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileToBeRead)); // 创建对Excel工作簿文件的引用
XSSFSheet sheet = workbook.getSheet("test"); // 创建对工作表的引用
int rows = sheet.getPhysicalNumberOfRows();//获取表格的
for (int r = 0; r < rows; r++) { //循环遍历表格的行
String value = "";
XSSFRow row = sheet.getRow(r); //获取单元格中指定的行对象
if (row != null) {
int cells = row.getPhysicalNumberOfCells();//获取单元格中指定列对象
for (short c = 0; c < cells; c++) { //循环遍历单元格中的列
XSSFCell cell = row.getCell((short) c); //获取指定单元格中的列
if (cell != null) {
if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) { //判断单元格的值是否为字符串类型
value += cell.getStringCellValue() + ",";
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { //判断单元格的值是否为数字类型
value += cell.getNumericCellValue() + ",";
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) { //判断单元格的值是否为布尔类型
value += cell.getStringCellValue() + ",";
}
}
}
}
String[] str = value.split(",");
System.out.println(value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
报如下错误:The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF) 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询