struts2 导出excel扩展名为.action

public void materialAgeList(List materialList, String path) {
		try {
            //设置响应头(以下这一坨)
			HttpServletResponse response = ServletActionContext.getResponse();
			response.reset();
			response.setContentType("application/vnd.ms-excel");
			String fileName = java.net.URLEncoder.encode("仓库物资账龄.xls", "UTF-8");
			response.setHeader("Content-Disposition", "attachment; filename=" 
					+ new String(fileName.getBytes("UTF-8"), "GBK"));
			OutputStream os = response.getOutputStream();
                        

			File file = new File(path);
			WritableWorkbook wwb;
			wwb = Workbook.createWorkbook(file);
			WritableSheet ws = wwb.createSheet("仓库物资账龄", 0);

			// 创建单元格样式
			WritableCellFormat wcf = new WritableCellFormat();
			WritableCellFormat wcf1 = new WritableCellFormat(
					new WritableFont(WritableFont.ARIAL, 15, WritableFont.BOLD,
							false, UnderlineStyle.NO_UNDERLINE,
							jxl.format.Colour.RED));
			wcf.setAlignment(Alignment.LEFT);
			wcf.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); 
			wcf1.setAlignment(Alignment.CENTRE);
			wcf1.setVerticalAlignment(VerticalAlignment.CENTRE);
			wcf1.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); 
			// 设置表头
			ws.mergeCells(0, 0, 4, 1);//设置第一列、第一行和 第一列、第二行合并
			ws.addCell(new Label(0, 0, "物资账龄", wcf1));
			String[] columns = { "物资编号", "物资名称", "物资数量", "物资价格", "账龄"};
			for (int i = 0; i < columns.length; i++) {
				// 设置列宽
				ws.setColumnView(i, 30);
				ws.addCell(new Label(i, 2, columns[i], wcf));
			}
			for (int i = 0; i < materialList.size(); i++) {
				Map map = (Map) materialList.get(i);
				ws.addCell(new Label(0, i + 3, changeToStr(map.get("物资编号")), wcf));
				ws.addCell(new Label(1, i + 3, changeToStr(map.get("物资名称")), wcf));
				ws.addCell(new Label(2, i + 3, changeToStr(map.get("物资数量")), wcf));
				ws.addCell(new Label(3, i + 3, changeToStr(map.get("物资价格")), wcf));
				ws.addCell(new Label(4, i + 3, changeToStr(map.get("账龄")), wcf));
			}
			wwb.write();
			wwb.close();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值