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,對應(yīng)Excel文檔;
2.import org.apache.poi.hssf.usermodel.HSSFWorkbook,對應(yīng)xls格式的Excel文檔;
3.import org.apache.poi.xssf.usermodel.XSSFWorkbook,對應(yīng)xlsx格式的Excel文檔;
4.import org.apache.poi.ss.usermodel.Sheet,對應(yīng)Excel文檔中的一個sheet;
5.import org.apache.poi.ss.usermodel.Row,對應(yīng)一個sheet中的一行;
6.import org.apache.poi.ss.usermodel.Cell,對應(yīng)一個單元格。
三、代碼如下
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>>();
//獲取第一個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盤下有一個test.xlsx文檔,不然報文件找不到異常;Excel文檔中的表頭要和代碼中的String columns[] = {"name","age","score"}對應(yīng)起來。

總結(jié)
以上所述是小編給大家介紹的java解析Excel的方法(xls、xlsx兩種格式),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
JavaWeb評論功能實(shí)現(xiàn)步驟以及代碼實(shí)例
項(xiàng)目初始版本上線,有時間寫點(diǎn)東西記錄一下項(xiàng)目中的心得體會,通過這個項(xiàng)目學(xué)習(xí)了很多,要寫下來的有很多,先從評論功能開始吧,下面這篇文章主要給大家介紹了關(guān)于JavaWeb評論功能實(shí)現(xiàn)步驟以及代碼的相關(guān)資料,需要的朋友可以參考下2023-01-01
idea中打開項(xiàng)目時import project和open區(qū)別詳解
本文主要介紹了idea中打開項(xiàng)目時import project和open區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
java實(shí)現(xiàn)簡單登錄界面的實(shí)戰(zhàn)過程
學(xué)習(xí)JAVA的過程當(dāng)中,登陸界面是多數(shù)人第一個實(shí)現(xiàn)的小程序,下面這篇文章主要給大家介紹了關(guān)于利用java實(shí)現(xiàn)簡單登錄界面的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
java配置多個過濾器優(yōu)先級以及幾個常用過濾器操作
這篇文章主要介紹了java配置多個過濾器優(yōu)先級以及幾個常用過濾器的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
java實(shí)現(xiàn)memcache服務(wù)器的示例代碼
本篇文章主要介紹了java實(shí)現(xiàn)memcache服務(wù)器的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
打開.properties中文顯示unicode編碼問題以及解決
這篇文章主要介紹了打開.properties中文顯示unicode編碼問題以及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

