国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

springmvc導出execl操作

2019-11-06 06:05:25
字體:
來源:轉載
供稿:網友
	/**	 * 導入訂單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"/>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平湖市| 双峰县| 临沂市| 栖霞市| 阳春市| 黄骅市| 自贡市| 玛多县| 秭归县| 大方县| 临西县| 高邑县| 康保县| 贵定县| 崇义县| 肥城市| 三明市| 昌图县| 互助| 汉寿县| 宁远县| 鸡泽县| 砚山县| 平泉县| 云安县| 郯城县| 武平县| 阿巴嘎旗| 梁平县| 济源市| 栾川县| 新乡市| 长治县| 象州县| 武平县| 黄冈市| 沂源县| 新乡县| 黄冈市| 长春市| 台南市|