您的位置:首页 > 编程语言 > Java开发

java生成Excel文件 xls

2010-07-30 09:58 330 查看
第一篇技术文章.博客不大会用.请多担待.由于工作需要,需要将结果生成xls文件并在客户端下载,决定使用poi来生成.下载问题由于使用jsf框架,本想用jsf方式.但是jsf接触几个月.没系统学过.于是就使用老方法servlet(servlet下载比jsp下载支持的好,比如支持weblogic问题).

先生成xls吧 poi官网 http://poi.apache.org/ 上面有poi包下载.文档

我下的是 poi-3.6-20091214.jar ,其中有些老版本的代码在新版本中是不被支持的 如HSSFCell 类的 setEncoding 方法.先说一下我要生成的xls的过程吧. 第一行是xls的标题,是多列合并的,居中,大字体.第二行是列名,从第三行往下是数据.最后一行则显示统计结果.废话不多了.贴主要代码.

// 创建Excel的工作书册 Workbook,对应到一个excel文档
HSSFWorkbook wb = new HSSFWorkbook();
// 创建Excel的工作sheet,对应到一个excel文档的tab
HSSFSheet sheet = wb.createSheet("片区统计");

// 设置excel每列宽度
sheet.setColumnWidth(0, 4000);
sheet.setColumnWidth(1, 3500);
sheet.setColumnWidth(2, 3500);
sheet.setColumnWidth(3, 3500);
sheet.setColumnWidth(4, 3500);
sheet.setColumnWidth(5, 3500);
sheet.setColumnWidth(6, 3500);
sheet.setColumnWidth(7, 3500);
sheet.setColumnWidth(8, 5000);
/*****************************************************************/
//标题行
HSSFRow row0 = sheet.createRow(0);
row0.setHeight((short) 1000);// 设定行的高度
// 创建一个Excel的单元格
HSSFCell cell0 = row0.createCell(0);
// 合并单元格(startRow,endRow,startColumn,endColumn)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 8));
// 给Excel的单元格设置样式和赋值
// 创建字体样式
HSSFFont font = wb.createFont();
font.setFontName("宋体");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//font.setFontHeight((short) 300);
font.setFontHeightInPoints((short)36);
// 创建单元格样式
HSSFCellStyle style0 = wb.createCellStyle();
style0.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style0.setFont(font);
cell0.setCellStyle(style0);
cell0.setCellValue("客户经理手机GIS走访情况汇总");
/*****************************************************************/
//列名行
// 创建字体样式
HSSFFont font1 = wb.createFont();
font1.setFontName("宋体");
font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font1.setFontHeightInPoints((short)12);
// 创建单元格样式
HSSFCellStyle style1 = wb.createCellStyle();
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style1.setFont(font1);
HSSFRow row1 = sheet.createRow(1);
row1.setHeight((short) 300);// 设定行的高度
HSSFCell cell = row1.createCell(0);
cell.setCellStyle(style1);
cell.setCellValue("字段1");
cell = row1.createCell(1);
cell.setCellStyle(style1);
cell.setCellValue("字段2");
cell = row1.createCell(2);
cell.setCellStyle(style1);
cell.setCellValue("字段3");
cell = row1.createCell(3);
cell.setCellStyle(style1);
cell.setCellValue("字段4");
cell = row1.createCell(4);
cell.setCellStyle(style1);
cell.setCellValue("字段5");
cell = row1.createCell(5);
cell.setCellStyle(style1);
cell.setCellValue("字段6");
cell = row1.createCell(6);
cell.setCellStyle(style1);
cell.setCellValue("字段7");
cell = row1.createCell(7);
cell.setCellStyle(style1);
cell.setCellValue("字段8");
cell = row1.createCell(8);
cell.setCellStyle(style1);
cell.setCellValue("字段9");
/*****************************************************************/
// 创建内容字体样式
HSSFFont fonts = wb.createFont();
fonts.setFontName("宋体");
fonts.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
fonts.setBoldweight((short) 100);
fonts.setFontHeight((short) 200);
fonts.setFontHeightInPoints((short)12);
// 创建内容单元格样式
HSSFCellStyle styles = wb.createCellStyle();
styles.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styles.setFont(fonts);
for(int i=0;i<trackAreaStatPageList.size();i++){
// 创建Excel的sheet的一行
HSSFRow row = sheet.createRow(i+2);
HashMap tMap = (HashMap) trackAreaStatPageList.get(i);

cell = row.createCell(0);
cell.setCellStyle(styles);
cell.setCellValue(tMap.get("departname").toString());

cell = row.createCell(1);
cell.setCellStyle(styles);
cell.setCellValue(tMap.get("managername").toString());

cell = row.createCell(2);
cell.setCellStyle(styles);
cell.setCellValue(tMap.get("visitCount").toString());

cell = row.createCell(3);
cell.setCellStyle(styles);
cell.setCellValue(tMap.get("noVisitCount").toString());

cell = row.createCell(4);
cell.setCellStyle(styles);
cell.setCellValue(tMap.get("normalCount").toString());

cell = row.createCell(5);
cell.setCellStyle(styles);
cell.setCellValue(tMap.get("exceptionCount").toString());

cell = row.createCell(6);
cell.setCellStyle(styles);
cell.setCellValue(tMap.get("stayTimes").toString());

cell = row.createCell(7);
cell.setCellStyle(styles);
cell.setCellValue(tMap.get("complete").toString());

cell = row.createCell(8);
cell.setCellStyle(styles);
cell.setCellValue(tMap.get("bz").toString());
}
/*****************************************************************/
//统计行
// 创建字体样式
HSSFFont font2 = wb.createFont();
font2.setFontName("宋体");
font2.setBoldweight((short) 100);
font2.setFontHeight((short) 200);
// 创建单元格样式
HSSFCellStyle style2 = wb.createCellStyle();
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setFont(font2);
HSSFRow row2 = sheet.createRow(trackAreaStatPageList.size()+2);
cell = row2.createCell(1);
cell.setCellStyle(style2);
cell.setCellValue("合计");
cell = row2.createCell(2);
cell.setCellValue(visits);
cell = row2.createCell(3);
cell.setCellValue(noVisits);
cell = row2.createCell(4);
cell.setCellValue(normals);
cell = row2.createCell(5);
cell.setCellValue(exceptions);
/***************************使用请求时间生成xls文件名**************************************/
File f=new File("D://temp//"+selectDate+".xls");
if(f.exists()){   //如果存在    则删除
f.delete();
}
FileOutputStream os = new FileOutputStream("D://temp//"+selectDate+".xls");
wb.write(os);
os.close();
//设置需要下载的文件名
this.setFileUrl(selectDate+".xls");


关于下载解决我的博客http://blog.csdn.net/doudoudewang/archive/2010/07/30/5775723.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: