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

最新文章

  1. Oracle体系结构总结
  2. sql 中实现往表中插入一条记录并返回当前记录的ID
  3. table插件实现
  4. vs2013_arcgis_developer_kit_101_install
  5. IOS中的网络编程
  6. 利用IronJs在.NET程序里面跑javascript脚本
  7. Nginx 笔记与总结(5)访问日志管理:计划任务 + 日志切割
  8. Android view的requestLayout()
  9. 域用户允许更改IP地址
  10. 让动态创建的ActiveX控件响应Windows消息
  11. 2014阿里实习生面试题——mysql如何实现的索引
  12. C++小技巧之CONTAINING_RECORD
  13. Jquery常用操作:checkbox、select取值,radio、checkbox、select选中及其相关
  14. Java SE学习笔记 ---&gt;高级类特性 ---&gt; toString() 方法
  15. 痞子衡嵌入式:飞思卡尔i.MX RT系列MCU开发那些事 - 索引
  16. docker服务各个模块
  17. 【转】Kaggle注册问题-验证码和手机短信
  18. ueditor保存出现 从客户端(Note=&quot;&lt;p&gt;12345&lt;/p&gt;&quot;)中检测到有潜在危险的 Request.Form 值
  19. SQL记录-解锁和dbms_job操作
  20. c++默认参数函数注意事项

热门文章

  1. jmeter连接mysql数据库进行单条语句查询
  2. SpEL表达式总结
  3. ES7.x mapping 类型
  4. 2.02_Python网络爬虫分类及其原理
  5. wlan相关查询命令
  6. CentOS7 解决不能切换中英文输入法的问题
  7. 修改文件属性与权限(鸟哥linux私房菜)
  8. c语言第一次作业1
  9. Gym 100548F Color 给花染色 容斥+组合数学+逆元 铜牌题
  10. JAVA的面向对象1