java讀取簡單excel通用工具類
阿新 • • 發佈:2021-01-04
本文例項為大家分享了java讀取簡單excel通用工具類的具體程式碼,供大家參考,具體內容如下
讀取excel通用工具類
import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * 讀取Excel通用工具類 * @author zql */ public class ReadExcel { /** * 讀取Excel * * @param filepath 檔案路徑 * @param filename 檔名,包括副檔名 * @param startrow 開始行號,索引從0開始 * @param startcol 開始列號,索引從0開始 * @param sheetnum 工作簿,索引從0開始 * @return */ public List<Map<String,String>> readExcel(String filepath,String filename,int startrow,int startcol,int sheetnum) { List<Map<String,String>> varList = new ArrayList<Map<String,String>>(); String suffix = filename.substring(filename.lastIndexOf(".") + 1); if ("xls".equals(suffix)) { varList = readExcel2003(filepath,filename,startrow,startcol,sheetnum); } else if ("xlsx".equals(suffix)) { varList = readExcel2007(filepath,sheetnum); } else { System.out.println("Only excel files with XLS or XLSX suffixes are allowed to be read!"); return null; } return varList; } /** * 讀取2003Excel * * @param filepath 檔案路徑 * @param filename 檔名,包括副檔名 * @param startrow 開始行號,索引從0開始 * @param startcol 開始列號,索引從0開始 * @param sheetnum 工作簿,索引從0開始 * @return */ public List<Map<String,String>> readExcel2003(String filepath,String>>(); try { File target = new File(filepath,filename); FileInputStream fis = new FileInputStream(target); HSSFWorkbook wb = new HSSFWorkbook(fis); fis.close(); // sheet 從0開始 HSSFSheet sheet = wb.getSheetAt(sheetnum); // 取得最後一行的行號 int rowNum = sheet.getLastRowNum() + 1; HSSFRow rowTitle = sheet.getRow(0); // 標題行的最後一個單元格位置 int cellTitleNum = rowTitle.getLastCellNum(); String[] title = new String[cellTitleNum]; for (int i = startcol; i < cellTitleNum; i++) { HSSFCell cell = rowTitle.getCell(Short.parseShort(i + "")); if (cell != null) { cell.setCellType(CellType.STRING); title[i] = cell.getStringCellValue(); } else { title[i] = ""; } } // 行迴圈開始 for (int i = startrow + 1; i < rowNum; i++) { Map<String,String> varpd = new HashMap<String,String>(); // 行 HSSFRow row = sheet.getRow(i); // 列迴圈開始 for (int j = startcol; j < cellTitleNum; j++) { HSSFCell cell = row.getCell(Short.parseShort(j + "")); String cellValue = ""; if (cell != null) { // 把型別先設定為字串型別 cell.setCellType(CellType.STRING); cellValue = cell.getStringCellValue(); } varpd.put(title[j],cellValue); } varList.add(varpd); } wb.close(); } catch (Exception e) { System.out.println(e); } return varList; } /** * 讀取2007Excel * * @param filepath 檔案路徑 * @param filename 檔名,包括副檔名 * @param startrow 開始行號,索引從0開始 * @param startcol 開始列號,索引從0開始 * @param sheetnum 工作簿,索引從0開始 * @return */ public List<Map<String,String>> readExcel2007(String filepath,filename); InputStream ins = new FileInputStream(target); XSSFWorkbook wb = new XSSFWorkbook(ins); ins.close(); // 得到Excel工作表物件 XSSFSheet sheet = wb.getSheetAt(sheetnum); // 取得最後一行的行號 int rowNum = sheet.getLastRowNum() + 1; XSSFRow rowTitle = sheet.getRow(0); int cellTitleNum = rowTitle.getLastCellNum(); String[] title = new String[cellTitleNum]; for (int i = startcol; i < cellTitleNum; i++) { XSSFCell cell = rowTitle.getCell(Short.parseShort(i + "")); if (cell != null) { // 把型別先設定為字串型別 cell.setCellType(CellType.STRING); title[i] = cell.getStringCellValue(); } else { title[i] = ""; } } // 行迴圈開始 for (int i = startrow + 1; i < rowNum; i++) { Map<String,String>(); // 得到Excel工作表的行 XSSFRow row = sheet.getRow(i); // 列迴圈開始 for (int j = startcol; j < cellTitleNum; j++) { // 得到Excel工作表指定行的單元格 XSSFCell cell = row.getCell(j); String cellValue = ""; if (cell != null) { // 把型別先設定為字串型別 cell.setCellType(CellType.STRING); cellValue = cell.getStringCellValue(); } varpd.put(title[j],cellValue); } varList.add(varpd); } wb.close(); } catch (Exception e) { System.out.println(e); } return varList; } }
讀取excel通用工具示例測試類
import java.util.List; import java.util.Map; /** * @author zql * */ public class ReadExcelTest { public static void main(String[] args) throws Exception { ReadExcel r = new ReadExcel(); List<Map<String,String>> list = r.readExcel("e:\\excel","測試表格.xls",0); if (list != null) { for (int i = 0; i < list.size(); i++) { Map<String,String> m = list.get(i); m.forEach((key,value) -> { System.out.println(key + ":" + value); }); System.out.println(); } } List<Map<String,String>> lists = r.readExcel("e:\\excel","測試表格.xlsx",0); if (lists != null) { for (int i = 0; i < lists.size(); i++) { Map<String,String> m = lists.get(i); m.forEach((key,value) -> { System.out.println(key + ":" + value); }); System.out.println(); } } } }
普通專案需要引入的包
poi-4.0.1.jar
poi-ooxml-4.0.1.jar
poi-ooxml-schemas-4.0.1.jar
commons-codec-1.11.jar
commons-collections4-4.3.jar
commons-math3-3.6.1.jar
xmlbeans-3.0.2.jar
commons-compress-1.18.jar
curvesapi-1.06.jar
maven專案依賴
<!-- poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.1</version> </dependency>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。