POI导出导入源码
public static Field getFieldByName(String fieldName, Class<?> clazz) {
Field[] selfFields = clazz.getDeclaredFields();
for (Field field : selfFields) {
if (field.getName().equals(fieldName)) {
return field;
}
}
Class<?> superClazz = clazz.getSuperclass();
if (superClazz != null && superClazz != Object.class) {
return getFieldByName(fieldName, superClazz);
}
return null;
}
public static Object getFieldValueByName(String fieldName, Object o)
throws Exception {
Object value = null;
Field field = getFieldByName(fieldName, o.getClass());
if (field != null) {
field.setAccessible(true);
value = field.get(o);
} else {
throw new Exception(o.getClass().getSimpleName() + "类不存在字段名 "
+ fieldName);
}
return value;
}
public static Object getFieldValueByNameSequence(String fieldNameSequence, Object o) throws Exception {
Object value = null;
String[] attributes = fieldNameSequence.split("\\.");
if (attributes.length == 1) {
value = getFieldValueByName(fieldNameSequence, o);
} else {
Object fieldObj = getFieldValueByName(attributes[0], o);
String subFieldNameSequence = fieldNameSequence
.substring(fieldNameSequence.indexOf(".") + 1);
value = getFieldValueByNameSequence(subFieldNameSequence, fieldObj);
}
return value;
}
public static String getCellFormatValue(HSSFCell cell) {
String cellValue = "";
DecimalFormat df = new DecimalFormat("#.0000");
if (cell != null) {
switch (cell.getCellType()) {
case STRING:
cellValue = cell.getRichStringCellValue().getString().trim();
break;
case NUMERIC:
cellValue = doubleTrans(cell.getNumericCellValue());
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date d = cell.getDateCellValue();
DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
cellValue = formater.format(d).toString();
}
break;
case BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue()).trim();
break;
case FORMULA:
cellValue = cell.getCellFormula();
break;
default:
cellValue = "";
}
}
return cellValue;
}
demo实现
@Override
public void export(List<ProductCodeBaseCategory> productCodeBaseCategoryList) {
HttpServletResponse response = ServletUtils.getResponse();
String excelName = "产品代码" + System.currentTimeMillis();
response.reset();
response.setContentType("application/vnd.ms-excel");
try {
response.setHeader("Content-disposition", "attachment; filename="
+ new String(excelName.getBytes("gb2312"), "ISO-8859-1") + ".xls");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet sheet = hssfWorkbook.createSheet(excelName);
HSSFCellStyle cellStyle = hssfWorkbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
fillSheet(productCodeBaseCategoryList, sheet, cellStyle);
ServletOutputStream outputStream = response.getOutputStream();
hssfWorkbook.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
log.error("导出Excel失败!");
log.error(e.getMessage());
}
}
private void fillSheet(List<ProductCodeBaseCategory> productCodeBaseCategoryList, HSSFSheet sheet, HSSFCellStyle cellStyle) throws Exception {
List<String> titles = new ArrayList<>();
titles.add("产品代码");
titles.add("同行M代码");
titles.add("同行Y代码");
titles.add("产品名称");
titles.add("类别");
titles.add("类别代码");
titles.add("单位");
titles.add("类型");
titles.add("二级类别");
titles.add("一级类别");
titles.add("产品组别");
titles.add("产品负责人");
titles.add("物料属性");
titles