在一個項目中,有一個需求,是把excel文件的內容轉換為xml格式展示。在學習如何操作的過程中,首先是如何獲取excel文件,其中操作的代碼如下:
1.首先是導入需要的 jar, 下載地址:https://github.com/locationbai/readExcelJar/tree/master/lib

2.代碼實現:
package com.apusic; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; 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; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * Created by baizhuang on 2018-11-20. * * dsccription: 讀取本地的一個excel文檔,並輸出到控制台 */ public class ReadLocalExcel { //總行數 private int totalRows = 0; //總列數 private int totalCells = 0; //錯誤信息 private String errorInfo; private ReadLocalExcel(){ } public int getTotalRows() { return totalRows; } public int getTotalCells() { return totalCells; } public String getErrorInfo() { return errorInfo; } //檢查文件是否為excel或者為空 public boolean validateExcel(String filePath){ //檢查文件格式 if (filePath==null || !(UUtil.isExcel2003(filePath) || UUtil.isExcel2007(filePath))) { errorInfo = "不是excel格式"; return false; } //檢查文件是否存在 File file = new File(filePath); if(file==null || !file.exists()){ errorInfo = "文件不存在"; return false; } return true; } public List<List<String>> read(String filePath){ List<List<String>> dataList = new ArrayList<List<String>>(); InputStream is = null; try { //驗證文件 if(!validateExcel(filePath)){ System.out.println(errorInfo); return null; } //判斷文件類型 boolean isExcel2003 = true; if(UUtil.isExcel2007(filePath)){ isExcel2003 = false; } //調用讀取方法 File file = new File(filePath); is = new FileInputStream(file); dataList = read(is,isExcel2003); is.close(); }catch (Exception e ){ }finally { } return dataList; } public List<List<String>> read(InputStream inputStream,boolean isExcel2003) throws Exception{ List<List<String>> dataLst = null; Workbook wb = null; if(isExcel2003){ wb = new HSSFWorkbook(inputStream); }else { wb = new XSSFWorkbook(inputStream); } dataLst =readWork(wb); return dataLst; } public List<List<String>> readWork(Workbook wb){ List<List<String>> dataList = new ArrayList<List<String>>(); //得到第一個shell Sheet sheet =wb.getSheetAt(0); this.totalRows = sheet.getPhysicalNumberOfRows(); if(this.totalRows>=1 && sheet.getRow(0)!=null){ this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells(); } //循環excel的行 for (int r=0;r<this.totalRows;r++){ Row row = sheet.getRow(r); if(row == null){ continue; } List<String> rowList = new ArrayList<String>(); //循環excel的列 for(int c=0;c<this.getTotalCells();c++){ Cell cell = row.getCell(c); String cellValue = ""; if(null !=cell){ //判斷數據類型 switch (cell.getCellType()){ case HSSFCell.CELL_TYPE_NUMERIC: //數字 cellValue = cell.getNumericCellValue()+""; break; case HSSFCell.CELL_TYPE_STRING: //字符串 cellValue = cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_BOOLEAN: //布爾 cellValue = cell.getBooleanCellValue()+""; break; case HSSFCell.CELL_TYPE_FORMULA: //公式 cellValue = cell.getCellFormula()+""; break; case HSSFCell.CELL_TYPE_BLANK: //空 cellValue = ""; break; case HSSFCell.CELL_TYPE_ERROR: //故障 cellValue = "非法字符"; break; default: cellValue = "未知類型"; break; } } rowList.add(cellValue); } dataList.add(rowList); } return dataList; } public static void main(String[] args){ ReadLocalExcel t = new ReadLocalExcel(); // List<List<String>> list = t.read("D://a.xlsx"); List<List<String>> list = t.read("D:\\a.xlsx"); if(list!=null){ System.out.println("***************************************"); for (int i=0;i<list.size();i++){ System.out.print("第"+i+"行:"); List<String> cellList = list.get(i); for (int j=0;j<cellList.size();j++){ System.out.print(" "+cellList.get(j)); } System.out.println(); } System.out.println("***************************************"); } } } class UUtil{ public static boolean isExcel2003(String filePath){ return filePath.matches("^.+\\.(?i)(xls)$"); } public static boolean isExcel2007(String filePath){ return filePath.matches("^.+\\.(?i)(xlsx)$"); } }
