导出Excel功能的实现

	function exportExcel(){
			if(confirm('确认导出数据')){
				$("#form1").attr("action","export-excel.action").submit();

				}else{
					return false;
					}
		
	}
 

在ExportExcel.java类里

@Action(results={@Result(name="success",type="stream",params={
			"contentType","application/octet-stream",
				"inputName","fileInputStream",
					"contentDisposition","attachment;filename=${fileName}.xls",
						"bufferSize","1024"
							}
						)
					}
	)
	
	@Override
	public String execute() throws Exception{
		System.out.println("aaaaaaaaaaaaaa");
		System.out.println(lotteryId);
		LotteryPeriod lotteryPeriod = this.lotteryPeriodService.findById(lotteryId);
		DetachedCriteria dc = this.lotteryPrizeService.createCriteria();
		dc.add(Restrictions.eq("lotteryPeriod", lotteryPeriod));
		List<LotteryPrize> lotPrize = this.lotteryPrizeService.findByDetachedCriteria(dc);
		
		DetachedCriteria dc2 = this.prizeListService.createCriteria();
		dc2.add(Restrictions.in("lotteryPrize", lotPrize));
		if(!StringUtils.isEmpty(user_num)){
			dc2.add(Restrictions.ilike("userNum",user_num.trim(),MatchMode.ANYWHERE ));
		}
		if(!StringUtils.isEmpty(user_name)){
			dc2.add(Restrictions.ilike("userName",user_name.trim(),MatchMode.ANYWHERE ));
		}
		if(!StringUtils.isEmpty(weibo_url)){
			dc2.add(Restrictions.ilike("weiboUrl",weibo_url.trim(),MatchMode.ANYWHERE ));
		}
		if(!StringUtils.isEmpty(contact_phone)){
			dc2.add(Restrictions.ilike("contactPhone",contact_phone.trim(),MatchMode.ANYWHERE ));
		}
		dc.addOrder(Order.desc("createTime"));
		List<PrizeList> priList=this.prizeListService.findByDetachedCriteria(dc2);
		
		Sheet sheet = workBook.createSheet();
	    HSSFCellStyle style=workBook.createCellStyle();
	    style.setFillForegroundColor(HSSFColor.BLACK.index);
	    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		 
		Row metaRow = sheet.createRow(0);
		//Date nowDate = new Date();
		//创建表头
		metaRow.createCell(0).setCellValue("编号");
		metaRow.createCell(1).setCellValue("姓名");
		metaRow.createCell(2).setCellValue("微博链接");
		metaRow.createCell(0).setCellValue("联系号码");
		metaRow.createCell(1).setCellValue("抽奖号");
		metaRow.createCell(2).setCellValue("是否已经抽奖");
		metaRow.createCell(0).setCellValue("所中奖项");
		metaRow.createCell(1).setCellValue("抽奖日期");
		//把查询出的数据编辑到excel中
		if(priList!=null&&priList.size()>0){
			int rowNum=1;
			for(int i=0;i<priList.size();i++){
				PrizeList obj = (PrizeList) priList.get(i);
				Row row = sheet.createRow(rowNum);
				
				row.createCell(0).setCellValue(rowNum);
				row.createCell(1).setCellValue(obj.getLotteryNo());
				row.createCell(2).setCellValue(obj.getUserNum());
				row.createCell(3).setCellValue(obj.getUserName());
				row.createCell(4).setCellValue(obj.getWeiboUrl());
				row.createCell(5).setCellValue(obj.getContactPhone());
				rowNum++;
			}
		}else{
			System.out.println("错误");
		}
		return super.execute();
	}
	
	/**
	 * 定义"contentDisposition","attachment;filename=${fileName}.xls",
	 * 中${fileName}属性来源,需要转成8850编码
	 * @return
	 * @throws ParseException 
	 */
	public String getFileName() throws ParseException {
		try {
				DateFormat formatSuffix = new SimpleDateFormat(
						"yyyy-MM-dd");
				this.fileNameSuffix = 
					formatSuffix.format(new Date());
			return new String(("中奖名单列表").getBytes("GBK"), "ISO8859-1" );	//实际环境要GBK
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "export";
	}
	
	
	public InputStream getFileInputStream(){
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] result = null;
		try {
			workBook.write(baos);
			baos.flush(); 
	        result = baos.toByteArray();
	        baos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	
		return new ByteArrayInputStream(result, 0, result.length);
	}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
导出Excel功能可以通过以下步骤实现: 1. 引入Excel导出库,比如Apache POI或者EasyExcel。 2. 在后端编写控制器,处理导出Excel的请求。根据业务需求,可以从数据库中查询数据或者从其他接口获取数据。 3. 将数据转换成Excel格式,生成Excel文件。 4. 将生成的Excel文件返回给前端,供用户下载。 以下是一个使用Apache POI实现导出Excel的示例代码: ```java // 引入Apache POI库 import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; // 处理导出Excel请求的控制器 @RestController public class ExcelController { @GetMapping("/export") public void exportExcel(HttpServletResponse response) throws Exception { // 从数据库中查询数据 List<User> userList = userService.getAllUsers(); // 创建Excel工作簿 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("User List"); // 创建表头 Row headerRow = sheet.createRow(0); headerRow.createCell(0).setCellValue("ID"); headerRow.createCell(1).setCellValue("Name"); headerRow.createCell(2).setCellValue("Age"); // 填充数据 int rowNum = 1; for (User user : userList) { Row row = sheet.createRow(rowNum++); row.createCell(0).setCellValue(user.getId()); row.createCell(1).setCellValue(user.getName()); row.createCell(2).setCellValue(user.getAge()); } // 设置响应头 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-Disposition", "attachment; filename=user-list.xlsx"); // 写入响应流 OutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); } } ``` 以上代码使用了XSSFWorkbook类创建Excel工作簿,并使用createRow和createCell方法创建行和单元格。最后将生成的Excel文件通过响应流返回给前端,供用户下载。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值