apzl_leasing/calc/com/tenwa/reckon/util/ExcelReader.java
2018-06-03 22:26:41 +08:00

186 lines
6.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<Map<String, String>> readExcelDatas(InputStream fis,ExcelVersionEnum excelVersionEnum) throws IOException {
List<Map<String, String>> excelData = new ArrayList<Map<String, String>>();
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<String, String> fieldMapping = new HashMap<String, String>();
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<String, String> titles = new HashMap<String, String>();
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<String, String> content = new HashMap<String, String>();
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));//<2F><><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>KEY
String field = fieldMapping.get(title);
title = field == null ? title : field;//ת<><D7AA><EFBFBD><EFBFBD>Ӣ<EFBFBD>ĵ<EFBFBD>KEY
content.put(title, value);
j++;
}
if(!content.isEmpty()){
excelData.add(content);
}
}
}
return excelData;
}
public static List<Map<String, String>> readExcelDatas(File file) throws IOException {
List<Map<String, String>> 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<Map<String, String>> datas) throws IOException {
JSONObject jsonObject = JSONObject.createObject();
if (datas != null) {
JSONObject child = JSONObject.createObject();
for (Map<String, String> 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: {// <20><><EFBFBD><EFBFBD>
if (HSSFDateUtil.isCellDateFormatted(cell)) {
cellvalue = new SimpleDateFormat("yyyy<EFBFBD><EFBFBD>MM<EFBFBD><EFBFBD>dd<EFBFBD><EFBFBD>").format(cell.getDateCellValue());
}else{
cellvalue = formatter.formatCellValue(cell).trim();
cellvalue = cellvalue.replaceAll("[,|<7C><>|(US$)|$|\\s+|\\*]", "");
}
break;
}
case HSSFCell.CELL_TYPE_FORMULA: {// <20><><EFBFBD>ʽ
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("[,|<7C><>|(US$)|$|\\s+|\\*]", "");
}
}else if("yyyy\"<EFBFBD><EFBFBD>\"mm\"<EFBFBD><EFBFBD>\"dd\"<EFBFBD><EFBFBD>\";@".equals(style.getDataFormatString())){
cellvalue=new SimpleDateFormat("yyyy<EFBFBD><EFBFBD>MM<EFBFBD><EFBFBD>dd<EFBFBD><EFBFBD>").format(cell.getDateCellValue());
} else {
cellvalue = formatter.formatRawCellContents(cell.getNumericCellValue(), style.getDataFormat(), style.getDataFormatString()).trim();
cellvalue = cellvalue.replaceAll("[,|<7C><>|(US$)|$|\\s+|\\*]", "");
}
break;
}
case HSSFCell.CELL_TYPE_BOOLEAN: {// <20><><EFBFBD><EFBFBD>
cellvalue = String.valueOf(cell.getBooleanCellValue());
break;
}
case HSSFCell.CELL_TYPE_ERROR: {// <20><><EFBFBD><EFBFBD>ֵ
cellvalue = ErrorEval.getText(cell.getErrorCellValue());
break;
}
}
break;
}
case HSSFCell.CELL_TYPE_BOOLEAN: {// <20><><EFBFBD><EFBFBD>
cellvalue = String.valueOf(cell.getBooleanCellValue()).trim();
break;
}
case HSSFCell.CELL_TYPE_STRING: {// <20>ı<EFBFBD>
cellvalue = cell.getRichStringCellValue().getString().trim();
break;
}
}
}
return cellvalue;
}
}