您的位置:首页 > 其它

关于POI导出excel过程总结

2015-12-29 16:01 253 查看
今天在维护项目过程中拿到一份关于导出员工薪酬数据的需求,要求使用Excel。查阅了之前导出的功能,都是使用服务器存储模板,然后按照指定路径读取并将查询到的数据在写入到模板中,过程稍稍微有些繁琐(同时后期维护的话也要修改模板)就直接使用POI创建Excel了。大致在开发之前整理了一下必要的信息:1:创建要使用的Excel对象2:创建导出文档分三个部分:标题,列标题,数据主体;标题:使用标题的样式以及命名规则列标题:使用列标题的样式以及命名规则数据主体:使用数据主体的样式以及命名规则具体的逻辑如下:
HSSFWorkbook workbook = new HSSFWorkbook();//创建Excel主体
Sheet sheet = workbook.createSheet(sheet_name);//创建工作空间
for(int i=0;i<title_names.length;i++){
sheet.setColumnWidth(i, title_names[i].getBytes().length * 256 + offset * 256);//设置每列的宽度
}
//开始创建表头标题样式
CellStyle cellStyle_title_row = workbook.createCellStyle();
Font font_title_row = workbook.createFont();
font_title_row.setFontHeightInPoints(height_title_row_font_size); //字体大小
font_title_row.setFontName("宋体");
font_title_row.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体
font_title_row.setColor(HSSFColor.BLACK.index);    //黑字
cellStyle_title_row.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
cellStyle_title_row.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
cellStyle_title_row.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
cellStyle_title_row.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyle_title_row.setFont(font_title_row);
cellStyle_title_row.setBorderBottom(height_title_row_border_size); //设置边框线宽度
cellStyle_title_row.setBorderRight(height_title_row_border_size);
cellStyle_title_row.setBottomBorderColor(IndexedColors.BLACK.getIndex());//设置边框线颜色
//开始创建并表体标题样式
CellStyle cellStyle_title_body = workbook.createCellStyle();
Font font_title_body = workbook.createFont();
font_title_body.setFontHeightInPoints(height_title_body_font_size); //字体大小
font_title_body.setFontName("宋体");
font_title_body.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体
font_title_body.setColor(HSSFColor.BLACK.index);    //黑字
cellStyle_title_body.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle_title_body.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
cellStyle_title_body.setFont(font_title_body);
cellStyle_title_body.setBorderBottom(height_title_body_border_size); //设置边框线宽度
cellStyle_title_body.setBorderRight(height_title_body_border_size);
cellStyle_title_body.setBottomBorderColor(IndexedColors.BLACK.getIndex());//设置边框线颜色
//开始创建表体标题并设置其样式
Row row_0 = sheet.createRow(0);//表体标题
row_0.setHeightInPoints(height_title_body);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, title_names.length-1)); //合并单元格
for(int i=0;i<title_names.length;i++){
Cell cell = row_0.createCell(i);
if(i==0){
cell.setCellValue(body_title_name);
}
cell.setCellStyle(cellStyle_title_body);
}
//开始创建表头标题并设置其样式
Row row_1 = sheet.createRow(1);//表头标题
row_1.setHeightInPoints(height_title_row);
for(int i=0;i<title_names.length;i++){
Cell cell = row_1.createCell(i);
cell.setCellValue(title_names[i]);
cell.setCellStyle(cellStyle_title_row);
}
//开始查询数据
HttpSession session = request.getSession();
StringBuffer sbsql = this.getPsnCostSql(vo, session);
List<PsnCostBVo> list = comDao.queryBySQL(sbsql.toString(), PsnCostBVo.class);

HSSFSheet sheet = workbook.getSheetAt(0);
HSSFDataFormat format= workbook.createDataFormat();
CellStyle cellstyle_money = workbook.createCellStyle();
cellstyle_money.setDataFormat(format.getFormat("#,##0.00"));//金额格式
CellStyle cellstyle_text = workbook.createCellStyle();
cellstyle_text.setDataFormat(format.getFormat("@"));//文本格式
sheet.createRow(2);
Map<String, Object> map = null;
for(int i=0;i<list.size();i++){
map= Constant.ConvertObjToMap(list.get(i));
HSSFRow row = sheet.createRow(i+2);
for(int j=0;j<title_names.length;j++){
HSSFCell cell = row.createCell(j);
if (j==6){
cell.setCellStyle(cellstyle_text);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
if(map.get(title_fileds[j]) != null && !("null").equals(map.get(title_fileds[j]))){
cell.setCellValue(((String)map.get(title_fileds[j])).equals("0") ? "已发放":"未发放");
}
} else if(j<7){
cell.setCellStyle(cellstyle_text);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
if(map.get(title_fileds[j]) != null && !("null").equals(map.get(title_fileds[j]))){
cell.setCellValue((String)map.get(title_fileds[j]));
}
} else {
<span style="white-space:pre">		</span>cell.setCellStyle(cellstyle_money);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
if(map.get(title_fileds[j]) != null && !("null").equals(map.get(title_fileds[j]))){
cell.setCellValue((Double)map.get(title_fileds[j]));
}
}
}
}
数据格式总结:
cellstyle.setDataFormat(format.getFormat("¥#,##0.00")); //金额格式
<pre name="code" class="java">cellstyle.setDataFormat(format.getFormat("@")); //文本格式
cellStyle.setDataFormat(format.getFormat("yyyy年m月d日"));
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00")); //两位小数格式
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%")); //百分比格式
cellStyle.setDataFormat( HSSFDataFormat.getBuiltinFormat("0.00E+00")); //科学计数法格式
//这里与上面有所不同,用的是HSSFDataFormat.getBuiltinFormat()方法,因为0.00是Excel内嵌的格式,Excel内嵌格式列表大家可以在Excel文件中查看。
cellStyle.setDataFormat(format.getFormat("[DbNum2][$-804]0")); //中文大写格式

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: