引入的jar包
org.apache.poi
poi-scratchpad
3.11-beta2
org.apache.poi
poi-ooxml
3.11-beta2
org.apache.poi
poi-ooxml-schemas
3.11-beta2
org.apache.poi
poi-excelant
3.11-beta2
第一种方法如下,这种方法很清晰,明了,使用简单,推荐使用第一种
@RequestMapping(value = "/exportOrderCar", method = { RequestMethod.GET, RequestMethod.POST })
@ResponseBody
public BaseRes
第二种如下,个人感觉不是很清晰
@RequestMapping("/exportExcel")
public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
List list = null;
//获取查询参数
String ids = request.getParameter("ids");
// String idFlag = request.getParameter("idFlag");
String account = request.getParameter("account");
String name = request.getParameter("name");
String status = request.getParameter("status");
String orgId = request.getParameter("orgId");
String showAllFlag = request.getParameter("showAllFlag");
if (StringUtils.isBlank(ids)) {
// 导出符合条件的所有
// 查询 数据不分页
list = sysUserService.geUserVoList(account, name, status, orgId, showAllFlag);
} else {
// 导出勾选的
// 根据id 查询
list = sysUserService.getUserVoListByIds(ids);
}
response.setContentType("application/vnd.ms-excel");
String exportFileName = null;
OutputStream fOut = null;
try {
// 进行转码,使其支持中文文件名
exportFileName = RequestUtils.getDownFileNameByBrower(request, "用户信息表");
response.setHeader("content-disposition", "attachment;filename=" + exportFileName + ".xls");
// 产生工作簿对象
HSSFWorkbook workbook = new HSSFWorkbook();
// 产生工作表对象
HSSFSheet sheet = workbook.createSheet();
sheet.setDefaultColumnWidth(15);
HSSFCellStyle headerStyle = (HSSFCellStyle) workbook.createCellStyle();// 创建标题样式
// headerStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);// 设置背景色
// headerStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);// 设置前景色
// headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// headerStyle.setDataFormat(HSSFDataFormat.);
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 设置垂直居中
HSSFFont headerFont = (HSSFFont) workbook.createFont(); // 创建字体样式
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
headerFont.setFontName("宋体"); // 设置字体类型
headerFont.setFontHeightInPoints((short) 10); // 设置字体大小
headerStyle.setFont(headerFont); // 为标题样式设置字体样式
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
// cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
// 定义居左对齐方式
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM);
HSSFFont headerFont2 = (HSSFFont) workbook.createFont(); // 创建字体样式
headerFont2.setFontName("宋体"); // 设置字体类型
headerFont2.setFontHeightInPoints((short) 12); // 设置字体大小
cellStyle.setFont(headerFont2); // 为标题样式设置字体样式
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
// 生成excel表头
HSSFRow headerRow = sheet.createRow(0);// 创建首行
headerRow.setHeightInPoints(48);
createCell(headerRow, 0, headerStyle, "登录账号");
createCell(headerRow, 1, headerStyle, "员工姓名");
createCell(headerRow, 2, headerStyle, "性别(填男或女)");
createCell(headerRow, 3, headerStyle, "手机号码");
createCell(headerRow, 4, headerStyle, "固定电话");
createCell(headerRow, 5, headerStyle, "邮箱地址");
createCell(headerRow, 6, headerStyle, "所属机构");
int rowNum = 0;
for (SysUserVo sysUserVo : list) {
rowNum++;
HSSFRow row = sheet.createRow(rowNum);// 创建一行
createCell(row, 0, cellStyle, sysUserVo.getAccount());
createCell(row, 1, cellStyle, sysUserVo.getName());
createCell(row, 2, cellStyle, sysUserVo.getSex());
createCell(row, 3, cellStyle, sysUserVo.getMobile());
createCell(row, 4, cellStyle, sysUserVo.getPhone());
createCell(row, 5, cellStyle, sysUserVo.getEmail());
createCell(row, 6, cellStyle, sysUserVo.getOrgName());
}
fOut = response.getOutputStream();
workbook.write(fOut);
} catch (Exception e) {
e.printStackTrace();
} finally {
fOut.flush();
fOut.close();
}
System.out.println("文件生成...");
}
public void createCell(HSSFRow row, int cellNum, CellStyle cellStyle, String cellValue) {
HSSFCell idCell = row.createCell(cellNum);
idCell.setCellStyle(cellStyle);
if (StringUtils.isNotBlank(cellValue))
idCell.setCellValue(cellValue);
}