private static final String SHEET_NAME = "培养计划表";

/**
* @param response
* @param trainingName
* @return
*/
@RequestMapping("/exportTrainingPlanMessageToExcel.do_")
@ResponseBody
public void exportTrainingPlanMessageToExcel(HttpServletResponse response, @RequestParam("trainingName") String trainingName) {
//文件的默认保存名
String fileName = "exportTrainingPlanMessageToExcel.xls"; List<List<Object>> paramList = trainingPlanService.getParamList(trainingName); ExportEmployeeMessageToExcel.exportDataToExcel(fileName, response, paramList, SHEET_NAME);
}
@Override
public List<List<Object>> getParamList(String trainingName) {
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("trainingName", trainingName); List<List<Object>> resultList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<TrainingPlan> dataList = trainingPlanDao.getTrainingPlanByTrainingName(paramMap); //添加excel文件表头
List<Object> headList = new ArrayList<>();
headList.add("创建时间");
headList.add("知识技能"); resultList.add(headList); //添加excel文件数据
for (TrainingPlan trainingPlan : dataList) {
list.add(sdf.format(trainingPlan.getCreateTime()));
list.add(trainingPlan.getKnowledgeSkills()); resultList.add(list);
}
return resultList;
}
public class ExportEmployeeMessageToExcel {

    private static final Logger log = LoggerFactory.getLogger(ExportEmployeeMessageToExcel.class);

    /**
* 批量将数据导入到excel中
* @param fileName 生成文件的名字
* @param response
* @param paramList 要导入到excel中所有的数据
* @param sheetName excel sheet名字
* @return
*/
public static void exportDataToExcel(String fileName, HttpServletResponse response, List<List<Object>> paramList, String sheetName){
/**
* 以下为生成Excel操作
*/
// 1.创建一个workbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 2.在workbook中添加一个sheet,对应Excel中的一个sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 3.在sheet中添加表头第0行,老版本poi对excel行数列数有限制short
HSSFRow row = sheet.createRow(0); /*// 设置表头
HSSFCell cell = row.createCell(0);
cell.setCellValue("表头1");
cell.setCellStyle(style); cell = row.createCell(1);
cell.setCellValue("表头2");
cell.setCellStyle(style); cell = row.createCell(2);
cell.setCellValue("表头3");
cell.setCellStyle(style); cell = row.createCell(3);
cell.setCellValue("表头4");
cell.setCellStyle(style); cell = row.createCell(4);
cell.setCellValue("表头5");
cell.setCellStyle(style);*/ // 循环将数据写入Excel,包括表头
for (int i = 0; i < paramList.size(); i++) {
row = sheet.createRow((int) i);
List list= paramList.get(i);
// 创建单元格,设置值
for (int j = 0; j < list.size(); j++) {
row.createCell(j).setCellValue(String.valueOf(list.get(j)));
}
} downloadExcel(wb, response, fileName); } /**
* 将导好数据的excel文件下载下来,并打开下载页面
* @param fileName 生成文件的名字
* @param response
* @return
*/
public static void downloadExcel(HSSFWorkbook wb, HttpServletResponse response, String fileName){
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
wb.write(os);
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
response.reset();
response.setContentType("application/octet-stream; charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes(),"ISO8859-1"));
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null; try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[1024];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
log.error("异常情况为:" + e.getMessage());
} finally {
if (wb != null) {
wb.close();
}
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
}
} catch (IOException e) {
log.error("异常情况为:" + e.getMessage());
}
}
}

POM:

<!-- poi,excel解析xls格式 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!-- poi-ooxml,excel解析xlsx格式 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency> <!--excel文件不需要转码成二进制-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>

