Java的Excel输出(HSSFWorkbook)

@Controller
public class DownLoadExcelController extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@RequestMapping(value = "/downloadExcel.io")
	public void downloadExcel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		//本质是获得模板后改写输出
		try {
			CheckResult checkResult = (CheckResult) request.getSession()
					.getAttribute("checkResult");

			// 为空判断
			if (checkResult == null || checkResult.getErrorList() == null
					|| checkResult.getErrorList().size() == 0) {

				return;
			}
			// excel模板路径
			@SuppressWarnings("deprecation")
			String path = request.getRealPath("./")
					+ "/excelTemplate/template.xls";

			InputStream in = new FileInputStream(new File(path));
			HSSFWorkbook work = new HSSFWorkbook(in);
			// 得到excel的第1张个sheet
			HSSFSheet sheet = work.getSheetAt(0);

			HSSFRow row = sheet.getRow(1);
			// 文件名
			String fileName = request.getSession().getAttribute("fileName") == null ? null
					: (String) request.getSession().getAttribute("fileName");
			//第二行第二格输出文件名
			row.getCell(1).setCellValue("文件名:" + fileName);

			StringBuffer sbf = new StringBuffer();
			sbf.append("统计结果 错误:");
			sbf.append(checkResult.getCountError());
			sbf.append("处 警告:");
			sbf.append(checkResult.getCountWarning());
			sbf.append("处");
			row = sheet.getRow(2);
			// 第三行第二格输出结果统计
			row.getCell(1).setCellValue(sbf.toString());
			// 获取初始位置
			int i = 5;

			row = sheet.getRow(5);
			HSSFCellStyle column1 = row.getCell(1).getCellStyle();
			column1.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平对齐方式指定为居中
			HSSFCellStyle column2 = row.getCell(2).getCellStyle();
			column2.setWrapText(true);// 文字自动换行

			// 循环结果
			for (ResultDetail resultDetail : checkResult.getErrorList()) {
				row = sheet.createRow(i);
				// 样式 第二列居中, 3456自动换行
				row.createCell(1).setCellStyle(column1);
				row.createCell(2).setCellStyle(column2);
				row.createCell(3).setCellStyle(column2);
				row.createCell(4).setCellStyle(column2);
				row.createCell(5).setCellStyle(column2);
				// 第二列输出行番
				row.getCell(1).setCellValue(resultDetail.getLineNo());

				if (resultDetail.getDetailInfoList() != null
						&& resultDetail.getDetailInfoList().size() > 0) {
					// 代码内容
					row.getCell(2).setCellValue(
							resultDetail
									.getCode()
									.replace(" ", " ")
									.replace("<", "<")
									.replace(">", ">")
									.replaceAll("    ",
											"\t"));

					StringBuffer warningMsg = new StringBuffer();
					StringBuffer errorMsg = new StringBuffer();
					StringBuffer treatment = new StringBuffer();
					for (DetailInfo detailInfo : resultDetail
							.getDetailInfoList()) {
						//为空的时候,跳过循环
						if(null == detailInfo.getErrorMessage() || "".equals(detailInfo.getErrorMessage())){
							continue;
						}
						if (detailInfo.getStatus() != null
								&& 1 == detailInfo.getStatus()) {
							// warning的情况
							warningMsg.append(detailInfo.getErrorMessage());
							warningMsg.append("\n");
						} else if (detailInfo.getStatus() != null
								&& 2 == detailInfo.getStatus()) {
							// error的情况
							errorMsg.append(detailInfo.getErrorMessage());
							errorMsg.append("\n");
						} else {
							// 处理办法
							treatment.append(detailInfo.getErrorMessage());
							treatment.append("\n");
						}
					}
					// warning
					row.getCell(3).setCellValue(warningMsg.toString());
					// error
					row.getCell(4).setCellValue(errorMsg.toString());
					// 处理办法
					row.getCell(5).setCellValue(treatment.toString());
				}
				i++;
			}

			HSSFCellStyle cellStyle = work.createCellStyle();
			cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			cellStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
			//输出文件(提供下载)
			try {
				response.addHeader("Content-Disposition",
						"attachment;filename=" + fileName + "_result.xls");
				OutputStream os = new BufferedOutputStream(
						response.getOutputStream());
				response.setContentType("application/vnd.ms-excel;charset=gb2312");
				work.write(os);
				os.flush();
				os.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}


http://blog.youkuaiyun.com/jackyxwr/article/details/5698065

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值