java解析Excel的方法(xls、xlsx兩種格式)
一、需要導(dǎo)入的jar
1.commons-collections4-4.1.jar
2.poi-3.17-beta1.jar
3.poi-ooxml-3.17-beta1.jar
4.poi-ooxml-schemas-3.17-beta1.jar
5.xmlbeans-2.6.0.jar
二、主要API
1.import org.apache.poi.ss.usermodel.Workbook,對(duì)應(yīng)Excel文檔;
2.import org.apache.poi.hssf.usermodel.HSSFWorkbook,對(duì)應(yīng)xls格式的Excel文檔;
3.import org.apache.poi.xssf.usermodel.XSSFWorkbook,對(duì)應(yīng)xlsx格式的Excel文檔;
4.import org.apache.poi.ss.usermodel.Sheet,對(duì)應(yīng)Excel文檔中的一個(gè)sheet;
5.import org.apache.poi.ss.usermodel.Row,對(duì)應(yīng)一個(gè)sheet中的一行;
6.import org.apache.poi.ss.usermodel.Cell,對(duì)應(yīng)一個(gè)單元格。
三、代碼如下
package poi;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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;
public class Testpoi {
public static void main(String[] args) {
Workbook wb =null;
Sheet sheet = null;
Row row = null;
List<Map<String,String>> list = null;
String cellData = null;
String filePath = "D:\\test.xlsx";
String columns[] = {"name","age","score"};
wb = readExcel(filePath);
if(wb != null){
//用來存放表中數(shù)據(jù)
list = new ArrayList<Map<String,String>>();
//獲取第一個(gè)sheet
sheet = wb.getSheetAt(0);
//獲取最大行數(shù)
int rownum = sheet.getPhysicalNumberOfRows();
//獲取第一行
row = sheet.getRow(0);
//獲取最大列數(shù)
int colnum = row.getPhysicalNumberOfCells();
for (int i = 1; i<rownum; i++) {
Map<String,String> map = new LinkedHashMap<String,String>();
row = sheet.getRow(i);
if(row !=null){
for (int j=0;j<colnum;j++){
cellData = (String) getCellFormatValue(row.getCell(j));
map.put(columns[j], cellData);
}
}else{
break;
}
list.add(map);
}
}
//遍歷解析出來的list
for (Map<String,String> map : list) {
for (Entry<String,String> entry : map.entrySet()) {
System.out.print(entry.getKey()+":"+entry.getValue()+",");
}
System.out.println();
}
}
//讀取excel
public static Workbook readExcel(String filePath){
Workbook wb = null;
if(filePath==null){
return null;
}
String extString = filePath.substring(filePath.lastIndexOf("."));
InputStream is = null;
try {
is = new FileInputStream(filePath);
if(".xls".equals(extString)){
return wb = new HSSFWorkbook(is);
}else if(".xlsx".equals(extString)){
return wb = new XSSFWorkbook(is);
}else{
return wb = null;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return wb;
}
public static Object getCellFormatValue(Cell cell){
Object cellValue = null;
if(cell!=null){
//判斷cell類型
switch(cell.getCellType()){
case Cell.CELL_TYPE_NUMERIC:{
cellValue = String.valueOf(cell.getNumericCellValue());
break;
}
case Cell.CELL_TYPE_FORMULA:{
//判斷cell是否為日期格式
if(DateUtil.isCellDateFormatted(cell)){
//轉(zhuǎn)換為日期格式Y(jié)YYY-mm-dd
cellValue = cell.getDateCellValue();
}else{
//數(shù)字
cellValue = String.valueOf(cell.getNumericCellValue());
}
break;
}
case Cell.CELL_TYPE_STRING:{
cellValue = cell.getRichStringCellValue().getString();
break;
}
default:
cellValue = "";
}
}else{
cellValue = "";
}
return cellValue;
}
}
四、運(yùn)行結(jié)果
代碼運(yùn)行前保證在D盤下有一個(gè)test.xlsx文檔,不然報(bào)文件找不到異常;Excel文檔中的表頭要和代碼中的String columns[] = {"name","age","score"}對(duì)應(yīng)起來。

總結(jié)
以上所述是小編給大家介紹的java解析Excel的方法(xls、xlsx兩種格式),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
spring中BeanPostProcessor的作用和使用注意事項(xiàng)
在Spring框架中,BeanPostProcessor?是一個(gè)核心擴(kuò)展接口,允許你在Bean實(shí)例化的過程中插入自定義邏輯,本文給大家介紹spring中BeanPostProcessor的作用,感興趣的朋友一起看看吧2025-04-04
Java如何使用JWT實(shí)現(xiàn)Token認(rèn)證機(jī)制
JWT(JSON Web Token)是一種用于在網(wǎng)絡(luò)上安全地傳輸信息的簡(jiǎn)潔的、URL 安全的表示方法,本文主要介紹了Java如何使用JWT實(shí)現(xiàn)Token認(rèn)證機(jī)制,需要的可以參考下2024-10-10
SpringCloud實(shí)現(xiàn)文件上傳功能的方法詳解
這篇文章主要為大家詳細(xì)介紹了SpringCloud如何實(shí)現(xiàn)文件上傳功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的借鑒價(jià)值,需要的可以參考一下2022-08-08
Spring Boot 實(shí)現(xiàn)https ssl免密登錄(X.509 pki登錄)
這篇文章主要介紹了Spring Boot 實(shí)現(xiàn)https ssl免密登錄(X.509 pki登錄),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Spring Security 實(shí)現(xiàn)用戶名密碼登錄流程源碼詳解
在服務(wù)端的安全管理使用了Spring Security,用戶登錄成功之后,Spring Security幫你把用戶信息保存在Session里,但是具體保存在哪里,要是不深究你可能就不知道,今天小編就帶大家具體了解一下Spring Security實(shí)現(xiàn)用戶名密碼登錄的流程2021-11-11
Java批量插入數(shù)據(jù)的代碼實(shí)現(xiàn)
日常工作或者學(xué)習(xí)中,可能會(huì)遇到批量插入數(shù)據(jù)的需求,一般情況下數(shù)據(jù)量少的時(shí)候,我們會(huì)直接調(diào)用批量接口插入數(shù)據(jù)即可,當(dāng)數(shù)據(jù)量特別大時(shí),我們就會(huì)用到分批插入數(shù)據(jù),所以本文給大家介紹了Java批量插入數(shù)據(jù)的代碼實(shí)現(xiàn),需要的朋友可以參考下2024-01-01

