所需jar包 poijava
package example.poi; import java.io.*; import java.text.DecimalFormat; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.*; public class ImportExcel { private Workbook wb = null; private Sheet sheet = null; private Row row = null; private int sheetNum = 0; private int rowNum = 0; private FileInputStream fis = null; private File file = null; private DecimalFormat df = new DecimalFormat("0"); public ImportExcel() { super(); } public void setSheetNum(int sheetNum) { this.sheetNum = sheetNum; } public void setRowNum(int rowNum) { this.rowNum = rowNum; } public void setFile(File file) { this.file = file; } /** * 讀取excel文件得到HSSFWorkbook對象 * @throws IOException */ public void open(String filePath) throws IOException { if(validateExcel(filePath)) { file = new File(filePath); fis = new FileInputStream(file); if(isExcel2003(filePath)) { wb = new HSSFWorkbook(fis); } else { wb = new XSSFWorkbook(fis); } fis.close(); } } /** * 獲取sheet表數目 * @return sheet表數目 */ public int getSheetCount() { int sheetCount = -1; sheetCount = wb.getNumberOfSheets(); return sheetCount; } /** * 獲取sheetNum下的記錄行數 * @return 記錄行數 */ public int getRowCount() { if(wb == null) { System.err.println("----------->WorkBook爲空"); } Sheet sheet = wb.getSheetAt(this.sheetNum); int rowCount = -1; rowCount = sheet.getLastRowNum(); return rowCount; } /** * 獲取指定sheetNum的記錄行數 * @param sheetNum 表編號 * @return 記錄行數 */ public int getRowCount(int sheetNum) { Sheet sheet = wb.getSheetAt(sheetNum); int rowCount = -1; rowCount = sheet.getLastRowNum(); return rowCount; } /** * 獲得指定行的內容 * @param lineNum 行數 * @return 內容 */ public String[] readExcelLine(int lineNum) { return readExcelLine(this.sheetNum, lineNum); } /** * 獲取指定工做表和行數的內容 * @param sheetNum 表編號 * @param lineNum 行數 * @return 內容 */ public String[] readExcelLine(int sheetNum, int lineNum) { if(sheetNum < 0 || lineNum < 0) { return null; } String[] strExcelLine = null; try { sheet = wb.getSheetAt(sheetNum); row = sheet.getRow(lineNum); int cellCount = row.getLastCellNum(); strExcelLine = new String[cellCount + 1]; for(int i = 0; i <= cellCount; i++) { strExcelLine[i] = readStringExcelCell(lineNum, i); } } catch (Exception e) { e.printStackTrace(); } return strExcelLine; } /** * 獲取指定列的內容 * @param cellNum 列編號 * @return 內容 */ public String readStringExcelCell(int cellNum) { return readStringExcelCell(this.rowNum, cellNum); } /** * 獲取指定行和列編號的內容 * @param rowNum 行編號 * @param cellNum 列編號 * @return 內容 */ public String readStringExcelCell(int rowNum, int cellNum) { return readStringExcelCell(this.sheetNum, rowNum, cellNum); } /** * 獲取指定工做表、行、列的內容 * @param sheetNum 表編號 * @param rowNum 行編號 * @param cellNum 列編號 * @return 內容 */ public String readStringExcelCell(int sheetNum, int rowNum, int cellNum) { if(sheetNum < 0 || rowNum < 0) { return ""; } String strExcelCell = ""; try { sheet = wb.getSheetAt(sheetNum); row = sheet.getRow(rowNum); if(row.getCell(cellNum) != null) { switch(row.getCell(cellNum).getCellType()) { case HSSFCell.CELL_TYPE_FORMULA: strExcelCell = "FORMULA"; break; case HSSFCell.CELL_TYPE_NUMERIC: //strExcelCell = String.valueOf(row.getCell(cellNum).getNumericCellValue()); //防止科學計數,不須要的話能夠用上一行 strExcelCell = decimalFormat(row.getCell(cellNum).getNumericCellValue()); break; case HSSFCell.CELL_TYPE_STRING: strExcelCell = row.getCell(cellNum).getStringCellValue(); break; case HSSFCell.CELL_TYPE_BLANK: strExcelCell = ""; break; default: strExcelCell = ""; break; } } } catch (Exception e) { e.printStackTrace(); } return strExcelCell; } /** * 測試 * @param args */ public static void main(String[] args) { String filePath = "C:\\Users\\Administrator\\Desktop\\工做簿1.xlsx"; ImportExcel ie = new ImportExcel(); try { ie.open(filePath); } catch (IOException e) { e.printStackTrace(); } ie.setSheetNum(0); int count = ie.getRowCount(); for (int i = 0; i <= count; i++) { String[] rows = ie.readExcelLine(i); for (int j = 0; j < rows.length; j++) { System.out.print(rows[j] + " "); } System.out.print("\n"); } } /** * 驗證文件是否存在以及是不是excel文件 * @param filePath * @return 結果 */ public boolean validateExcel(String filePath) { if(filePath == null || ! (isExcel2003(filePath) || isExcel2007(filePath))) { System.err.println("文件不是excel文件!"); return false; } File file = new File(filePath); if(file == null || ! file.exists()) { System.err.println("文件不存在!"); return false; } return true; } /** * 判斷是不是2003版excel * @param filePath 文件路徑 * @return 結果 */ public boolean isExcel2003(String filePath) { return filePath.matches("^.+\\.(?i)(xls)$"); } /** * 判斷是不是2007版excel * @param filePath 文件路徑 * @return 結果 */ public boolean isExcel2007(String filePath) { return filePath.matches("^.+\\.(?i)(xlsx)$"); } /** * 將獲取到的數據類型轉換成String防止科學計數法 * @param decimal 數據 * @return 結果 */ public String decimalFormat(Double decimal) { return df.format(decimal); } }
內容摘自:apache