Java导出Excel表格(超全注释)

注意事项!此方法必须使用表单提交参数!

public void ExportCwInfo(HttpServletRequest request, HttpServletResponse response) {

		String themeId = request.getParameter("themeId");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("themeId", themeId);
		paraMap.put("startTime", startTime);
		paraMap.put("endTime", endTime);
		List<Map<String, Object>> cwList = service.exportCwInfo(paraMap);

		// ------------------------------------样式设置开始------------------------------------
		// 创建工作簿
		final XSSFWorkbook wb = new XSSFWorkbook();
		// 分页名称
		final XSSFSheet sheet = wb.createSheet("结果");
		// 生成一种样式
		final XSSFCellStyle style = wb.createCellStyle();
		// 设置样式
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		// 生成一种字体
		final XSSFFont font = wb.createFont();
		// 设置字体
		font.setFontName("微软雅黑");
		// 设置字体大小
		font.setFontHeightInPoints((short) 12);
		// 字体加粗
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		// 在样式中引用这种字体
		style.setFont(font);

		// 生成并设置另一个样式
		final XSSFCellStyle style2 = wb.createCellStyle();
		style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

		// 生成另一种字体2
		final XSSFFont font2 = wb.createFont();
		// 设置字体
		font2.setFontName("微软雅黑");
		// 设置字体大小
		font2.setFontHeightInPoints((short) 12);
		// 在样式2中引用这种字体
		style2.setFont(font2);
		// 设置列宽
		sheet.setColumnWidth(0, 22 * 256);
		sheet.setColumnWidth(1, 10 * 256);
		sheet.setColumnWidth(2, 5 * 256);
		sheet.setColumnWidth(3, 28 * 256);
		sheet.setColumnWidth(4, 12 * 256);
		sheet.setColumnWidth(5, 25 * 256);
		// ------------------------------------样式设置结束------------------------------------
		// ------------------------------------填充表头开始------------------------------------
		final String[] excelHeaderArr = {"身份证号", "姓名", "性别", "专业", "金额", "支付时间"};
		// 生成表格第一行 第一行表头
		final XSSFRow row = sheet.createRow(0);
		XSSFCell cell = null;
		for (int i = 0; i < excelHeaderArr.length; i ++ ) {
			cell = row.createCell(i);
			// 设置数值
			cell.setCellValue(excelHeaderArr[i]);
			// 设置样式
			cell.setCellStyle(style);
		}
		// ------------------------------------填充表头结束------------------------------------
		// ------------------------------------设置表体开始------------------------------------

		for (int i = 0; i < cwList.size(); i ++ ) {
			final XSSFRow row1 = sheet.createRow(i + 1);
			cell = row1.createCell(0);
			cell.setCellValue(cwList.get(i).get("colum_1").toString());
			cell.setCellStyle(style2);

			cell = row1.createCell(1);
			cell.setCellValue(cwList.get(i).get("colum_2").toString());
			cell.setCellStyle(style2);

			cell = row1.createCell(2);
			cell.setCellValue(cwList.get(i).get("colum_3").toString());
			cell.setCellStyle(style2);

			cell = row1.createCell(3);
			cell.setCellValue(cwList.get(i).get("colum_4").toString());
			cell.setCellStyle(style2);

			cell = row1.createCell(4);
			cell.setCellValue(cwList.get(i).get("amount").toString());
			cell.setCellStyle(style2);

			cell = row1.createCell(5);
			cell.setCellValue(cwList.get(i).get("gengxin_date").toString());
			cell.setCellStyle(style2);
		}
		// ------------------------------------设置表体结束------------------------------------
		ExcelExportUtil.downloadExcel(response, wb, "cwInfo.xlsx");

下载Excel方法 :

public static void downloadExcel(final HttpServletResponse response, final Workbook workBook, final String filename) {

		try {
			response.addHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
			response.setContentType("application/octet-stream");

			final OutputStream stream = new BufferedOutputStream(response.getOutputStream());
			workBook.write(stream);
			stream.close();
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值