Java数据导出为Excel表格

		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFCell cell;
		HSSFSheet sheet = wb.createSheet("结果导出");

		// /////////设置行高 row0.setHeight((short)780);
		// /////////设置列宽 sheet.setColumnWidth((short)1,(short)5900);

		HSSFCellStyle cellStyle = setCellStyle(wb);
		HSSFFont font = setFontStyle(wb, (short) 15, " 宋体 "); // 设置字体
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		cellStyle.setFont(font);
		cellStyle.setVerticalAlignment((short) 1); // 单元格垂直 0 居上 1 居中 2 居下 3 正当
		cellStyle.setWrapText(true);

		HSSFCellStyle cellStyle1 = setCellStyle(wb);
		HSSFFont font1 = setFontStyle(wb, (short) 12, " 宋体 ");
		font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		cellStyle1.setFont(font1);
		cellStyle1.setVerticalAlignment((short) 1);
		cellStyle1.setWrapText(true);

		HSSFCellStyle cellStyle2 = setCellStyle(wb);
		HSSFFont font2 = setFontStyle(wb, (short) 12, " 宋体 ");
		cellStyle2.setFont(font2);
		cellStyle2.setVerticalAlignment((short) 1);
		cellStyle2.setWrapText(true);

		HSSFCellStyle cellStyle3 = setCellStyle(wb);
		HSSFFont font3 = setFontStyle(wb, (short) 10, " 宋体 ");
		cellStyle3.setFont(font3);
		cellStyle3.setVerticalAlignment((short) 1);
		cellStyle3.setWrapText(true);

		HSSFCellStyle cellStyle4 = setCellStyle(wb);
		HSSFFont font4 = setFontStyle(wb, (short) 10, " 宋体 ");
		cellStyle4.setFont(font4);
		cellStyle4.setVerticalAlignment((short) 1);
		cellStyle4.setWrapText(true); // 设置单元格自动换行
		cellStyle4.setAlignment((short) 2); // 单元格水平 0普通 1 左对齐2 居中3 右对齐4 填充5 正当6
											// 居中选择
		DealString ds = new DealString();

		HSSFCellStyle cellStyle5 = setCellStyle(wb);
		HSSFFont font5 = setFontStyle(wb, (short) 10, " 宋体 ");
		font5.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		cellStyle5.setFont(font5);
		cellStyle5.setVerticalAlignment((short) 1);
		cellStyle5.setWrapText(true); // 设置单元格自动换行
		cellStyle5.setAlignment((short) 2); // 单元格水平 0普通 1 左对齐2 居中3 右对齐4 填充5 正当6
											// 居中选择

		HSSFRow row1 = sheet.createRow((short) 0);
		HSSFRow row;

		cell = row1.createCell((short) 0);
		cell.setCellValue("课题名");
		cell.setCellStyle(cellStyle5);
		sheet.setColumnWidth((short) 0, (short) 3900);

		cell = row1.createCell((short) 1);
		cell.setCellValue("课题性质名称");
		cell.setCellStyle(cellStyle5);
		sheet.setColumnWidth((short) 1, (short) 3900);
                 //shuju chaxun return rslist
                 for (int i = 0; i < rslist.size(); i++) {
			HashMap xxmap = (HashMap) rslist.get(i);
			row = sheet.createRow((short) (1 + i));

			cell = row.createCell((short) 0);
			cell.setCellValue(ds.toString(xxmap.get("ktmc")));
			cell.setCellStyle(cellStyle4);

			cell = row.createCell((short) 1);
			cell.setCellValue(ds.toString(xxmap.get("ktxzmc")));
			cell.setCellStyle(cellStyle4);
		}

		response.reset();
		response.setContentType("application/vnd.ms-excel");
		response.addHeader("Content-Disposition", "attachment; filename="
				+ new String("学生基本信息导出.xls".getBytes(), "ISO8859-1"));
		wb.write(response.getOutputStream());
		response.getOutputStream().close();

你可能感兴趣的:(Java学习)