zoukankan      html  css  js  c++  java
  • jeecg根据模板自定义导出

    //1.获取模板的路径:

    String lujing = request.getSession().getServletContext().getRealPath("/")+ "export\\template\\test.xlsx";

    //2.准备数据(一个List<Map<String, Object>>对象)

    List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
    //3.读取InputStream流并且写入XSSFWorkbook中

    InputStream in;
    try {
    in = new FileInputStream(new File(lujing1));
    XSSFWorkbook work = null;
    work = new XSSFWorkbook(in);
    //4.把map里的值复写入Excel模板里

    //获取第一个sheet

    XSSFSheet sheetAt = work.getSheetAt(0);

    //从第三行开始赋值
    XSSFRow row = sheetAt.createRow(i+2);

    row.createCell(0).setCellValue("");

    //......依次循环赋值

    //5.导出前台的一些设置

    response.reset();
    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/vnd.ms-excel"); //保证不乱码

    Date date=new Date();
    SimpleDateFormat format=new SimpleDateFormat("MMddHHmmss");
    String time="bb"+format.format(date)+".xlsx";//导出的Excel的名字
    response.setHeader("Content-Disposition","attachment;" + " filename=" + new String(time.getBytes("utf-8"), "ISO-8859-1"));
    ByteArrayOutputStream oss =new ByteArrayOutputStream();
    OutputStream os = response.getOutputStream();
    work.write(oss);

    byte temp[] = oss.toByteArray();
    ByteArrayInputStream in1 = new ByteArrayInputStream(temp);
    int n = 0;
    while ((n = in1.read(temp)) >0) {
    os.write(temp, 0, n);
    }
    os.flush();
    os.close(); 

    } catch (Exception e) {

    e.printStackTrace();
    }

    //附:引入的jar包的pom.xml依赖

    <!-- poi start -->
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>${poi.version}</version>
    </dependency>
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>${poi.version}</version>
    </dependency>
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>${poi.version}</version>
    </dependency>
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>${poi.version}</version>
    </dependency>
    <dependency>
    <groupId>org.jeecgframework</groupId>
    <artifactId>jeasypoi-base</artifactId>
    <version>${jeasypoi.version}</version>
    <exclusions>
    <exclusion>
    <groupId>org.jeecgframework</groupId>
    <artifactId>jeasypoi-annotation</artifactId>
    </exclusion>
    </exclusions>
    </dependency>
    <dependency>
    <groupId>org.jeecgframework</groupId>
    <artifactId>jeasypoi-web</artifactId>
    <version>${jeasypoi.version}</version>
    <exclusions>
    <exclusion>
    <groupId>org.jeecgframework</groupId>
    <artifactId>jeasypoi-base</artifactId>
    </exclusion>
    </exclusions>
    </dependency>
    <dependency>
    <groupId>org.jeecgframework</groupId>
    <artifactId>jeasypoi-annotation</artifactId>
    <version>${jeasypoi.version}</version>
    </dependency>
    <!-- poi end-->

  • 相关阅读:
    IDEA连接 Oracle数据库
    什么是混合云备份
    什么是阿里云ACA认证
    什么是阿里云ACE认证
    什么是轻量应用服务器
    什么是时序时空数据库TSDB
    什么是数据管理DMS
    什么是分析型数据库PostgreSQL版
    阿里云多端小程序
    阿里云云计算ACP专业认证考试
  • 原文地址:https://www.cnblogs.com/xueblvip/p/11981242.html
Copyright © 2011-2022 走看看