//设置response相应格式,用于乱码
response.setContentType("application/vnd.ms-excel");
String sDownLoadName = new String("XXXX .xls".getBytes("GBK"), "ISO-8859-1");
response.setHeader("Content-disposition", "attachment; filename=" + sDownLoadName);
export(statistics, article, response.getOutputStream());
//导出功能的具体实现
public void export(List<Map<String, Object>> statistics, List<Map<String, Object>> article, OutputStream out) {
Object[][] rowStatistics = { { "用户名", null }, { "真实姓名", null }, { "投稿数", null }, { "录用数", null },
{ "稿费发放数", null } };
Object[][] rowArticle = { { "文章名称", null }, { "投稿时间", null }, { "稿件状态", null }, { "发布媒体", null },
{ "投稿分类", null }, { "稿费状态", null } };
try {
// out = new FileOutputStream("f:ugc账号数据统计ddddd.xls");
WritableWorkbook book = Workbook.createWorkbook(out);
WritableSheet sheet = book.createSheet("sheet0", 0);
WritableCell label;
for (int i = 0; i < rowStatistics.length; i++) {
label = new Label(i, 0, (String) rowStatistics[i][0]);
sheet.addCell(label);
}
Map<String, Object> map = statistics.get(0);
label = new Label(0, 1, (String) map.get("user_name"));
sheet.addCell(label);
label = new Label(1, 1, (String) map.get("truename"));
sheet.addCell(label);
label = new jxl.write.Number(2, 1, Integer.valueOf(map.get("newcnt").toString()));
sheet.addCell(label);
label = new jxl.write.Number(3, 1, Integer.valueOf(map.get("usecnt").toString()));
sheet.addCell(label);
label = new jxl.write.Number(4, 1, Integer.valueOf(map.get("costcnt").toString()));
sheet.addCell(label);
for (int i = 0; i < rowArticle.length; i++) {
label = new Label(i, 2, (String) rowArticle[i][0]);
sheet.addCell(label);
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
for (int i = 0, length = article.size(); i < length; i++) {
JSONObject jsonObject = JSONObject.fromObject(article.get(i));
rowArticle[0][1] = jsonObject.getString("articleTitle");
if (jsonObject.get("postTime").equals(null)
|| JSONObject.fromObject(jsonObject.get("postTime")).get("time") == null) {
rowArticle[1][1] = null;
} else {
Object obj = JSONObject.fromObject(jsonObject.get("postTime")).get("time");
rowArticle[1][1] = sdf.format(obj);
}
rowArticle[2][1] = Integer
.valueOf(jsonObject.get("articleUseStatus").toString()) == ArticleConstants.ARTICLE_IS_PUB
? "已发布" : "未发布";
rowArticle[3][1] = (CMyString.isEmpty(jsonObject.getString("articlePublishDept"))
|| jsonObject.getString("articlePublishDept").equals("null")) ? "-"
: jsonObject.getString("articlePublishDept");
rowArticle[4][1] = jsonObject.get("classicNames");
rowArticle[5][1] = Integer
.valueOf(jsonObject.get("articleCostStatus").toString()) == ArticleConstants.ARTICLE_IS_COST
? "已发" : "未发";
for (int j = 0; j < rowArticle.length; j++) {
if (rowArticle[j][1] instanceof Number) {
label = new jxl.write.Number(j, i + 3, (Integer) rowArticle[j][1]);
} else {
label = new Label(j, i + 3, (rowArticle[j][1] == null ? "-" : rowArticle[j][1]).toString());
}
sheet.addCell(label);
}
}
book.write();
book.close();
out.flush();
out.close();
} catch (Exception e) {
logger.error("导出用户Excel失败!错误原因:" + e.getMessage());
// e.printStackTrace();
}
}
//同一单元格实现换行
WritableCellFormat format = new WritableCellFormat();
format.setWrap(true);// 换行设置
row1[6][1] = jsonObject.get("column_propagate_level") + "\n" + jsonObject.get("read_cnt");// 传播度+阅读总数
label = new Label(j, i + 2, (String) row1[j][1], format);