基于jxls的Lreports

来源:互联网 发布:mac中返回主界面图标 编辑:程序博客网 时间:2024/06/03 19:20

1.项目简介(自己的开源项目,欢迎大家拍砖)

      基于jxls的xls及cvs文件生成下载(目前暂适用于浏览器下载),在excel或cvs模板中使用特殊表达式来定义输出格式和数据布局。
      为解决日常中通过浏览器下载报表文件,从而在jxls基础上开发适用大部分下载场景。
      目前暂提供下载接口,后续有想新增生成文件到指定目录API。
       github地址:https://github.com/SayNight/Lreports

2.项目依赖


    

3.使用示例

     (1) 首先使用模板来定义输出格式和数据布局,下面是简单示例,数据格式化有金额、数字、日期及时间戳。
           excel模板
 
          csv模板

     (2) 获取表格数据

          可以从数据库中查询,也可以是集合中的数据,数据来源不限,但sql中列名或集合中实体属性名由a-z, A-Z, 及下划线'_'组成。

    (3)  调用API生成文件并下载
private static final String XLSTEMPLATE = "/WEB-INF/template/xls/template.xls";private static final String CSVTEMPLATE = "/WEB-INF/template/csv/template.csv";@RequestMapping("download")public void download(HttpServletRequest request, HttpServletResponse response) {Map<String, Object> map = buildQueryMap(request);//构建查询参数String downType = request.getParameter("downType");List<Map<String, Object>> resultList = queryService.queryList(map);// 数据库查询报表数据try {WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();ServletContext servletContext = webApplicationContext.getServletContext();String template = null;if (SupportTypeEnum.XLS.name().equals(downType)) {template = XLSTEMPLATE;}if (SupportTypeEnum.CSV.name().equals(downType)) {template = CSVTEMPLATE;}DownloadService downloadService = DownloadFactory.createDownloadService(request.getParameter("downType"));String fileName = FileUtil.fmtFileName(request, DateUtils.getReqDate() + "模板");//如果文件名称没有中文,此行可去除downloadService.downloadFile(response, servletContext.getResourceAsStream(template), resultList, fileName, DownLoadFunction.class);} catch (Exception e) {logger.error("下载异常", e);}}