/** * 导入订单Excel(需poi包) * @param request * @param model * @return */ @RequestMapping(value = "/exportExcel", method = RequestMethod.POST) public String exportExcel(HttpServletRequest request, Model model) { try { Map<String, Object> map = dtPageSearchParams(request); List<OrderVo> orderList = orderService.findListByParam(map); DtExcelMap excel = DtExcelMap.getExcelMap("订单列表", new String[] { "ID","订单编号","迪迪序列", "是否匿名", "订单状态", "借款人", "手机号", "借款类型", "借款地点-省", "借款地点-市", "借款金额", "借款期限", "订单详细介绍","撤单原因", "创建时间" }, orderList, new String[] { "id","orderNo","orderSerialNumber", "anonymousFlagView", "statusView", "realName", "mobile", "typeView", "PRovinceName", "cityName", "amount", "period", "orderDesc","cancelVal", "createTime" }); model.addAttribute("map", excel); return EXCELVIEW;//protected final String EXCELVIEW = "excelView"; } catch (Exception e) { log.error("导出excel失败:" + e); } return null; }import java.util.Date;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.springframework.web.servlet.view.document.AbstractExcelView;import com.xhh.kdw.ms.tool.DateUtil;import com.xhh.kdw.ms.tool.DtExcelMap;import com.xhh.kdw.ms.tool.ReflectUtil;public class KDWExcelView extends AbstractExcelView { @Override protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { // 从model对象中获取excel所需数据 DtExcelMap map = (DtExcelMap) model.get("map"); // 创建Excel的sheet String name = map.getExcelName(); HSSFSheet sheet = workbook.createSheet(name); // 创建标题行 HSSFRow header = sheet.createRow(0); String[] titles = map.getTitles(); int cell = 0; for (String title : titles) { header.createCell(cell).setCellValue(title); cell++; } // 填充数据 int rowNum = 1; List<?> models = map.getModels(); String[] fileds = map.getFields(); for (Object model2 : models) { HSSFRow row = sheet.createRow(rowNum); String cellValue = null; for (int i = 0; i < fileds.length; i++) { String fieldName = fileds[i]; Object result = ReflectUtil.invokeGetter(model2, fieldName); cellValue = ReflectUtil.toString(result); // 如果是日期类型则进行格式化处理 if (ReflectUtil.isDateType(model2.getClass(), fieldName)) { cellValue = DateUtil.Date2Stirng2Second((Date) result); } row.createCell(i).setCellValue(cellValue); } rowNum++; } response.setHeader("Content-Disposition", "attachment;filename=" + new String((name + ".xls").getBytes(), "ISO-8859-1")); }}package com.xhh.kdw.ms.tool;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Date;/** * 反射工具类 */public class ReflectUtil { /** * 反射调用指定构造方法创建对象 * * @param clazz * 对象类型 * @param argTypes * 参数类型 * @param args * 构造参数 * @return 返回构造后的对象 * @throws SecurityException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws IllegalaccessException * @throws InstantiationException * */ public static <T> T invokeConstructor(Class<T> clazz, Class<?>[] argTypes, Object[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Constructor<T> constructor = clazz.getConstructor(argTypes); return constructor.newInstance(args); } /** * 反射调用指定对象属性的getter方法 * * @param <T> * 泛型 * @param target * 指定对象 * @param fieldName * 属性名 * @return 返回调用后的值 * @throws SecurityException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws IllegalAccessException * */ public static <T> Object invokeGetter(T target, String fieldName) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // 如果属性名为xxx,则方法名为getXxx String methodName = "get" + StringUtil.firstCharUpperCase(fieldName); Method method = target.getClass().getMethod(methodName); return method.invoke(target); } /** * 反射调用指定对象属性的setter方法 * * @param <T> * 泛型 * @param target * 指定对象 * @param fieldName * 属性名 * @param argTypes * 参数类型 * @param args * 参数列表 * @throws SecurityException * @throws NoSuchFieldException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws IllegalAccessException * */ public static <T> void invokeSetter(T target, String fieldName, Object args) throws NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // 如果属性名为xxx,则方法名为setXxx String methodName = "set" + StringUtil.firstCharUpperCase(fieldName); Class<?> clazz = target.getClass(); Field field = clazz.getDeclaredField(fieldName); Method method = clazz.getMethod(methodName, field.getType()); method.invoke(target, args); } @SuppressWarnings("unchecked") public static <T> boolean isDateType(Class<T> clazz, String fieldName) { boolean flag = false; Field field = null; boolean noSuchFiled = true; do { try { field = clazz.getDeclaredField(fieldName); Object typeObj = field.getType().newInstance(); flag = typeObj instanceof Date; noSuchFiled = false; } catch (NoSuchFieldException e) { clazz = (Class<T>) clazz.getSuperclass(); } catch (Exception e) { // 除了NoSuchFieldException这个异常,其他直接跳出循环 noSuchFiled = false; } } while (noSuchFiled && clazz != Object.class); // try { // Field field = clazz.getField(fieldName); // field.setAccessible(true); // Object typeObj = field.getType().newInstance(); // flag = typeObj instanceof Date; // } catch (Exception e) { // e.printStackTrace(); // } return flag; } public static String toString(Object object) { StringBuffer buffer = new StringBuffer(); if (object != null) { buffer.append(object); } return buffer.toString(); }}<bean name="excelView" class="com.xhh.kdw.ms.view.KDWExcelView"/>
新闻热点
疑难解答