package com.tenwa.reckon.util; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFDataFormatter; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.RichTextString; 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 com.amarsoft.are.util.json.JSONEncoder; import com.amarsoft.are.util.json.JSONObject; import com.tenwa.officetempalte.util.ExcelVersionEnum; public class ExcelReader { private static HSSFDataFormatter formatter = new HSSFDataFormatter(); public static List> readExcelDatas(InputStream fis,ExcelVersionEnum excelVersionEnum) throws IOException { List> excelData = new ArrayList>(); if (fis != null) { Workbook wb = null; switch (excelVersionEnum) { case VERSION2007: wb = new XSSFWorkbook(fis); break; default: wb = new HSSFWorkbook(fis); break; } Sheet sheet1 = wb.getSheetAt(0); Row row = sheet1.getRow(0); Map fieldMapping = new HashMap(); Sheet sheet2 = wb.getSheetAt(1); int configRowsNum = sheet2.getLastRowNum(); for (int i = 0; i <= configRowsNum; i++) { String key = sheet2.getRow(i).getCell(0).getStringCellValue(); String value = sheet2.getRow(i).getCell(1).getStringCellValue(); fieldMapping.put(key, value); } int rowNum = sheet1.getLastRowNum(); int colNum = row.getPhysicalNumberOfCells(); Map titles = new HashMap(); int count = 0; for (int i = 0; i < colNum; i++) { if(row.getCell(i) != null){ titles.put(String.valueOf(i), row.getCell(i).getStringCellValue()); }else{ count++; } } colNum -= count; for (int i = 1; i <= rowNum; i++) { Map content = new HashMap(); row = sheet1.getRow(i); if(row == null){ continue; } int j = 0; while (j < colNum) { if(row.getCell(j) == null || getCellFormatValue(row.getCell(j)) == null || getCellFormatValue(row.getCell(j)).trim().equals("")){ j++; continue; } String value = getCellFormatValue(row.getCell(j)); String title = titles.get(String.valueOf(j));//中文标题KEY String field = fieldMapping.get(title); title = field == null ? title : field;//转换成英文的KEY content.put(title, value); j++; } if(!content.isEmpty()){ excelData.add(content); } } } return excelData; } public static List> readExcelDatas(File file) throws IOException { List> excelData = null; if (file != null) { FileInputStream fis = new FileInputStream(file); String suffix = file.getName().substring(file.getName().lastIndexOf(".")+1); excelData = readExcelDatas(fis,ExcelVersionEnum.valueOf(suffix)); fis.close(); } return excelData; } public static String fomartExcelDatasToString(List> datas) throws IOException { JSONObject jsonObject = JSONObject.createObject(); if (datas != null) { JSONObject child = JSONObject.createObject(); for (Map row : datas) { for(String key : row.keySet()){ child.appendElement(key, row.get(key)); } } jsonObject.appendElement(child); } return JSONEncoder.encode(jsonObject); } private static String getCellFormatValue(Cell cell) { String cellvalue = ""; if (cell != null) { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: {// 数字 if (HSSFDateUtil.isCellDateFormatted(cell)) { cellvalue = new SimpleDateFormat("yyyy年MM月dd日").format(cell.getDateCellValue()); }else{ cellvalue = formatter.formatCellValue(cell).trim(); cellvalue = cellvalue.replaceAll("[,|€|(US$)|$|\\s+|\\*]", ""); } break; } case HSSFCell.CELL_TYPE_FORMULA: {// 计算公式 switch (cell.getCachedFormulaResultType()) { case HSSFCell.CELL_TYPE_STRING: { RichTextString str = cell.getRichStringCellValue(); if (str != null) { cellvalue = str.toString(); } break; } case HSSFCell.CELL_TYPE_NUMERIC: { CellStyle style = cell.getCellStyle(); if (style == null) { if (HSSFDateUtil.isCellDateFormatted(cell)) { cellvalue = new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue()); } else { cellvalue = formatter.formatCellValue(cell).trim(); cellvalue = cellvalue.replaceAll("[,|€|(US$)|$|\\s+|\\*]", ""); } }else if("yyyy\"年\"mm\"月\"dd\"日\";@".equals(style.getDataFormatString())){ cellvalue=new SimpleDateFormat("yyyy年MM月dd日").format(cell.getDateCellValue()); } else { cellvalue = formatter.formatRawCellContents(cell.getNumericCellValue(), style.getDataFormat(), style.getDataFormatString()).trim(); cellvalue = cellvalue.replaceAll("[,|€|(US$)|$|\\s+|\\*]", ""); } break; } case HSSFCell.CELL_TYPE_BOOLEAN: {// 布尔 cellvalue = String.valueOf(cell.getBooleanCellValue()); break; } case HSSFCell.CELL_TYPE_ERROR: {// 错误值 cellvalue = ErrorEval.getText(cell.getErrorCellValue()); break; } } break; } case HSSFCell.CELL_TYPE_BOOLEAN: {// 布尔 cellvalue = String.valueOf(cell.getBooleanCellValue()).trim(); break; } case HSSFCell.CELL_TYPE_STRING: {// 文本 cellvalue = cell.getRichStringCellValue().getString().trim(); break; } } } return cellvalue; } }