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