数据集导出到excel文件

本文介绍如何从数据库查询获取数据集,并将其详细步骤记录下来,内容包括将数据写入Excel文件以及设置导出时的Excel样式。适用于需要将大量数据整理成表格格式的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说明:从数据库查询数据集写入数据到excel文件,并设置excel导出样式。记录一下,不喜勿喷。

@RequestMapping("/fileDown")
	public @ResponseBody
	WrappedResult fileDown(@RequestParam(value = "appNoQfbd") String appNoQfbd,@RequestParam(value = "eventCode") String eventCode,
			@RequestParam(value = "ym") String ym,HttpServletResponse response,HttpServletRequest request) throws Exception {
		GasfeeComCondition condition = new GasfeeComCondition();
		condition.setAppNoQfbd(appNoQfbd);
		condition.setEventCode(eventCode);
		condition.setYm(ym);
		gasfeeComService.fileDown(response, condition);
		return new WrappedResult(true, true, "");
	}

@SuppressWarnings("deprecation")
	public void fileDown(HttpServletResponse response, GasfeeComCondition condition) throws IOException {
		// create HSSFWorkbook  EXCEL设置部分
		HSSFWorkbook workbook = new HSSFWorkbook();
		//默认黑色字体
		HSSFFont font = workbook.createFont();
		font.setFontName("宋体");
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 粗体
		font.setFontHeightInPoints((short) 11);//字号
		//红色字体
		HSSFFont redFont = workbook.createFont();
		redFont.setFontName("宋体");
		//redFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 粗体
		redFont.setColor(HSSFFont.COLOR_RED);
		redFont.setFontHeightInPoints((short) 10);//字号
		//单元格样式(非必填)
		HSSFCellStyle titleStyleNo = workbook.createCellStyle();
		//灰色填充
		titleStyleNo.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
		titleStyleNo.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		titleStyleNo.setFont(font);
		titleStyleNo.setBorderBottom((short)1);//下边框     
		titleStyleNo.setBorderLeft((short)1);//左边框     
		titleStyleNo.setBorderRight((short)1);//右边框     
		titleStyleNo.setBorderTop((short)1);//上边框
		titleStyleNo.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中    
		titleStyleNo.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		//单元格样式(必填)
		HSSFCellStyle titleStyleYes = workbook.createCellStyle();
		//灰色填充
		titleStyleYes.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
		titleStyleYes.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		titleStyleYes.setFont(redFont);
		titleStyleYes.setBorderBottom((short)1);//下边框     
		titleStyleYes.setBorderLeft((short)1);//左边框     
		titleStyleYes.setBorderRight((short)1);//右边框     
		titleStyleYes.setBorderTop((short)1);//上边框
		titleStyleYes.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中    
		titleStyleYes.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		List<GasQfbdLogVo> listGasLog = null;
		// 创建sheet页
		HSSFSheet sheet = workbook.createSheet();
		String fileName = "";
		String[] headList = new String[]{};
		if("01".equals(condition.getEventCode())){
			headList = new String[]{"工单号","年月","用户号","错误代码","错误内容"};
			listGasLog = dao.fileDataObtain(condition);
			fileName = "数据获取异常明细.xls";
			workbook.setSheetName(0, "数据获取异常明细");
		}
		//设置表格宽度
		for(int i = 0;i < headList.length; i++){
			sheet.setColumnWidth((short) i, (short) 5766);
		}
		//设置第一行标题列
		HSSFRow rowTitleList = sheet.createRow(0);
		for(int i = 0 ;i < headList.length; i++){
			HSSFCell cellTitleList_0 = rowTitleList.createCell((short) i);
			cellTitleList_0.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellTitleList_0.setCellStyle(titleStyleNo);
			cellTitleList_0.setCellValue(headList[i]);
		}
		//写入数据到excel
		if(listGasLog != null){
			for(int j = 0 ;j < listGasLog.size();j++){
				HSSFRow row = sheet.createRow(j + 1);
				for(int n = 0 ;n < headList.length;n++){
					HSSFCell cellData = row.createCell((short) n);
					cellData.setCellType(HSSFCell.CELL_TYPE_STRING);
					cellData.setCellStyle(titleStyleYes);
					if(n == 0){
						cellData.setCellValue(listGasLog.get(j).getAppNoQfbd());
					}
					if(n == 1){
						cellData.setCellValue(listGasLog.get(j).getYm());
					}
					if(n == 2){
						cellData.setCellValue(listGasLog.get(j).getConsNo());
					}
					if(n == 3){
						cellData.setCellValue(listGasLog.get(j).getOutCode());
					}
					if(n == 4){
						cellData.setCellValue(listGasLog.get(j).getOutMsg());
					}
				}
			}
		}
		//弹出下载对话框
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.addHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"utf-8"));
		OutputStream out = response.getOutputStream();
		workbook.write(out);
		//清空缓冲区
		out.flush();
		out.close();
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值