经过3天的研究,终于实现用两种方法实现excel导出
第一种POI方法实现:
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("teacher");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("序号");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("计划名称");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("教师姓名");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("组织");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("专业");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("创建人");
cell.setCellStyle(style);
cell = row.createCell((short) 6);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("建立时间");
cell.setCellStyle(style);
// 第五步,写入实体数据 实际应用中这些数据从数据库得到
ActionContext act = ActionContext.getContext();
HttpServletRequest req = (HttpServletRequest) act
.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse rsp = (HttpServletResponse) act
.get(ServletActionContext.HTTP_RESPONSE);
HttpSession session = req.getSession();
/*
拼接字符串
List body=(ArrayList)session.getAttribute("tongguo_body_list");
for(int i=0;i<body.size();i++){
row = sheet.createRow((int)i+1);
String[] str = ((String)body.get(i)).split(",");
for(int j=0;j<str.length;j++)
{
cell = row.createCell((short)j);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(str[j]);
}
try {
FileOutputStream fout = new FileOutputStream("E:/123.xls");
wb.write(fout);
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
}*/
session.getAttribute("tongguo_body_list_1");
List dlist = null;
if (session.getAttribute("tongguo_body_list_1") != null
&& !"".equals(session.getAttribute("tongguo_body_list_1"))) {
dlist = (ArrayList) session.getAttribute("tongguo_body_list_1");
}
Map map = new HashMap();
if (dlist != null && dlist.size() > 0) {
for (int i = 0; i < dlist.size(); i++) {
row = sheet.createRow((int)i+1);
map = (HashMap) dlist.get(i);
cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((int) i);
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((String) map.get("PLAN_NAME"));
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((String) map.get("TEACHER_NAME"));
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((String) map.get("ORGNAME"));
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((String) map.get("SPECIALITY"));
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((String) map.get("CREATE_BY"));
cell.setCellStyle(style);
cell = row.createCell((short) 6);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((String) map.get("CREATE_DATE"));
cell.setCellStyle(style);
}
}
try {
/*rsp.setContentType("application/vnd.ms-excel");
String filename="2012";
rsp.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("gb2312"), "ISO8859-1") + ".xls"); */
FileOutputStream fout = new FileOutputStream("E:/haha.xls");
wb.write(fout);
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
第二种jxl实现:
ActionContext act = ActionContext.getContext();
HttpServletRequest req = (HttpServletRequest) act
.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse rsp = (HttpServletResponse) act
.get(ServletActionContext.HTTP_RESPONSE);
HttpSession session = req.getSession();
WritableWorkbook wwb;
try {
OutputStream os=new FileOutputStream("E:/haha11.xls");
wwb=Workbook.createWorkbook(os);
WritableSheet ws=wwb.createSheet("teacher", 0); //创建一个工作表
WritableFont font1 =new WritableFont(WritableFont.createFont("微软雅黑"), 10 ,WritableFont.BOLD);
// 设置单元格的文字格式
WritableCellFormat wcf = new WritableCellFormat(font1);
wcf.setAlignment(Alignment.CENTRE); //平行居中
wcf.setVerticalAlignment(VerticalAlignment.CENTRE); //垂直居中
//填充数据
ws.addCell(new Label(0, 0, "序号", wcf));
ws.addCell(new Label(1, 0, "计划名称", wcf));
ws.addCell(new Label(2, 0, "教师姓名", wcf));
ws.addCell(new Label(3, 0, "组织", wcf));
ws.addCell(new Label(4, 0, "专业", wcf));
ws.addCell(new Label(5, 0, "创建人", wcf));
ws.addCell(new Label(6, 0, "创建时间", wcf));
List body=(ArrayList)session.getAttribute("tongguo_body_list");
for(int i=0;i<body.size();i++){
String[] str = ((String)body.get(i)).split(",");
for(int j=0;j<str.length;j++)
{
//new Label(列,行,数据,格式)
ws.addCell(new Label(j, i+1, str[j], wcf));
}
}
wwb.write();
wwb.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}