一、使用jxl技术
需要jxl.jar :
如果是通过maven添加jxl.jar,则在pom.xml中添加配置
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
maven的访问地址:http://183.62.194.65:8085/nexus/index.html#view-repositories
二、代码如下
public void exportUserExcel(List<UserStatisticsVo> userStatisticsList,String state, HttpServletResponse response) {
// 创建Excel工作薄
WritableWorkbook wwb = null;
try {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename="
+ new String("用户资源数量统计表".getBytes("gb2312"), "ISO8859-1")
+ ".xls");
wwb = Workbook.createWorkbook(response.getOutputStream());
WritableFont codeFont = new WritableFont(
WritableFont.createFont("宋体"), 12);// 设置字体、格式
WritableCellFormat codeCF = new WritableCellFormat(codeFont);
codeCF.setVerticalAlignment(VerticalAlignment.CENTRE);// 上下居中
codeCF.setAlignment(Alignment.CENTRE);// 居中
// 设置列宽
CellView cellView = new CellView();
cellView.setSize(10 * 550);
// 添加工作表并设置Sheet的名字
WritableSheet sheet = wwb.createSheet("帐号信息", 0);
for (int n = 0; n < 6; n++) {
sheet.setColumnView(n, cellView);
}
// 序号,用户,资源数量,资源下载量,资源浏览量,所属部门/栏目
Label sortLabel = new Label(0, 0, "序号");
sortLabel.setCellFormat(codeCF);
sheet.addCell(sortLabel);
Label userNameLabel = new Label(1, 0, "用户");
userNameLabel.setCellFormat(codeCF);
sheet.addCell(userNameLabel);
Label totalNumberLabel = new Label(2, 0, "资源数量");
totalNumberLabel.setCellFormat(codeCF);
sheet.addCell(totalNumberLabel);
Label downNumberLabel = new Label(3, 0, "资源下载量");
downNumberLabel.setCellFormat(codeCF);
sheet.addCell(downNumberLabel);
Label HisNumberLabel = new Label(4, 0, "资源浏览量");
HisNumberLabel.setCellFormat(codeCF);
sheet.addCell(HisNumberLabel);
Label departNameLabel = new Label(5, 0, "所属部门/栏目");
departNameLabel.setCellFormat(codeCF);
sheet.addCell(departNameLabel);
if(userStatisticsList!=null && userStatisticsList.size()>0){
for (int i = 0; i < userStatisticsList.size(); i++) {
UserStatisticsVo statistics = userStatisticsList.get(i);
Label label_1 = new Label(0, i + 1, String.valueOf(i+1));
label_1.setCellFormat(codeCF);
sheet.addCell(label_1);
Label label_2 = new Label(1, i + 1, statistics.getUserName());
label_2.setCellFormat(codeCF);
sheet.addCell(label_2);
Label label_3 = new Label(2, i + 1, String.valueOf(statistics.getResourceNumber()));
label_3.setCellFormat(codeCF);
sheet.addCell(label_3);
Label label_4 = new Label(3, i + 1, String.valueOf(statistics.getResourceDownNumber()));
label_4.setCellFormat(codeCF);
sheet.addCell(label_4);
Label label_5 = new Label(4, i + 1,String.valueOf(statistics.getResourceHisNumber()));
label_5.setCellFormat(codeCF);
sheet.addCell(label_5);
Label label_6 = new Label(5, i + 1, statistics.getDepartmentName());
label_6.setCellFormat(codeCF);
sheet.addCell(label_6);
}
}
// 写入数据
wwb.write();
} catch (Exception e) {
logger.error(e);
} finally {
// 关闭文件
try {
if (wwb != null)
wwb.close();
} catch (WriteException e) {
logger.error(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error(e.getMessage());
}
}
}