Java使用jxl下载excel

一、使用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());
	   }
	}
		
}

备注:在jsp或者ftl中不要用post发送请求,最好可以用:window.location.href。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值