分享

java 生成excel 表格

 走墨 2014-10-22
String areaCode=request.getParameter("areaCode");
List users=new ArrayList();
users=baseAreaManager.getSchoolUserByAreaCode(areaCode);
String areaName = baseAreaManager.getLabelByCode(areaCode);
String downloadUrl="";
    String title="";
try {
//response.setHeader("Content-Disposition", "attachment; filename=" + new String(("导出学校管理员账号表.xls").getBytes("GBK"),"ISO8859-1"));
//ExcelUtil.WriteExcel(request.getSession().getServletContext().getRealPath("/excel/schoolusers.xls"),
// "学校管理员账号表", 2, users, response.getOutputStream());
//} catch (Exception e) {
// e.printStackTrace();
///}
title=areaName+"-直管学校管理员账号表";
   String url=request.getSession().getServletContext().getRealPath("/");
String templatePath=url+"excel/"+title+".xls";
downloadUrl="/excel/"+title+".xls";
File ff = new File(templatePath);
if(!ff.exists()) {
ff.createNewFile();
}
WorkbookSettings settings = new WorkbookSettings();  
settings.setWriteAccess(null);
WritableWorkbook wwb = Workbook.createWorkbook(ff,settings);
WritableSheet wws = wwb.createSheet(title, 0);
WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 12,
WritableFont.NO_BOLD);
WritableCellFormat cellFormat = new WritableCellFormat(font,NumberFormats.TEXT);
WritableFont font1 = new WritableFont(WritableFont.createFont("宋体"), 14,
WritableFont.NO_BOLD);
WritableCellFormat cellFormat1 = new WritableCellFormat(font1,NumberFormats.TEXT);
cellFormat.setWrap(true);
cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
cellFormat1.setWrap(true);
cellFormat1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
cellFormat1.setAlignment(jxl.format.Alignment.CENTRE);
cellFormat1.setBorder(Border.ALL, BorderLineStyle.THIN);
Label label = new Label(0, 0, title, cellFormat1);
wws.addCell(label);
//设置Excel表头 
String[] titles=new String[]{"姓名","账号","学校","地区名称"};
for (int i = 0; i < titles.length; i++) { 
Label excelTitle = new Label(i, 1, titles[i], cellFormat1); 
wws.addCell(excelTitle); 
}
for (int i = 0; i < users.size(); i++) {
Object[] baseUser=(Object[])users.get(i);
wws.setRowView(i+2, 270);
wws.addCell(new Label(0, i + 2, baseUser[0].toString(), cellFormat));
wws.setColumnView(0, 28);  
wws.addCell(new Label(1, i + 2, baseUser[1].toString(), cellFormat));
wws.setColumnView(1, 18); 
wws.addCell(new Label(2, i + 2, baseUser[4].toString(), cellFormat));
wws.setColumnView(2, 38); 
wws.addCell(new Label(3, i + 2, baseUser[3].toString(), cellFormat));
wws.setColumnView(3, 18); 
}
wws.mergeCells( 0 , 0 , 3 , 0 );
wwb.write();
wwb.close();
} catch (Exception e) {
e.printStackTrace();
}
response.setContentType("text/html;charset=utf-8");
java.io.PrintWriter pw = null;
String returnValue = downloadUrl+"#"+title;
try {
pw = response.getWriter();
pw.write(returnValue);
} catch (java.io.IOException e) {
returnValue = "0";
e.printStackTrace();
}
return null;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多