org.apache.poi.poifs.filesystem.OfficeXmlFileException: 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)
緣由是:
HSSFWorkbook:是操做Excel2003之前(包括2003)的版本,擴展名是.xls
XSSFWorkbook:是操做Excel2007的版本,擴展名是.xlsxjava
<!--處理2003 excel--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.16</version> </dependency> <!--處理2007 excel--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.16</version> </dependency>
在使用POI讀取Excel文件內容時,發生了異常,報錯以下:apache
java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell 異常怎麼辦?app
大概意思是不能從一個數值的列獲取一個字符串類型的值,我使用下面的代碼來獲取單元格的值:this
解決方法是在讀取某單元格時,使用setCellType()方法先將該單元格的類型設置爲STRING,代碼以下:spa
cell.setCellType(CellType.STRING);
String stringCellValue = cell.getStringCellValue();
poi建立excel 和讀取excel (2003版本)excel
建立code
import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.junit.Test; import java.io.FileOutputStream; import java.io.IOException; public class ordingTest { @Test public void createOrding() throws IOException { HSSFWorkbook work = new HSSFWorkbook(); HSSFSheet mysheet = work.createSheet("mysheet"); HSSFRow row = mysheet.createRow(0); HSSFRow row2 = mysheet.createRow(1); HSSFRow row3 = mysheet.createRow(2); row.createCell(0).setCellValue("name"); row.createCell(1).setCellValue("age"); row.createCell(2).setCellValue("adress"); row2.createCell(0).setCellValue("張三"); row2.createCell(1).setCellValue(13); row2.createCell(2).setCellValue("上海"); row3.createCell(0).setCellValue("李四"); row3.createCell(1).setCellValue(16); row3.createCell(2).setCellValue("北京"); FileOutputStream fileOutputStream = new FileOutputStream("E:\\hello.xls"); work.write(fileOutputStream); fileOutputStream.flush(); work.close(); fileOutputStream.close(); } }
讀取:xml
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class ordingTest2 {
@Test
public void reading() throws IOException {
InputStream inputStream = new FileInputStream("E:\\hello.xls") {
};
HSSFWorkbook sheets = new HSSFWorkbook(inputStream);
HSSFSheet sheetAt = sheets.getSheetAt(0);
for (Row cells : sheetAt) {
for (Cell cell : cells) {
cell.setCellType(CellType.STRING);
String stringCellValue = cell.getStringCellValue();
System.out.println("stringCellValue = " + stringCellValue);
}
}
sheets.close();
}
}
第二中讀取方法:blog
/*從最後一行讀取,起始索引0*/ int lastRowNum = sheetAt.getLastRowNum(); for (int i = 0; i <=lastRowNum ; i++) { HSSFRow row = sheetAt.getRow(i); short lastCellNum = row.getLastCellNum(); for (int j = 0; j < lastCellNum; j++) { HSSFCell cell = row.getCell(j); cell.setCellType(CellType.STRING); String stringCellValue = cell.getStringCellValue(); System.out.println("stringCellValue = " + stringCellValue); } }