Java Poi 操作Excle(三)

2014-11-24 03:14:05 · 作者: · 浏览: 3
的缘故,我用的是poi3.0版本。

例2:Grails

/**

* Excel导出方法,导出班级工作情况统计列表

* @param

* titleList 标题集合

* classTableInstanceList,questionList 数据集合

*

* out OutputStream out = response.getOutputStream();

* @return

*

*/

def exportWorkList(titleList,classList,questionList,out,ttList){

Workbook wb = new HSSFWorkbook();

Sheet sheet = wb.createSheet("sheet1"); //班级工作情况统计

HSSFRow row = sheet.createRow((short)1);

HSSFRow row2 = sheet.createRow((short)2);

HSSFCell cell = row.createCell((short)1);

HSSFCell cell2 = row.createCell((short)2);

// 设置字体

HSSFFont font = wb.createFont();

//font.setFontHeightInPoints((short) 11); //字体高度

//font.setColor(HSSFFont.COLOR_RED); //字体颜色

//font.setFontName("黑体"); //字体

font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度

// 设置单元格类型

HSSFCellStyle cellStyle = wb.createCellStyle();

cellStyle.setFont(font);

cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中

cellStyle.setWrapText(true);

HSSFCellStyle cellStyle1 = wb.createCellStyle();

cellStyle1.setAlignment(HSSFCellStyle.VERTICAL_TOP); //垂直布局:居上

cellStyle1.setWrapText(true); //设置自动换行

//设置单元格宽度

sheet.setColumnWidth(0,1500); //序列

sheet.setColumnWidth(1,2500); //区县名称

sheet.setColumnWidth(2,7000); //基地名称

sheet.setColumnWidth(3,7000); //班级名称

sheet.setColumnWidth(4,2500); //班级状态

sheet.setColumnWidth(29,3000); //拓宽问题5的单元格宽度

sheet.setColumnWidth(35,5000); //评价最高的老师

sheet.setColumnWidth(36,5000); //评价最低的老师

titleList.eachWithIndex {p,i->

if(i>=5 && i<=10){

cell = row.createCell(5+5*(i-5));

sheet.addMergedRegion(new Region(0,(short)0,0,(short)36));//合并区域第一行

sheet.addMergedRegion(new Region(1,(short)(5+5*(i-5)),1,(short)(5+5*(i-5)+4)));//指定合并区域

cell.setCellValue(p);

cell.setCellStyle(cellStyle);//设置单元格样式

} }

}

try {

wb.write(out);

out.close();

} catch (FileNotFoundException e1) {

//e1.printStackTrace();

}catch(IOException e){

//e.printStackTrace();

}

}

摘自LiFei's 博客http://blog.csdn.net/fei1502816