Java导出Excel
一、jar包/pom依赖
maven+spring+spingMvc,需要使用poi相关jar包(poi的版本是3.17,poi-3.17.jar),或在pom文件中配置## 新的改变
- poi提供microsoft office旧版本支持,eg .xls Excel
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
- poi-ooxml提供microsoft office新版本支持,eg .xlsx Excel
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
二、前端
<#--<a href="/user/export.do" >导出</a>-->
或
<button type="button" id="daoUsers" class="btn btn-success">导出</button>
<script>
/*Excel导出*/
$("#daoUsers").click(function () {
window.location.href="/user/export.do";
})
</script>
三、后台Controller层
//导出Excel
@RequestMapping("/export")
@ResponseBody
public String createExcel(HttpServletResponse response) throws IOException {
//获取查询结果的数据,reportlist为别的方法查询出来的数据,格式为List<Object[]>,其实这里不管 reportlist是什么数据格式,这里只要对其进行封装就行了
List<User> newlist = userService.queryUsers();
System.out.println("数据行数:"+newlist.size());
//System.out.println("集合的第一行数据:"+newlist.get(0));
//数据封装,这里的map之所以敢这样add是因为这里的add顺序和hql中的select字段顺序是一样的,总共就查询那么多字段
List<Map<String,Object>> solist = new ArrayList();
for(User obj:newlist){
//每次循环都要重新new一个map,表示不同对象
//System.out.println("User的第一个字段"+obj.getUid());
Map<String,Object> map = new HashMap();
map.put("uid", obj.getUid());
map.put("uname",obj.getUname());
map.put("upass",obj.getUpass());
map.put("uage",obj.getUage());
map.put("usex",obj.getUsex());
solist.add(map);
}
//创建HSSFWorkbook对象(excel的文档对象)
HSSFWorkbook wb = new HSSFWorkbook();
//建立新的sheet对象(excel的表单)
HSSFSheet sheet=wb.createSheet("报表");
//在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
HSSFRow row1=sheet.createRow(0);
//创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
HSSFCell cell=row1.createCell(0);
// 1.生成字体对象
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 12);
font.setFontName("新宋体");
// 2.生成样式对象,这里的设置居中样式和版本有关,我用的poi用HSSFCellStyle.ALIGN_CENTER会报错,所以用下面的
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置居中样式
style.setFont(font); // 调用字体样式对象
style.setWrapText(true);
/*style.setAlignment(HorizontalAlignment.CENTER);//设置居中样式*/
// 3.单元格应用样式
cell.setCellStyle(style);
//设置单元格内容
cell.setCellValue("报表");
//合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
sheet.addMergedRegion(new CellRangeAddress(0,0,0,9));
//在sheet里创建第二行
HSSFRow row2=sheet.createRow(1);
//创建单元格并设置单元格内容及样式
HSSFCell cell0=row2.createCell(0);
cell0.setCellStyle(style);
cell0.setCellValue("用户号");
HSSFCell cell1=row2.createCell(1);
cell1.setCellStyle(style);
cell1.setCellValue("用户名");
HSSFCell cell2=row2.createCell(2);
cell2.setCellStyle(style);
cell2.setCellValue("密码");
HSSFCell cell3=row2.createCell(3);
cell3.setCellStyle(style);
cell3.setCellValue("年龄");
HSSFCell cell4=row2.createCell(4);
cell4.setCellStyle(style);
cell4.setCellValue("性别");
//单元格宽度自适应
sheet.autoSizeColumn((short)3);
sheet.autoSizeColumn((short)4);
sheet.autoSizeColumn((short)5);
sheet.autoSizeColumn((short)6);
sheet.autoSizeColumn((short)7);
sheet.autoSizeColumn((short)8);
sheet.autoSizeColumn((short)9);
//宽度自适应可自行选择自适应哪一行,这里写在前面的是适应第二行,写在后面的是适应第三行
for (int i = 0; i < solist.size(); i++) {
//单元格宽度自适应
sheet.autoSizeColumn((short)0);
sheet.autoSizeColumn((short)1);
sheet.autoSizeColumn((short)2);
//从sheet第三行开始填充数据
HSSFRow rowx=sheet.createRow(i+2);
Map<String,Object> map = solist.get(i);
//这里的hospitalid,idnumber等都是前面定义的全局变量
uid = (Integer) map.get("uid");
HSSFCell cell00=rowx.createCell(0);
cell00.setCellStyle(style);
cell00.setCellValue(uid);
uname = (String) map.get("uname");
HSSFCell cell01=rowx.createCell(1);
cell01.setCellStyle(style);
cell01.setCellValue(uname);
upass = (String) map.get("upass");
HSSFCell cell02=rowx.createCell(2);
cell02.setCellStyle(style);
cell02.setCellValue(upass);
uage = (Integer) map.get("uage");
HSSFCell cell03=rowx.createCell(3);
cell03.setCellStyle(style);
cell03.setCellValue(uage);
usex = (String) map.get("usex");
HSSFCell cell04=rowx.createCell(4);
cell04.setCellStyle(style);
cell04.setCellValue(usex);
}
//输出Excel文件
OutputStream output=response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename=user.xls");//文件名这里可以改
response.setContentType("application/msexcel");
wb.write(output);
output.close();
return null;
}
至此,数据会以Excel格式导出。