您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關使用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, filename, startrow, startcol, 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 filename, int startrow, int startcol, int sheetnum) { List<Map<String, String>> varList = new ArrayList<Map<String, 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, String filename, int startrow, int startcol, int sheetnum) { List<Map<String, String>> varList = new ArrayList<Map<String, String>>(); try { File target = new File(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> varpd = new HashMap<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, 0, 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, 0, 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-math4-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>
關于使用java怎么編寫一個讀取excel的工具類就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。