com.github.xphsc.excel.ExcelHelper Maven / Gradle / Ivy
package com.github.xphsc.excel;
import com.github.xphsc.annotation.ExcelField;
import com.github.xphsc.collect.Lists;
import com.github.xphsc.date.DateUtil;
import com.github.xphsc.util.RegexUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.*;
import java.util.regex.Pattern;
/**
* Created by ${huipei.x} on 2017/6/20
*/
public class ExcelHelper {
/**
* 根据JAVA对象注解获取Excel表头信息
*/
static
public List getHeaderList(Class clz) {
List headers = Lists.newArrayList();
List fields = Lists.newArrayList();
for (Class clazz = clz; clazz != Object.class; clazz = clazz.getSuperclass()) {
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
}
for (Field field : fields) {
// 是否使用ExcelField注解
if (field.isAnnotationPresent(ExcelField.class)) {
ExcelField er = field.getAnnotation(ExcelField.class);
headers.add(new ExcelHeader(er.title(), er.order(), field.getName(), field.getType()));
}
}
Collections.sort(headers);
return headers;
}
static
public Map getHeaderMap(Row titleRow, Class clz) {
List headers = getHeaderList(clz);
Map maps = new HashMap<>();
for (Cell c : titleRow) {
String title = c.getStringCellValue();
for (ExcelHeader eh : headers) {
if (eh.getTitle().equals(title.trim())) {
maps.put(c.getColumnIndex(), eh);
break;
}
}
}
return maps;
}
static
public void outPutFile(Workbook wb, String outFilePath){
FileOutputStream fos = null;
try {
File f = new File(outFilePath);
if (f.getParentFile().isDirectory() && !f.getParentFile().exists()) {
f.mkdirs();
}
if (!f.exists()) {
f.createNewFile();
}
fos = new FileOutputStream(outFilePath);
wb.write(fos);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null){
fos.close();}
} catch (IOException e) {
e.printStackTrace();
}
}
}
static
public String getCellValue(Cell c) {
String o;
switch (c.getCellType()) {
case Cell.CELL_TYPE_BLANK:
o = "";
break;
case Cell.CELL_TYPE_BOOLEAN:
o = String.valueOf(c.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
o = String.valueOf(c.getCellFormula());
break;
case Cell.CELL_TYPE_NUMERIC:
o = String.valueOf(c.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
o = c.getStringCellValue();
break;
default:
o = null;
break;
}
return o;
}
static
public Object str2TargetClass(String strField, Class clazz){
if (null == strField || "".equals(strField)){
return null;}
if ((Long.class == clazz) || (long.class == clazz)) {
strField = matchDoneBigDecimal(strField);
strField = RegexUtil.converNumByReg(strField);
return Long.parseLong(strField);
}
if ((Integer.class == clazz) || (int.class == clazz)) {
strField = matchDoneBigDecimal(strField);
strField = RegexUtil.converNumByReg(strField);
return Integer.parseInt(strField);
}
if ((Float.class == clazz) || (float.class == clazz)) {
strField = matchDoneBigDecimal(strField);
return Float.parseFloat(strField);
}
if ((Double.class == clazz) || (double.class == clazz)) {
strField = matchDoneBigDecimal(strField);
return Double.parseDouble(strField);
}
if ((Character.class == clazz) || (char.class == clazz)) {
return strField.toCharArray()[0];
}
if (Date.class == clazz) {
return DateUtil.stringToDateUnmatchToNull(strField);
}
return strField;
}
private static String matchDoneBigDecimal(String bigDecimal){
// 对科学计数法进行处理
boolean flg = Pattern.matches("^-?\\d+(\\.\\d+)?(E-?\\d+)?$", bigDecimal);
if (flg) {
BigDecimal bd = new BigDecimal(bigDecimal);
bigDecimal = bd.toPlainString();
}
return bigDecimal;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy