1. 程式人生 > >excel工具類

excel工具類

pom.xml
<!--利用poi 讀取excel-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.16</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.16</version>
        </dependency>


        <!-- https://
mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>

 



ExcelUtils.java
package
com.kakaluote.common.utils; import org.apache.commons.io.IOUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.util.StringUtils;
import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * Created by 敲程式碼的卡卡羅特 * on 2018/11/19 22:12. */ public class ExcelUtils { //思路是按照Workbook,Sheet,Row,Cell一層一層往下讀取。首先是初始化Workbook public static Workbook getReadWorkBookType(String filePath) throws Exception{ //xls-2003, xlsx-2007 FileInputStream is = null; try { is = new FileInputStream(filePath); if (filePath.toLowerCase().endsWith("xlsx")) { return new XSSFWorkbook(is); } else if (filePath.toLowerCase().endsWith("xls")) { return new HSSFWorkbook(is); } else { // 丟擲自定義的業務異常 } } catch (IOException e) { } finally { IOUtils.closeQuietly(is); } return null; } public static List<String> readExcel(String sourceFilePath) throws Exception { Workbook workbook = null; try { workbook = getReadWorkBookType(sourceFilePath); List<String> contents = new ArrayList<>(); //獲取第一個sheet Sheet sheet = workbook.getSheetAt(0); //第0行是表名,忽略,從第二行開始讀取 System.out.println(sheet.getLastRowNum()); for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) { Row row = sheet.getRow(rowNum); Cell cell=null; short lastCellNum = row.getLastCellNum(); for (int j=0;j<lastCellNum;j++){ cell = row.getCell(j); System.out.println(getCellVal(cell).trim()); } contents.add(getCellVal(cell).trim()); } return contents; }catch (Exception e){ System.out.println(e.getMessage()); } return null; } /** * 獲取單元格的值 * * @param cell * @return */ //如果excel中的資料是數字,會發現java中對應的變成了科學計數法的,所以在獲取值的時候就要做一些特殊處理,這樣就能保證獲取的值是我想要的值。 public static String getCellVal(Cell cell) { CellType cellType = cell.getCellTypeEnum(); //System.out.println(cell.getStringCellValue()); switch (cellType) { case NUMERIC: return cell.getNumericCellValue()+""; case STRING: return cell.getStringCellValue(); case BOOLEAN: return String.valueOf(cell.getBooleanCellValue()); case FORMULA: return cell.getCellFormula(); case BLANK: return ""; case ERROR: return String.valueOf(cell.getErrorCellValue()); default: return ""; } } /** * 判斷是不是空白行 false表示是空白行,true表示不是空白行 * @param row * @return */ public static boolean isBlankLine(Row row){ boolean flag=true; int i=0; if (row.getLastCellNum()==-1){ flag=false; return flag; } for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) { Cell cell = row.getCell(c); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK){ i++; if (i==row.getLastCellNum()){ flag=false; return flag; } } } return flag; } /** * 判斷指定的單元格是否是合併單元格 * * @param sheet * 工作表 * @param row * 行下標 * @param column * 列下標 * @return */ public static boolean isMergedRegion(Sheet sheet, int row, int column) { int sheetMergeCount = sheet.getNumMergedRegions(); for (int i = 0; i < sheetMergeCount; i++) { CellRangeAddress range = sheet.getMergedRegion(i); int firstColumn = range.getFirstColumn(); int lastColumn = range.getLastColumn(); int firstRow = range.getFirstRow(); int lastRow = range.getLastRow(); if (row >= firstRow && row <= lastRow) { if (column >= firstColumn && column <= lastColumn) { return true; } } } return false; } /** * 讀合併單元格值 * @param sheet 表格 * @param cell cell * @return value */ public static String getMergedCellValue(Sheet sheet, Cell cell){ int firstC; int lastC; int firstR; int lastR; String value = ""; List<CellRangeAddress> listCombineCell = sheet.getMergedRegions(); for (CellRangeAddress ca : listCombineCell) { // 獲得合併單元格的起始行, 結束行, 起始列, 結束列 firstC = ca.getFirstColumn(); lastC = ca.getLastColumn(); firstR = ca.getFirstRow(); lastR = ca.getLastRow(); if (cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) { if (cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) { // 獲取合併單元格左上角單元格值 Row fRow = sheet.getRow(firstR); Cell fCell = fRow.getCell(firstC); value = getCellVal(fCell); } } } return value; } public static void main(String[] arg){ // String path = "C:\\Users\\Administrator\\Desktop\\1.xlsx"; // try { // List<String> tenantIds = readExcel(path); //// for (String s:tenantIds){ //// System.out.println(s); //// } // } catch (Exception e) { // // } String s="0.0"; System.out.println("0.0".equals(s)); } }