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.导出前台的一些设置

扫描二维码关注公众号,回复: 6301708 查看本文章

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-->

猜你喜欢

转载自www.cnblogs.com/xujiating/p/10944720.html
今日推荐