将数据导出到 excel ,然后下载下来的更多相关文章

  1. abp中文件下载,将内存数据导出到Excel并下载

    1.数据导出为Excel的Stream using System; using System.Collections.Generic; using System.IO; using Abp.Colle ...

  2. DataTable数据导出到Excel,并发送到客户端进行下载

    本代码实现思路是:页面显示和导出分开,导出的数据和用于页面显示的是同一查询数据方式,所以也是同样的数据,只是在导出数据时从数据库重新捞了一次数据.此导出数据方式会先将数据保存到Excel中,然后将创建 ...

  3. 数据导出至Excel文件--好库编程网http://code1.okbase.net/codefile/SerializeHelper.cs_2012122018724_118.htm

    using System; using System.IO; using System.Data; using System.Collections; using System.Data.OleDb; ...

  4. 将datagrid中数据导出到excel中 -------<<工作日志2014-6-6>>

    前台datagrid数据绑定 #region 导出到excel中    /// <summary>    /// 2014-6-6    /// </summary>    / ...

  5. Qt中将QTableView中的数据导出为Excel文件

    如果你在做一个报表类的程序,可能将内容导出为Excel文件是一项必须的功能.之前使用MFC的时候我就写过一个类,用于将grid中的数据导出为Excel文件.在使用了QtSql模块后,我很容易的将这个类 ...

  6. 使用PHPExcel将数据导出至Excel

    安装类库 从GitHub上下载PHPExcel类库 地址:https://github.com/PHPOffice/PHPExcel 解压后将Classes文件夹移动到ThinkPHP的extend目 ...

  7. Asp.net网页中DataGridView数据导出到Excel

    经过上网找资料,终于找到一种可以直接将GridView中数据导出到Excel文件的方法,归纳方法如下: 1. 注:其中的字符集格式若改为“GB2312”,导出的部分数据可能为乱码: 导出之前需要关闭分 ...

  8. struts2结合poi-3.7实现数据导出为excel

    我们在处理数据的时候,有可能要将数据导出到excel文件中,那么java中是怎么实现的呢?apache开发的poi就可以帮我们实现啦,它也是开源的代码,导入相应的jar包,就可以轻松实现,下面让我们来 ...

  9. 学习笔记 DataGridView数据导出为Excel

    DataGridView数据导出为Excel   怎样把WinForm下的“DGV”里的绑定数据库后的数据导出到Excel中. 比如:在窗体里有个一“DGV”,DataGridView1,绑定了数据源 ...

  10. 将C1Chart数据导出到Excel

    大多数情况下,当我们说将图表导出到Excel时,意思是将Chart当成图片导出到Excel中.如果是这样,你可以参考帮助文档中保存和导出C1Chart章节. 不过,也有另一种情况,当你想把图表中的数据 ...

随机推荐

  1. 关于OpenModelica的编译

    由于工作需要,最近对OpenModelica进行二次开发,由于国内资料也比较少,所以踩了一些坑,近期计划把OpenModelica的编译,msys,及OpenModelica里面比较关键的部分OMEd ...

  2. koa 路由模块化(一)

    1.项目目录 2.入口文件 根目录/app.js /** * koa 路由模块化 */ const Koa = require('koa'); const router = require('koa- ...

  3. 创建DevExtreme应用程序

    如果你从头开始一个项目,那就使用DevExtreme Angular 模板.生成的项目带导航菜单以及在响应布局中的几个对应的示例视图. 你可以使用 DevExtreme CLI 生成应用程序: npx ...

  4. centos7 - mysql修改密码

    set password for 'root'@'localhost'=password('MyNewPass4!'); mysql5.7默认安装了密码安全检查插件(validate_password ...

  5. Cortex-M3 双堆栈指针(MSP&PSP)

    [双堆栈指针(MSP&PSP)] Cortex-M3内核中有两个堆栈指针(MSP & PSP),但任何时刻只能使用到其中一个. 复位后处于线程模式特权级,默认使用MSP. 通过SP访问 ...

  6. ajax post 请求

    $(".login_btn").click(function(){ if($(".user_").val()=="admin"&&a ...

  7. Django框架 选项卡加active类的方案

    ------html部分----- <div class="left-menu"> <div class="menu-body"> &l ...

  8. 关于Linux下的连接文件学习总结

    1.连接文件区分为两种,一种类似windows下快捷方式,使用户能够快速连接到目标文件或目录. 另一种则通过文件系统中的inode连接来产生新文件名,而不是产生新文件. 两种方式分别称为符号/硬连接. ...

  9. 【VS开发】关于内存泄漏的调试

    没想到造成泄漏的原因是由于保存数据的线程因为事件阻塞在那里,此时要关闭OnClose的时候,这个挂起的线程爆出了内存泄漏,所以在关闭窗口之前,需要SetEvent(m_hSaveDataEvent); ...

  10. 【Linux开发】linux中关于dma_alloc_coherent的用法

    大家都知道,DMA的操作是需要物理地址的,但是在linux内核中使用的都是虚拟地址,如果我们想要用DMA对一段内存进行操作,我们如何得到这一段内存的物理地址和虚拟地址的映射呢?dma_alloc_co ...