java poi判斷excel是xlsx還是xls類型
java poi判斷excel是xlsx還是xls
依賴
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.5</version>
</dependency>
代碼
package com.test.excel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.nio.file.Files;
public class ExcelRateOneTest {
public static void main(String[] args) {
File file = new File("D:\\年輕干部人才庫名冊.xls");
try {
Workbook workbook = null;
try {
workbook = new XSSFWorkbook(Files.newInputStream(file.toPath()));
} catch (Exception e) {
System.out.println("該excel文件不是xlsx類型"+e.getMessage());
}
try {
if (null == workbook) {
workbook = new HSSFWorkbook(Files.newInputStream(file.toPath()));
}
} catch (Exception e) {
System.out.println("該文件不是excel文件"+e.getMessage());
}
if (workbook != null) {
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
System.out.println("單元格內(nèi)容:" + getCellValue(cell));
}
}catch (Exception e){
System.out.println(e.getMessage());
}
}
/**
* 根據(jù)單元格類型獲取單元格內(nèi)容,將內(nèi)容轉(zhuǎn)為字符串類型
* @param cell 單元格
* @return
*/
public static String getCellValue(Cell cell) {
if (cell == null) {
return "";
}
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue();
case NUMERIC:
return cell.getNumericCellValue() + "";
case BOOLEAN:
return cell.getBooleanCellValue() + "";
case FORMULA:
return cell.getCellFormula() + "";
default:
return "";
}
}
}知識補充
除了上文的內(nèi)容,小編還為大家整理了Java利用poi解析xlsx和xls文件的相關(guān)代碼,希望對大家有所幫助
遇到過處理excel的問題,在網(wǎng)上找了很久,感覺他們的代碼太亂太復(fù)雜,這是我精簡版的excel處理代碼,簡單暴力。
首先,為什么使用poi?jxl只能處理03版之前的excel,也就是xls結(jié)尾的,不能處理xlsx。poi兼容兩種格式,poi解析兩種格式的文件時,唯一的不同就是xls時,為HSSF;xlsx時,為XSSF。
首先是處理Cell對象的小方法:
//這個方法對cell進行處理,傳入cell對象,返回cell中內(nèi)容,String類型。
public static String getCellFormatValue(Cell cell) {
String cellValue = "";
if (cell != null) {
// 判斷cell類型
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: {
cellValue = String.valueOf(cell.getNumericCellValue());
break;
}
case Cell.CELL_TYPE_STRING: {
cellValue = cell.getRichStringCellValue().getString();
break;
}
default:
cellValue = "";
}
}
return cellValue;
}
兩種處理excel的代碼:
xls時:
InputStream inputStream = new FileInputStream("絕對路徑.xls");
HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
HSSFSheet s = workbook.getSheetAt(0);
for (int j = 0; j < s.getPhysicalNumberOfRows(); j++) {//獲取總行數(shù)
Row row = s.getRow(j); // 取出第i行 getRow(index) 獲取第(j)行
for (int k = 0; k < row.getPhysicalNumberOfCells(); k++) { // getPhysicalNumberOfCells() 獲取當(dāng)前行的總列數(shù)
String value1 = getCellFormatValue(row.getCell(k));//取出第j行第k列的值
System.out.println(value1);
}
}
workbook.close();
xlsx時:
InputStream inputStream = new FileInputStream("絕對路徑.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
XSSFSheet s = workbook.getSheetAt(0);
for (int j = 0; j < s.getPhysicalNumberOfRows(); j++) {//獲取總行數(shù)
Row row = s.getRow(j); // 取出第i行 getRow(index) 獲取第(j)行
for (int k = 0; k < row.getPhysicalNumberOfCells(); k++) { // getPhysicalNumberOfCells() 獲取當(dāng)前行的總列數(shù)
String value1 = getCellFormatValue(row.getCell(k));//取出第j行第k列的值
System.out.println(value1);
}
}
workbook.close();
以上就是java poi判斷excel是xlsx還是xls類型的詳細內(nèi)容,更多關(guān)于java判斷excel是xlsx還是xls的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
手把手教你實現(xiàn)Java第三方應(yīng)用登錄
本文主要介紹了手把手教你實現(xiàn)Java第三方應(yīng)用登錄,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
SpringBoot2.0整合Redis自定義注入bean組件配置的實戰(zhàn)教程
這篇文章主要介紹了SpringBoot2.0整合Redis自定義注入bean組件配置,我們將基于SpringBoot2.0整合搭建的微服務(wù)項目為奠基,開啟中間件Redis的實戰(zhàn)之路,需要的朋友可以參考下2023-06-06
spring data jpa @Query注解中delete語句報錯的解決
這篇文章主要介紹了spring data jpa @Query注解中delete語句報錯的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
application.yml和bootstrap.yml不生效的3種解決方案
SpringBoot默認支持?properties(.properties) 和 YAML(.yml .yaml ) 配置文件,本文主要介紹了application.yml和bootstrap.yml不生效的3種解決方案,具有一定的參考價值,感興趣的可以了解一下2024-03-03
Java讀取json數(shù)據(jù)并存入數(shù)據(jù)庫的操作代碼
很多朋友問大佬們JAVA怎么把json存入數(shù)據(jù)庫啊,這一問題就把我難倒了,糾結(jié)如何操作呢,下面小編把我的經(jīng)驗分享給大家,感興趣的朋友一起看看吧2021-08-08

