zoukankan      html  css  js  c++  java
  • java导出excel报表

    1、使用的是ssm框架,maven构建项目

    2、使用jxls

    3、maven依赖:

    <dependency>

        <groupId>net.sf.jxls</groupId>

        <artifactId>jxls-core</artifactId>

        <version>1.0.5</version>

       </dependency>

     

        <dependency>

          <groupId>net.sf.jxls</groupId>

          <artifactId>jxls-reader</artifactId>

          <version>1.0.5</version>

        </dependency>

    4、代码实现

    @RequestMapping("/alarmTopdf")

    @ResponseBody

    public AjaxResult alarmTopdf(HttpServletRequest req,HttpServletResponse response) throws OprException, ParseException{

    AjaxResult ajaxResult = new AjaxResult();

    List<RealData> list = (List<RealData>) SecurityUtils.getSubject().getSession().getAttribute("lm");

    HashMap map = (HashMap) SecurityUtils.getSubject().getSession().getAttribute("pmap");

    HashMap beans = new HashMap();   

    String fileFlag = String.valueOf(System.currentTimeMillis());

    String dataInfo = "";

     

    String path = req.getSession().getServletContext().getRealPath("/"); 

    String userInfo = null;

    String templateDir = path+"/excel-template/historyalarm.xls";//

    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");

    fileFlag = sf.format(new Date());

    String outPutDir = path+"/outfile/报表告警数据"+fileFlag+".xls";

    InputStream  inputStream = null;

     

    XLSTransformer xls = new XLSTransformer();

    beans.put("list", list);

    beans.put("map", map);

    try {

    xls.transformXLS(templateDir,beans,outPutDir);

    userInfo = "/outfile/报表告警数据"+fileFlag+".xls";

    } catch (ParsePropertyException e) {

    e.printStackTrace();

    } catch (InvalidFormatException e) {

    e.printStackTrace();

    } catch (IOException e) {

    e.printStackTrace();

    }

    AjaxResult result = ajaxResult.success(userInfo);

    return result;

    }

     

    分析

    在做这个的时候 遇到一个坑:一定要有这个的,不然是不会出现excel文件的

     excel模板:jstl表达式实现

    梦想这东西和经典一样,永远不会随时间而褪色,反而更加珍贵!
  • 相关阅读:
    利用vuex 做个简单的前端缓存
    EFcore 解决 SQLite 没有datetime 类型的问题
    dotnet 清理 nuget 缓存
    .net 5 单文件模式发布异常 CodeBase is not supported on assemblies loaded from a single-file bundle
    ubuntu 开启ip转发的方法
    Vue-ECharts 6 迁移记录
    System.Text.Json 5.0 已增加支持将Enum 由默认 Number类型 转换为String JsonStringEnumConverter
    Windows 10 LTSC 2019 正式版轻松激活教程
    Mac 提示Permission denied
    苹果手机代理 charles 提示(此链接非私人连接)
  • 原文地址:https://www.cnblogs.com/haoxiu1004/p/9143231.html
Copyright © 2011-2022 走看看