利用jxl在java程序中导出数据到excel的例子

public String showDetail() throws Exception {
		// 当前用户查询范围
		SysUsers loginUser = (SysUsers) session.get("loginUser");
		String queryScope = loginUser.getQueryScope();
		String[] orgScope = queryScope.split(",");

		Map map = new HashMap();
		map.put("orgScope", orgScope);
		map.put("mobtypeid", mobtypeid);

		// 明细查询
		detailList = dao.queryList("mobStock.getDetail", map);

		// 按excel方式展示数据
		if ("excel".equals(detailType)) {
			OutputStream os = response.getOutputStream();	//取得输出流
			response.setHeader("Content-disposition", "attachment; filename=detail.xls");// 设定输出文件头   
			response.setContentType("application/msexcel");	//定义输出类型

			WritableWorkbook wb = Workbook.createWorkbook(os);	//建立excel文件
			WritableSheet sheet = wb.createSheet("detail", 0);	//建立sheet名称

			sheet.addCell(new Label(0, 0, "手机型号"));
			sheet.addCell(new Label(1, 0, "串号一"));
			sheet.addCell(new Label(2, 0, "串号二"));
			sheet.addCell(new Label(3, 0, "入库价格"));
			Iterator<StoMobstock> detailIterator = detailList.iterator();
			int rowNo = 1;
			while (detailIterator.hasNext()) {
				StoMobstock mobstock = detailIterator.next();
				sheet.addCell(new Label(0, rowNo, mobstock.getMobtypeid()));
				sheet.addCell(new Label(1, rowNo, mobstock.getSn1()));
				sheet.addCell(new Label(2, rowNo, mobstock.getSn2()));
				sheet.addCell(new Number(3, rowNo, mobstock.getBuyprice()
						.doubleValue(), new WritableCellFormat(
						new NumberFormat("#,###.00"))));  // 以指定格式显示数字
				rowNo++;
			}
			wb.write();
			wb.close();
			return null;
		} else {
			return "detail";
		}
	}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值