1.控制层
controller(控制层)
/**
* 写一个导出excel的功能
*/
@RequestMapping("export")
@ResponseBody
public void export(HttpServletResponse response) {
response.setContentType("application/binary;charset=UTF-8");
String[] titles = { "角色", "描述", "审核状态", "审核时间","最后更新人","最后更新时间" };
System.out.println(titles);
system_RoleService.export(titles);
}
2.业务层及业务实现层
service(接口层)
/**
* @return
*
* @Title: export
* @Description: 导出excel(这里用一句话描述这个方法的作用)
* @param @param titles 设定文件
* @return void 返回类型
* @throws
*/
void export(String[] titles);
serviceimpl(业务实现层)
/**
* 导出excel
*/
public void export(String[] titles) {
try {
// 创建excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
// 建立新的sheet对象
HSSFSheet hssfSheet = workbook.createSheet("sheet1");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow hssfRow = hssfSheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
// 居中样式
hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell hssfCell = null;
for (int i = 0; i < titles.length; i++) {
hssfCell = hssfRow.createCell(i);// 列索引从0开始
hssfCell.setCellValue(titles[i]);// 列名1
hssfCell.setCellStyle(hssfCellStyle);// 列居中显示
}
// 第五步,写入实体数据
List<System_Role> role = system_RoleDao.export();
if (role != null && !role.isEmpty()) {
for (int i = 0; i < role.size(); i++) {
hssfRow = hssfSheet.createRow(i + 1);
System_Role System_Role = role.get(i);
// 第六步,创建单元格,并设置值
String name = "";
if (System_Role.getName() != null) {
name = System_Role.getName();
}
hssfRow.createCell(0).setCellValue(name);
String description = "";
if (System_Role.getDescription() != null) {
description = System_Role.getDescription();
}
hssfRow.createCell(1).setCellValue(description);
int audit_state = 0;
if (System_Role.getAudit_state() != 0) {
audit_state = System_Role.getAudit_state();
}
hssfRow.createCell(2).setCellValue(audit_state);
String audit_date_time = null;
if (System_Role.getAudit_date_time() != null) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
audit_date_time =formatter.format(System_Role.getAudit_date_time());
}
hssfRow.createCell(3).setCellValue(audit_date_time);
String last_updated_user_name = "";
if(System_Role.getLast_updated_user_name() != null){
last_updated_user_name =System_Role.getLast_updated_user_name();
}
hssfRow.createCell(4).setCellValue(last_updated_user_name);
String last_updated_date_time = "";
if(System_Role.getLast_updated_date_time() !=null){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
last_updated_date_time =formatter.format(System_Role.getLast_updated_date_time());
}
hssfRow.createCell(5).setCellValue(last_updated_date_time);
}
}
// 第七步,将文件输出到客户端浏览器 || 使用下载
try {
FileOutputStream fileOutputStream = new FileOutputStream("d:\\2.xls");// 指定路径与名字和格式
workbook.write(fileOutputStream);// 将数据写出去
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
3.dao(接口层)
/**
*
* @Title: export
* @Description: 导出excel(这里用一句话描述这个方法的作用)
* @param @param titles 设定文件
* @return void 返回类型
* @throws
*/
List<System_Role> export();
4.mapper(映射)
<!-- 导出的SQL语句 -->
<select id="export" resultType="com.znkj.entity.System_Role">
select name,description,
audit_state,audit_date_time,last_updated_user_name,
last_updated_date_time from system_role
</select>
5.总结
poi导出功能相对简单,需要注意的是导出路径一定要选对