/**Controller层*/
/**excel标题 */
String[] title = new String[12];
title[0] = "用户名称";
title[1] = "手机号";
title[2] = "所属工会";
title[3] = "所属话题";
title[4] = "动态标题";
title[5] = "动态内容";
title[6] = "点赞数";
title[7] = "评论数";
title[8] = "分享数";
title[9] = "阅读数";
title[10] = "发布时间";
title[11] = "是否为热门";
/**sheet名 */
String sheetName = "动态列表记录";
String[][] content = new String[list.size()][12];
Map<String, Object> map = new HashMap<String, Object>();
/**循环取出从数据库中查出的记录list,一行一行填充Excel表*/
for (int i = 0; i < list.size(); i++) {
map = list.get(i);
content[i][0] = map.get("userName") == null ? "-" : map.get("userName").toString();
content[i][1] = map.get("userMobile") == null ? "-" : map.get("userMobile").toString();
content[i][2] = map.get("unionName") == null ? "-" : map.get("unionName").toString();
content[i][3] = map.get("topicTitle") == null ? "-" : map.get("topicTitle").toString();
content[i][4] = map.get("dynamicTitle") == null ? "-" : map.get("dynamicTitle").toString();
content[i][5] = map.get("dynamicContent") == null ? "-" : map.get("dynamicContent").toString();
content[i][6] = map.get("dymamicLikeNum") == null ? "-" : map.get("dymamicLikeNum").toString();
content[i][7] = map.get("dymamicRviewNum") == null ? "-" : map.get("dymamicRviewNum").toString();
content[i][8] = map.get("dymamicShareNum") == null ? "-" : map.get("dymamicShareNum").toString();
content[i][9] = map.get("dymamicReadNum") == null ? "-" : map.get("dymamicReadNum").toString();
content[i][10] = map.get("createTime") == null ? "-" : map.get("createTime").toString();
String flagHot = "-";
if("0".equals(map.get("flagHot").toString())){
flagHot = "否";
}else if("1".equals(map.get("flagHot").toString())){
flagHot = "是";
}
content[i][11] = flagHot;
}
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
/** 响应到客户端 */
try {
ExcelUtil.setResponseHeader(response, sheetName);
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
}
return ResultBean.ok("导出成功");
ExcelUtil工具类:
public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {
if (wb == null) {
wb = new HSSFWorkbook();
}
HSSFSheet sheet = wb.createSheet(sheetName);
HSSFRow row = sheet.createRow(0);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment((short)2);
HSSFFont font2 = wb.createFont();
font2.setBoldweight((short)700);
font2.setFontHeightInPoints((short)12);
style.setFont(font2);
HSSFCell cell = null;
int i;
for(i = 0; i < title.length; ++i) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
for(i = 0; i < values.length; ++i) {
row = sheet.createRow(i + 1);
for(int j = 0; j < values[i].length; ++j) {
row.createCell(j).setCellValue(values[i][j]);
}
}
for(i = 0; i < title.length; ++i) {
sheet.setColumnWidth(i, title[i].getBytes().length * 2 * 256);
}
return wb;
}
public static void setResponseHeader(HttpServletResponse response, String fileName) {
try {
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "must-revalidate");
response.setDateHeader("Expires", 0L);
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1") + ".xls");
} catch (Exception var3) {
var3.printStackTrace();
}
}