java 导出excel

页面:

$("#exportBtn").live("click", function () {
               
            	window.open('/exportMoneyConsume?type=1&incident=1');
            });

后台:

controller

	@Get("/exportMoneyConsume")
	@LoginCheckRequired
	public String exportMoneyConsume(Invocation inv,MoneyConsumeView param) throws Exception {
		
		
		String fileName="adf"+System.currentTimeMillis()+".xls";
		String path=Constants.UPLOAD_BASE_FOLD +"/"+fileName;
			
		
		exportMoneyConsume(path,param);
		
		downLoad(inv, fileName, path);
		
		return "@";
	}

Service:

public void exportMoneyConsume(String path, MoneyConsumeView param) {

		List<MoneyConsumeView> list = MoneyConsumeDAO.exportMoneyConsume(param);

		// 1.创建Excel工作薄对象
		HSSFWorkbook wb = new HSSFWorkbook();
		// 2.创建Excel工作表对象
		HSSFSheet sheet = wb.createSheet("new Sheet");

		// 3.创建单元格样式
		CellStyle cellStyle = wb.createCellStyle();
		// 设置这些样式
		cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		sheet.setDefaultColumnWidth(20);

		int i = 0;
		// 4.创建Excel工作表的行
		HSSFRow row = sheet.createRow(i);
		row.createCell(0).setCellValue("");
		row.createCell(1).setCellValue("aa");
		row.createCell(2).setCellValue("bb");
		row.createCell(3).setCellValue("df");
		row.createCell(4).setCellValue("fasasf");
		row.createCell(5).setCellValue("adsf");
		row.createCell(6).setCellValue("asdf");
		row.createCell(7).setCellValue("asdf");
		row.createCell(8).setCellValue("asdf");
		row.createCell(9).setCellValue("asfd");
		row.createCell(10).setCellValue("safd");
		row.createCell(11).setCellValue("asfd");

		for (MoneyConsumeView moneyConsume : list) {

			if (moneyConsume.getLevel() == null) {
				continue;
			}

			String fileName = SchoolLevelEnum.getName(moneyConsume.getLevel());
			if (StringUtils.isNotBlank(fileName)) {
				moneyConsume.setLevelName(fileName);
			}

			row = sheet.createRow(i + 1);
			row.createCell(0).setCellValue(i + 1);
			row.createCell(1).setCellValue(areaCacheService.getAreaNameCache(moneyConsume.getAreaCode()));
			row.createCell(2).setCellValue(moneyConsume.getLevelName());
			row.createCell(3).setCellValue(schoolCacheService.getSchoolNameCache(moneyConsume.getSchoolId()));
			row.createCell(4).setCellValue(moneyConsume.getUserName());
			row.createCell(5)
					.setCellValue(DateUtils.formatDate(moneyConsume.getCreateTime(), DateUtils.FORMAT_DAY_TIME));
			row.createCell(6).setCellValue(moneyConsume.getName());
			row.createCell(7).setCellValue(moneyConsume.getPrice());
			row.createCell(8).setCellValue(moneyConsume.getStudentCount());
			row.createCell(9).setCellValue(moneyConsume.getDiscount());
			row.createCell(10).setCellValue(moneyConsume.getPayAmount());
			if(moneyConsume.getPresentBalance()!=null){
				row.createCell(11).setCellValue(moneyConsume.getPresentBalance());
			}else{
				row.createCell(11).setCellValue(0);
			}
			

			i = i + 1;

		}

		// 设置sheet名称和单元格内容
		wb.setSheetName(0, "第一张工作表");
		// 设置单元格内容 cell.setCellValue("单元格内容");

		// 最后一步,将文件存到指定位置

		try {
			FileOutputStream fout = new FileOutputStream(path);
			wb.write(fout);
			fout.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

public void downLoad(Invocation inv, String fileName, String path) throws IOException {
		
		File file = new File(path);// 构造要下载的文件
		if (file.exists()) {

		    InputStream ins = null;
			BufferedInputStream bins = null;
			OutputStream outs = null;
			BufferedOutputStream bouts = null;

			try {

				ins = new FileInputStream(path);// 构造一个读取文件的IO流对象
				bins = new BufferedInputStream(ins);// 放到缓冲流里面
				outs = inv.getResponse().getOutputStream();// 获取文件输出IO流
				bouts = new BufferedOutputStream(outs);

				inv.getResponse().setContentType("application/x-download");// 设置response内容的类型
				inv.getResponse().setHeader("Content-disposition",
						"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));// 设置头部信息

				int bytesRead = 0;
				byte[] buffer = new byte[8192];

				// 开始向网络传输文件流
				while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {

					bouts.write(buffer, 0, bytesRead);
				}
				bouts.flush();// 这里一定要调用flush()方法


			} catch (Exception e) {

				e.printStackTrace();
			} finally {

				if (bouts != null) {

					bouts.close();
					bouts = null;
				}
				if (outs != null) {
					
					outs.close();
					outs = null;
				}
				
				if (bins != null) {

					bins.close();
					bins = null;
				}
				if (ins != null) {

					ins.close();
					ins = null;
				}

				file.delete();
			}

		} else {
			logger.info("导出的文件不存在");
		}
		
	}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值