poi导出excel表格案例

private List<ChouyangDetail> listall;//这个是数据集合
@RequestMapping("/downloadYearInfo")
public void downloadDisDayInfo(String date, HttpServletRequest request,
		HttpServletResponse response) throws IOException {
	HSSFWorkbook work = new ExportExcel().exportExcel2("质量安全监测信息年报表",
			getHeader(), listall);
	OutputStream out = FileUtil.downloadExcel(date + "-质量安全信息年报表.xls",
			response);
	work.write(out);
	out.close();
}

private String[] getHeader() {
	// TODO Auto-generated method stub
	return new String[]{"级别","定量检测抽样总数","定量检测合格样品数","定量检测平均合格率","快速检测抽样总数","快速检测合格样品数","快速检测平均合格率","定量检测抽样总数","定量检测合格样品数","定量检测平均合格率","快速检测抽样总数","快速检测合格样品数","快速检测平均合格率","定量检测抽样总数","定量检测合格样品数","定量检测平均合格率","快速检测抽样总数","快速检测合格样品数","快速检测平均合格率","定量检测抽样总数","定量检测合格样品数","定量检测平均合格率","快速检测抽样总数","快速检测合格样品数","快速检测平均合格率"};
}

ExportExcel.java
public HSSFWorkbook exportExcel2(String title, String[] header,
			List<ChouyangDetail> dataset) {
		return exportExcel2(title, header, dataset, "yyyy-MM-dd");
	}

	public HSSFWorkbook exportExcel2(String title, String[] header,
			List<ChouyangDetail> dataset, String pattern) {
		HSSFWorkbook workbook = new HSSFWorkbook();//创建Excel工作簿对象
		Map<String, CellStyle> styles = createStyles(workbook);//设置excel样式 
		HSSFSheet sheet = workbook.createSheet(title);//创建Excel工作表对象(生成一个表格)  
		sheet.setDefaultColumnWidth((short) 15);// 设置表格默认列宽度为15个字节
		// 声明一个画图的顶级管理器
		// HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
		// // 定义注释的大小和位置,详见文档
		// HSSFComment comment = patriarch.createComment(new
		// HSSFClientAnchor(
		// 0, 0, 0, 0, (short) 4, 2, (short) 6, 5));
		// // 设置注释内容
		// comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));
		// // 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.
		// comment.setAuthor("hzh");

		HSSFRow row = sheet.createRow(0);// 产生表格标题行
		for (int j = 0; j < header.length; j++) {
			HSSFCell cell = row.createCell(j);//创建单元格
			cell.setCellStyle(styles.get("header"));//取得header样式
			HSSFRichTextString text = new HSSFRichTextString(header[j]);设置字体样式
			cell.setCellValue(text);//设置标题值
		}
		Iterator<ChouyangDetail> ite = dataset.iterator();//迭代数据集合
		int index = 1;//设置索引
		while (ite.hasNext()) {
			row = sheet.createRow(index);//创建一行
			ChouyangDetail item = ite.next();//ChouyangDetail:要导出内容的实体
			index++;
			HSSFCell cell1 = row.createCell(0);
			cell1.setCellStyle(styles.get("header"));
			HSSFRichTextString text = new HSSFRichTextString(
					item.getRegionname());
			cell1.setCellValue(text);
			for (int k = 1; k < header.length; k++) {
				HSSFCell cell = row.createCell(k);
				cell.setCellStyle(styles.get("cell"));
				switch (k) {
				case 1:
					text = new HSSFRichTextString(item.getTp().getDlcyzs());
					cell.setCellValue(text);
					break;
				case 2:
					text = new HSSFRichTextString(item.getTp().getDlyphgs());
					cell.setCellValue(text);
					break;
				case 3:
					text = new HSSFRichTextString(item.getTp().getDlhgl());
					cell.setCellValue(text);
					break;
				case 4:
					text = new HSSFRichTextString(item.getTp().getKscyzs());
					cell.setCellValue(text);
					break;
				case 5:
					text = new HSSFRichTextString(item.getTp().getKscyzs());
					cell.setCellValue(text);
					break;
				case 6:
					text = new HSSFRichTextString(item.getTp().getKshgl());
					cell.setCellValue(text);
					break;
				case 7:
					text = new HSSFRichTextString(item.getTa().getDlcyzs());
					cell.setCellValue(text);
					break;
				case 8:
					text = new HSSFRichTextString(item.getTa().getDlyphgs());
					cell.setCellValue(text);
					break;
				case 9:
					text = new HSSFRichTextString(item.getTa().getDlhgl());
					cell.setCellValue(text);
					break;
				case 10:
					text = new HSSFRichTextString(item.getTa().getKscyzs());
					cell.setCellValue(text);
					break;
				case 11:
					text = new HSSFRichTextString(item.getTa().getKsyphgs());
					cell.setCellValue(text);
					break;
				case 12:
					text = new HSSFRichTextString(item.getTa().getKshgl());
					cell.setCellValue(text);
					break;
				case 13:
					text = new HSSFRichTextString(item.getTa_ru().getDlcyzs());
					cell.setCellValue(text);
					break;
				case 14:
					text = new HSSFRichTextString(item.getTa_ru().getDlyphgs());
					cell.setCellValue(text);
					break;
				case 15:
					text = new HSSFRichTextString(item.getTa_ru().getDlhgl());
					cell.setCellValue(text);
					break;
				case 16:
					text = new HSSFRichTextString(item.getTa_ru().getKscyzs());
					cell.setCellValue(text);
					break;
				case 17:
					text = new HSSFRichTextString(item.getTa_ru().getKsyphgs());
					cell.setCellValue(text);
					break;
				case 18:
					text = new HSSFRichTextString(item.getTa_ru().getKshgl());
					cell.setCellValue(text);
					break;
				case 19:
					text = new HSSFRichTextString(item.getTf().getDlcyzs());
					cell.setCellValue(text);
					break;
				case 20:
					text = new HSSFRichTextString(item.getTf().getDlyphgs());
					cell.setCellValue(text);
					break;
				case 21:
					text = new HSSFRichTextString(item.getTf().getDlhgl());
					cell.setCellValue(text);
					break;
				case 22:
					text = new HSSFRichTextString(item.getTf().getKscyzs());
					cell.setCellValue(text);
					break;
				case 23:
					text = new HSSFRichTextString(item.getTf().getKsyphgs());
					cell.setCellValue(text);
					break;
				case 24:
					text = new HSSFRichTextString(item.getTf().getKshgl());
					cell.setCellValue(text);
					break;
				}
			}

		}
		return workbook;
	}
	// excel样式
	private Map<String, CellStyle> createStyles(Workbook wb) {
		Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
		CellStyle style = null;
		Font titleFont = wb.createFont();
		titleFont.setFontHeightInPoints((short) 18);
		titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
		style = wb.createCellStyle();
		//指定单元格居中对齐
		style.setAlignment(CellStyle.ALIGN_CENTER);
		//指定单元格垂直居中
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		style.setFont(titleFont);
		styles.put("title", style);

		style = wb.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		style.setWrapText(true);
		styles.put("header", style);

		style = wb.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		//自动换行
		style.setWrapText(true);
		styles.put("cell", style);

		style = wb.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		//设置单元格背景色
		style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
		style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
		styles.put("formula", style);

		style = wb.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
		style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
		styles.put("formula_2", style);

		return styles;
	}
	
	FileUtil.java
	/**
	 * 导出excle文件 downloadExcle
	 * 
	 * @author hanzh
	 * @param filename
	 * @param response
	 * @return OutputStream
	 */
	public static OutputStream downloadExcel(String filename,
			HttpServletResponse response) {
		OutputStream toClient = null;
		try {
			// 清空response
			response.reset();
			// 设置response的Header
			response.addHeader("Content-Disposition", "attachment;filename="
					+ new String(filename.getBytes("gbk"), "iso-8859-1")); 
			toClient = new BufferedOutputStream(response.getOutputStream());
			response.setContentType("application/vnd.ms-excel;charset=gb2312");
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return toClient;
	}

猜你喜欢

转载自blog.csdn.net/wzm1994/article/details/78557762