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.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.InputStream; import java.util.ArrayList; import java.util.List; public class ExcelImportServiceImpl { @Override public String importExcel(InputStream inputStream, String fileName) throws Exception{ String message = "Import success"; boolean isE2007 = false; //判斷是不是excel2007格式 if(fileName.endsWith("xlsx")){ isE2007 = true; } int rowIndex = 0; try { InputStream input = inputStream; //創建輸入流 Workbook wb; //根據文件格式(2003或者2007)來初始化 if(isE2007){ wb = new XSSFWorkbook(input); }else{ wb = new HSSFWorkbook(input); } Sheet sheet = wb.getSheetAt(0); //得到第一個表單 int rowCount = sheet.getLastRowNum()+1; for(int i = 1; i < rowCount;i++){ rowIndex = i; Row row ; for(int j = 0;j<26;j++){ if(isMergedRegion(sheet,i,j)){ System.out.print(getMergedRegionValue(sheet,i,j)+"\t"); }else{ row = sheet.getRow(i); System.out.print(row.getCell(j)+"\t"); } } System.out.print("\n"); } } catch (Exception ex) { message = "Import failed, please check the data in "+rowIndex+" rows "; } return message; } /** * 獲取單元格的值 * @param cell * @return */ public String getCellValue(Cell cell){ if(cell == null) return ""; return cell.getStringCellValue(); } /** * 合併單元格處理,獲取合併行 * @param sheet * @return List<CellRangeAddress> */ public List<CellRangeAddress> getCombineCell(Sheet sheet) { List<CellRangeAddress> list = new ArrayList<>(); //得到一個 sheet 中合併單元格的數量 int sheetmergerCount = sheet.getNumMergedRegions(); //遍歷全部的合併單元格 for(int i = 0; i<sheetmergerCount;i++) { //得到合併單元格保存進list中 CellRangeAddress ca = sheet.getMergedRegion(i); list.add(ca); } return list; } private int getRowNum(List<CellRangeAddress> listCombineCell,Cell cell,Sheet sheet){ int xr = 0; int firstC = 0; int lastC = 0; int firstR = 0; int lastR = 0; 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) { xr = lastR; } } } return xr; } /** * 判斷單元格是否爲合併單元格,是的話則將單元格的值返回 * @param listCombineCell 存放合併單元格的list * @param cell 須要判斷的單元格 * @param sheet sheet * @return */ public String isCombineCell(List<CellRangeAddress> listCombineCell,Cell cell,Sheet sheet) throws Exception{ int firstC = 0; int lastC = 0; int firstR = 0; int lastR = 0; String cellValue = null; 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); cellValue = getCellValue(fCell); break; } } else { cellValue = ""; } } return cellValue; } /** * 獲取合併單元格的值 * @param sheet * @param row * @param column * @return */ public String getMergedRegionValue(Sheet sheet ,int row , int column){ int sheetMergeCount = sheet.getNumMergedRegions(); for(int i = 0 ; i < sheetMergeCount ; i++){ CellRangeAddress ca = sheet.getMergedRegion(i); int firstColumn = ca.getFirstColumn(); int lastColumn = ca.getLastColumn(); int firstRow = ca.getFirstRow(); int lastRow = ca.getLastRow(); if(row >= firstRow && row <= lastRow){ if(column >= firstColumn && column <= lastColumn){ Row fRow = sheet.getRow(firstRow); Cell fCell = fRow.getCell(firstColumn); return getCellValue(fCell) ; } } } return null ; } /** * 判斷指定的單元格是不是合併單元格 * @param sheet * @param row 行下標 * @param column 列下標 * @return */ private 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; } }