java工具類之Excel檔案匯入、讀取資料(支援xls、和xlsx)
阿新 • • 發佈:2019-01-03
所需的jar包:
poi的jar包儘量保持一致,不然會報版本不一致的錯誤
下面是程式碼:
package ReadExcel; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.*; import java.util.List; import java.util.Properties; /** * @Author:青松 * @Desc: * @Date: Create in 13:56 2018/5/26 * @Modified by : */ public class ReadExcel_xlsx { private static int count = 0; private static String OutPictruePath = null; public static void main(String[] args) { getUrlByXlsx(getPath()); } /** * 從配置檔案中讀取出Excel檔案的路徑 * * @return */ public static String getPath() { String filePath = null; InputStream in = null; try { Properties properties = new Properties(); in = ReadExcelUtil.class.getClassLoader().getResourceAsStream("util.properties"); properties.load(in); filePath = properties.getProperty("ExcelFilepath"); OutPictruePath = properties.getProperty("OutPicturePath"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) in.close(); } catch (IOException e) { e.printStackTrace(); } } return filePath; } /** * 讀取excel檔案 * * @param filePath excel檔案路徑 */ public static void getUrlByXlsx(String filePath) { InputStream in = null; Row row = null; int rownum = 0; int colnum = 0; String url = null; Workbook workbook = null; try { in = new FileInputStream(new File(filePath)); } catch (FileNotFoundException e) { e.printStackTrace(); } if (in != null && (workbook = getWorkbook(in, filePath)) != null) { int sheet_size = workbook.getNumberOfSheets(); for (int i = 0; i < sheet_size; i++) { Sheet sheet = workbook.getSheetAt(i); rownum = sheet.getPhysicalNumberOfRows();//行數 row = sheet.getRow(0);//獲取第一行,讀取出一共有幾列資料 colnum = row.getPhysicalNumberOfCells();//列數 for (int j = 1; j < rownum; j++) { row = sheet.getRow(j); for (int k = 0; k < colnum; k++) { // Cell cell=row.getCell(k);//獲取單元 // String a=cell.getRichStringCellValue().toString(); // url=cell.getStringCellValue(); // System.out.println("a:"+a+",\n url:"+url); url = (String) getCellFormatValue(row.getCell(k)); System.out.println("url:" + url); } } } } } /** * 判斷單元格的格式(string、int、date...) * * @param cell 單元格 * @return */ 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)) { //轉換為日期格式YYYY-mm-dd cellValue = cell.getDateCellValue(); } else { //數字 cellValue = String.valueOf(cell.getNumericCellValue()); } break; } case Cell.CELL_TYPE_STRING: { cellValue = cell.getRichStringCellValue().getString(); break; } default: cellValue = ""; } } else { cellValue = ""; } return cellValue; } /** * 判斷是xls檔案還是xlsx檔案,並返回相對應的workbook * * @param in * @param filePath * @return */ public static Workbook getWorkbook(InputStream in, String filePath) { if (filePath != null) { try { char[] utlChar = filePath.toCharArray(); char end = utlChar[utlChar.length - 1]; if (end == 's') { return new HSSFWorkbook(in); } else if (end == 'x') { return new XSSFWorkbook(in); } } catch (IOException e) { e.printStackTrace(); } } return null; } }