`

java 导出excel

    博客分类:
  • java
 
阅读更多
public boolean exportExcel(LogBean[] beanlist,
			HttpServletResponse response) throws IOException, WriteException {
		OutputStream os = null;
		response.reset(); // 清空输出流
		os = response.getOutputStream(); // 取得输出流
		String fileName = "日志管理.xls";// 导出文件名
		response.setHeader("Content-disposition", "attachment; filename="
				+ new String(fileName.getBytes(), "ISO-8859-1")); // 设定输出文件头

		response.setContentType("application/msexcel"); // 定义输出类型

		// 生成一个 Exc 的工作空间
		// 创建新的Excel 工作簿
		WritableWorkbook workbook = Workbook.createWorkbook(os);
		// 生成 Exc 的Sheet,Sheet名为通讯录导出
		WritableSheet sheet = workbook.createSheet("日志", 0);
		// 这里是对Cell里面的字体进行格式化
		// WritableFont wfc = new WritableFont(WritableFont.ARIAL, 10,
		// WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
		// jxl.format.Colour.BLACK);
		// WritableCellFormat format = new WritableCellFormat(wfc);
		WritableCellFormat wcfFC = new jxl.write.WritableCellFormat();
		// 行居中
		wcfFC.setAlignment(Alignment.CENTRE);
		// 列居中
		wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);

		Label labelCk = null;

		// 编号 	用户 	IP 	类型 	信息 	记录时间 
		int colindex = 0;
		labelCk = new Label(colindex, 0, "编号", wcfFC);
		sheet.addCell(labelCk);
		sheet.setColumnView(colindex, 10);
		colindex++;

		labelCk = new Label(colindex, 0, "用户 ", wcfFC);
		sheet.addCell(labelCk);
		sheet.setColumnView(colindex, 20);
		colindex++;

		labelCk = new Label(colindex, 0, "IP", wcfFC);
		sheet.addCell(labelCk);
		sheet.setColumnView(colindex, 15);
		colindex++;

		labelCk = new Label(colindex, 0, "类型", wcfFC);
		sheet.addCell(labelCk);
		sheet.setColumnView(colindex, 20);
		colindex++;

		labelCk = new Label(colindex, 0, "信息", wcfFC);
		sheet.addCell(labelCk);
		sheet.setColumnView(colindex, 40);
		colindex++;

		labelCk = new Label(colindex, 0, "记录时间", wcfFC);
		sheet.addCell(labelCk);
		sheet.setColumnView(colindex, 40);
		colindex++;
 
		if (beanlist != null) {
			int rowIndex = 1;
			for (LogBean bean : beanlist) {
				if (bean == null)
					continue;
				int row_colindex = 0;
				// 编号 	用户 	IP 	类型 	信息 	记录时间 
				String order = bean.getId() == null ? "" : bean.getId();
				labelCk = new Label(row_colindex, rowIndex, order, wcfFC);
				sheet.addCell(labelCk);
				row_colindex++;
				
				String userName = bean.getUserName() == null ? ""
						: bean.getUserName();
				labelCk = new Label(row_colindex, rowIndex, userName,
						wcfFC);
				sheet.addCell(labelCk);
				row_colindex++;

				String ip = bean.getIp()== null ? ""
						: bean.getIp();
				labelCk = new Label(row_colindex, rowIndex,
						ip, wcfFC);
				sheet.addCell(labelCk);
				row_colindex++;

				String type_t = bean.getType_t()== null ? ""
						: bean.getType_t();
				labelCk = new Label(row_colindex, rowIndex,
						type_t, wcfFC);
				sheet.addCell(labelCk);
				row_colindex++;

				String info = bean.getInfo() == null ? "" : bean.getInfo();
				labelCk = new Label(row_colindex, rowIndex, info, wcfFC);
				sheet.addCell(labelCk);
				row_colindex++;
				
				
				String createTime =bean.getCreateTime(); 
				labelCk = new Label(row_colindex, rowIndex, createTime, wcfFC);
				sheet.addCell(labelCk);
				row_colindex++;

				rowIndex++;
			}
		}
		workbook.write();
		workbook.close();
		return true;
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics