简单制作通用Excel导出工具

使用的包、hutool、swagger
懒得解释,直接上代码,

public class HuToolExcel {
	public static <T> void download(List<T> data, Class<T> bean, HttpServletResponse response) {
		ExcelWriter writer = ExcelUtil.getWriter();
		Field[] fields = bean.getDeclaredFields();
		for (Field field : fields) {
			if (field.getAnnotation(ApiModelProperty.class) != null) {
				String headName = field.getAnnotation(ApiModelProperty.class).value();
				String fieldName = field.getName();
				writer.addHeaderAlias(fieldName, headName );
			}
		}
		writer.writer(data, true);
		
		responser.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename=" + bean.getSimpleName() + ".xls");
		ServletOutputStream out = null;
		try {
			out = response.getOutputStream();
			writer.flush(out, true);
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			writer.close();
		}
		IoUtil.close(out);
	}
}

纯手敲可能某个字段存在有误

上一篇:hash散列


下一篇:Conversion not supported for type java.time.LocalDateTime报错