导出EXCEL

/**
	 * 导出EXCLE
	 * 
	 * @param response
	 * @param list
	 * @return boolean
	 */
	public boolean exportExcel(HttpServletResponse response, List<ProjectVO> list) {
		try {

			OutputStream os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition", "attachment; filename="
					+ java.net.URLEncoder.encode("项目管理", "utf-8") + ".xls");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型

			WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件
			String tmptitle = "项目列表"; // 标题
			WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // 工作单名称
			// 字体格式
			WritableFont wfont = new WritableFont(WritableFont.createFont("楷体_GB2312"), 16, WritableFont.BOLD, false,
					UnderlineStyle.NO_UNDERLINE, Colour.RED);

			// 单元格格式
			WritableCellFormat wcfFC = new WritableCellFormat(wfont);
			wcfFC.setAlignment(jxl.format.Alignment.CENTRE);// 文本在单元格中的位置
			wcfFC.setBackground(Colour.AQUA);// 单元格颜色

			wsheet.addCell(new Label(3, 0, tmptitle, wcfFC)); // 设置标题位置 label(列,行,填充的字符串,字体格式)

			// 开始生成主体内容
			wsheet.addCell(new Label(0, 1, "项目名称"));
			wsheet.addCell(new Label(1, 1, "项目简称"));
			wsheet.addCell(new Label(2, 1, "归属单位"));
			wsheet.addCell(new Label(3, 1, "主管领导"));
			wsheet.addCell(new Label(4, 1, "预算"));
			wsheet.addCell(new Label(5, 1, "计划开始日期"));
			wsheet.addCell(new Label(6, 1, "计划结束日期"));
			wsheet.addCell(new Label(7, 1, "状态"));
			wsheet.addCell(new Label(8, 1, "备注"));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			for (int i = 0; i < list.size() + 9; i++) {
				wsheet.setRowView(i, 500);// 每一行的高
				wsheet.setColumnView(i, 20);// 每一行的宽
			}
			for (int i = 0; i < list.size(); i++) {
				wsheet.addCell(new Label(0, i + 2, list.get(i).getProjectName()));
				wsheet.addCell(new Label(1, i + 2, list.get(i).getProjectAbbreviation()));
				wsheet.addCell(new Label(2, i + 2, list.get(i).getProjectUnits()));
				wsheet.addCell(new Label(3, i + 2, list.get(i).getProjectManagement()));
				wsheet.addCell(new Label(4, i + 2, String.valueOf(list.get(i).getProjectDudget())));
				wsheet.addCell(new Label(5, i + 2, sdf.format(list.get(i).getProjectStartDate())));
				wsheet.addCell(new Label(6, i + 2, sdf.format(list.get(i).getProjectFinishDate())));
				String statu = "未知";
				if (list.get(i).getProjectStatus() == 1) {
					statu = "已完成";
				} else if (list.get(i).getProjectStatus() == 2) {
					statu = "未完成";
				} else {
					statu = "正在进行";
				}
				wsheet.addCell(new Label(7, i + 2, statu));
				wsheet.addCell(new Label(8, i + 2, list.get(i).getProjectRemark()));
			}
			// 主体内容生成结束
			wbook.write(); // 写入文件
			wbook.close();
			os.close(); // 关闭流
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}

JXL.JAR的下载路径为:http://www.andykhan.com/jexcelapi/download.html
参考文档:http://kingsui.iteye.com/blog/154712

你可能感兴趣的:(.net,Excel,OS,项目管理,ITeye)