您的位置:首页 > 其它

poi导出excel

2015-06-15 16:34 267 查看
/**
*
* 按单位统计exl导出
* @author bdy
* @date 2015-6-15
* @version 1.0
* @description
*/
@SuppressWarnings({ "deprecation", "rawtypes" })
@RequestMapping(value = "/statisticsBydeportExportExcel", method = RequestMethod.GET)
public void statisticsBydeportExportExcel(HttpServletRequest request,
HttpServletResponse response) throws Exception {

StringBuffer sb = new StringBuffer();
//sb.append("select cc.ORG_CODE,cc.ORG_NAME,cc.FOLDERCOUNT,cc.ITEMCOUNT,cc.DRAFT,cc.REJECT,dd.NODE1,dd.NODE2,dd.NODE3,dd.NODE4,dd.NODE5,dd.NODE6,dd.NODE7,dd.NODE8 ");

List<Object> param = new ArrayList<Object>();

sb.append("select cc.ORG_CODE,cc.ORG_NAME,cc.FOLDERCOUNT,cc.ITEMCOUNT,cc.DRAFT,cc.REJECT,cc.DRAFT+cc.REJECT+cc.PUBLISH as NODE1,dd.NODE2,dd.NODE3,dd.NODE4,dd.NODE5,dd.NODE6,dd.NODE7,dd.NODE8,cc.PUBLISH,cc.FLOW "+
" from (( "
+" select i.ORG_CODE,i.ORG_NAME "
+",COUNT(distinct fol.CODE) as FOLDERCOUNT "
+",COUNT(distinct i.CODE) as ITEMCOUNT "

+",SUM(case when i.PROPERTY='0' and i.STATE='1' and i.STATUS='0' then 1 else 0 end) as DRAFT "
+",SUM(case when i.PROPERTY='0' and i.STATUS='4' then 1 else 0 end) as REJECT "
+",SUM(case when i.PROPERTY='0' and i.STATE='3' and i.STATUS='1' then 1 else 0 end) as FLOW "
+",SUM(case when i.PROPERTY='0' and i.STATE='5' and i.STATUS='5' then 1 else 0 end) as PUBLISH "
+" from project_item i inner join PROJECT_FOLDER FOL on i.FOLDER_CODE=FOL.CODE"
+" where i.TYPE='XK' and i.STATE!='4' AND i.STATE!='0' AND i.STATUS!='8' and FOL.STATUS=5 "
+" group by i.ORG_CODE,i.ORG_NAME "
+") cc left join ( "
+" select i.ORG_CODE,i.ORG_NAME"
+",SUM(case when l.STATUS='0' and l.NODE_CODE='d5e3ec9b79ee1cbdaa2d1b2853a55752' then 1 else 0 end) as NODE1"
+",SUM(case when l.STATUS='0' and l.NODE_CODE='0e4bc31716f992d08d8bdf7e812ae97b' then 1 else 0 end) as NODE2"
+",SUM(case when l.STATUS='0' and l.NODE_CODE='e7b185ef6b5ccad51054cca9e91517a2' then 1 else 0 end) as NODE3"
+",SUM(case when l.STATUS='0' and l.NODE_CODE='d5a320d2d4a392b414e9123131694a84' then 1 else 0 end) as NODE4"
+",SUM(case when l.STATUS='0' and l.NODE_CODE='7c7fd3e231935e0fc783a50c9b86691e' then 1 else 0 end) as NODE5"
+",SUM(case when l.STATUS='0' and l.NODE_CODE='d17fbabf44d40dcfd4b24863cf752f2b' then 1 else 0 end) as NODE6"
+",SUM(case when l.STATUS='0' and l.NODE_CODE='6ba8b342ee633654c2ee9bd805099d5b' then 1 else 0 end) as NODE7"
+",SUM(case when l.STATUS='0' and l.NODE_CODE='554b2f522325170d2d1b369da8788812' then 1 else 0 end) as NODE8"
+" from PROJECT_FLOW_NODE n "
+" inner join PROJECT_FLOW f on f.FLOW_CODE=n.FLOW_CODE "
+" inner join PROJECT_LOG l on n.NODE_CODE=l.NODE_CODE "
+" inner join PROJECT_ITEM i on i.ID=l.ITEM_ID "
+" where f.FLOW_TYPE='XK' and l.STATUS='0' and i.PROPERTY='0' and i.STATE='3' and i.STATUS='1'"
+"  group by i.ORG_CODE,i.ORG_NAME"
+" ) dd on  cc.ORG_CODE=dd.ORG_code"
+" )"
+" ORDER BY cc.org_code asc");

/*StringBuffer where = new StringBuffer();
where.append(" from (");
where.append("( " + s1 + ") cc");//语句1,统计主项、子项、草稿状态、被驳回状态数量
where.append(" left join ");
where.append("(" + s2 + ") dd on  cc.ORG_CODE=dd.ORG_code)");*/
JSONObject json = new JSONObject();

List<Map<String,Object>> ls = this.itemService.find( sb.toString(), param.toArray());

// 创建Excel的工作书册 Workbook,对应到一个excel文档
HSSFWorkbook wb = new HSSFWorkbook();
// 创建Excel的工作sheet,对应到一个excel文档的tab
HSSFSheet sheet = wb.createSheet("sheet1");
// 设置excel每列宽度
sheet.setColumnWidth(0, 20 * 400);
sheet.setColumnWidth(1, 20 * 200);
sheet.setColumnWidth(2, 20 * 200);
sheet.setColumnWidth(3, 20 * 200);
sheet.setColumnWidth(4, 20 * 200);
sheet.setColumnWidth(5, 20 * 200);
sheet.setColumnWidth(6, 20 * 200);
sheet.setColumnWidth(7, 20 * 200);
sheet.setColumnWidth(8, 20 * 200);
sheet.setColumnWidth(9, 20 * 200);
sheet.setColumnWidth(10, 20 * 200);
sheet.setColumnWidth(11, 20 * 200);
sheet.setColumnWidth(12, 20 * 200);
// 创建字体样式
HSSFFont font = wb.createFont();
font.setFontName("Verdana");
font.setColor(HSSFColor.BLACK.index);
// 创建字体样式
HSSFFont fontTitle = wb.createFont();
fontTitle.setFontName("Verdana");
fontTitle.setBoldweight((short) 1);
fontTitle.setBoldweight((short) 200);
fontTitle.setFontHeight((short) 300);
fontTitle.setColor(HSSFColor.BLACK.index);

// 创建单元格样式
HSSFCellStyle style = wb.createCellStyle();

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setFillForegroundColor(HSSFColor.WHITE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

// 设置边框
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);

style.setFont(font);// 设置字体

// 创建单元格样式
HSSFCellStyle styleTitle = wb.createCellStyle();

styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styleTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
styleTitle.setFillForegroundColor(HSSFColor.WHITE.index);
styleTitle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

// 设置边框
styleTitle.setBottomBorderColor(HSSFColor.BLACK.index);
styleTitle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
styleTitle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
styleTitle.setBorderRight(HSSFCellStyle.BORDER_THIN);
styleTitle.setBorderTop(HSSFCellStyle.BORDER_THIN);

styleTitle.setFont(fontTitle);// 设置字体

// 创建Excel的sheet的一行
HSSFRow row0 = sheet.createRow(0);
row0.setHeight((short) 500);// 设定行的高度
HSSFCell titleCell = row0.createCell(0);
HSSFCell titleCell1 = row0.createCell(1);
HSSFCell titleCell2 = row0.createCell(2);
HSSFCell titleCell3 = row0.createCell(3);

titleCell.setCellValue("按单位统计");

titleCell.setCellStyle(styleTitle);
titleCell1.setCellStyle(styleTitle);
titleCell2.setCellStyle(styleTitle);
titleCell3.setCellStyle(styleTitle);
CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0,
0, 3);
sheet.addMergedRegion(cellRangeAddress);

// 创建Excel的sheet的一行
HSSFRow row = sheet.createRow(1);
// row.setHeight((short) 500);// 设定行的高度
HSSFCell cellZero = row.createCell(0);
cellZero.setCellStyle(style);
cellZero.setCellValue("单位编码");
HSSFCell cellOne = row.createCell(1);
cellOne.setCellStyle(style);
cellOne.setCellValue("单位名称");
HSSFCell cellTwo = row.createCell(2);
cellTwo.setCellStyle(style);
cellTwo.setCellValue("主项");
HSSFCell cellThree = row.createCell(3);
cellThree.setCellStyle(style);
cellThree.setCellValue("子项");
HSSFCell cellfour = row.createCell(4);
cellfour.setCellStyle(style);
cellfour.setCellValue("草稿");
HSSFCell cellfive = row.createCell(5);
cellfive.setCellStyle(style);
cellfive.setCellValue("已驳回");
HSSFCell cellsix = row.createCell(6);
cellsix.setCellStyle(style);
cellsix.setCellValue("部门录入");
HSSFCell cellseven = row.createCell(7);
cellseven.setCellStyle(style);
cellseven.setCellValue("部门审核");
HSSFCell celleight = row.createCell(8);
celleight.setCellStyle(style);
celleight.setCellValue("省筹建办初审");
HSSFCell cellnine = row.createCell(9);
cellnine.setCellStyle(style);
cellnine.setCellValue("省编办初审");
HSSFCell cellten = row.createCell(10);
cellten.setCellStyle(style);
cellten.setCellValue("省编办复核");
HSSFCell celleleven = row.createCell(11);
celleleven.setCellStyle(style);
celleleven.setCellValue("省法制办审查");
HSSFCell celltwelve = row.createCell(12);
celltwelve.setCellStyle(style);
celltwelve.setCellValue("省筹建办审核");

HSSFCell cellt13 = row.createCell(13);
cellt13.setCellStyle(style);
cellt13.setCellValue("发布");

for (int i = 0; i < ls.size(); i++) {
//Record rc = (Record) ls.get(i);
Map<String,Object> rc = ls.get(i);
HSSFRow rowValue = sheet.createRow(i + 2);

HSSFCell cellZeroVal = rowValue.createCell(0);
cellZeroVal.setCellStyle(style);
cellZeroVal.setCellValue((String)rc.get("ORG_CODE"));

HSSFCell cellOneVal = rowValue.createCell(1);
cellOneVal.setCellStyle(style);
cellOneVal.setCellValue((String)rc.get("ORG_NAME"));

HSSFCell cellTwoVal = rowValue.createCell(2);
cellTwoVal.setCellStyle(style);
cellTwoVal.setCellValue(Float.valueOf(rc.get("FOLDERCOUNT").toString()).intValue());

HSSFCell cellThreeVal = rowValue.createCell(3);
cellThreeVal.setCellStyle(style);
cellThreeVal.setCellValue((Float.valueOf(rc.get("ITEMCOUNT").toString())).intValue());

HSSFCell cellfourVal = rowValue.createCell(4);
cellfourVal.setCellStyle(style);
cellfourVal.setCellValue(Float.valueOf(rc.get("DRAFT").toString()).intValue());

HSSFCell cellfiveVal = rowValue.createCell(5);
cellfiveVal.setCellStyle(style);
cellfiveVal.setCellValue(Float.valueOf(rc.get("REJECT").toString()).intValue());

HSSFCell cellsixVal = rowValue.createCell(6);
cellsixVal.setCellStyle(style);

if(null!= rc.get("NODE1") ){
cellsixVal.setCellValue(Float.valueOf(rc.get("NODE1").toString()).intValue());

}
cellsixVal.setCellValue("0");

HSSFCell cellsevenVal = rowValue.createCell(7);
cellsevenVal.setCellStyle(style);
if(null!= rc.get("NODE2") ){
cellsevenVal.setCellValue(Float.valueOf(rc.get("NODE2").toString()).intValue());
}
cellsevenVal.setCellValue("0");

HSSFCell celleightVal = rowValue.createCell(8);
celleightVal.setCellStyle(style);
if(null!= rc.get("NODE3") ){
celleightVal.setCellValue(Float.valueOf(rc.get("NODE3").toString()).intValue());
}
celleightVal.setCellValue("0");

HSSFCell cellnineVal = rowValue.createCell(9);
cellnineVal.setCellStyle(style);
if(null!= rc.get("NODE4") ){
cellnineVal.setCellValue(Float.valueOf(rc.get("NODE4").toString()).intValue());
}
cellnineVal.setCellValue("0");

HSSFCell celltenVal = rowValue.createCell(10);
celltenVal.setCellStyle(style);
if(null!= rc.get("NODE5") ){
celltenVal.setCellValue(Float.valueOf(rc.get("NODE5").toString()).intValue());
}
celltenVal.setCellValue("0");

HSSFCell cellelevenVal = rowValue.createCell(11);
cellelevenVal.setCellStyle(style);
if(null!= rc.get("NODE6") ){
cellelevenVal.setCellValue(Float.valueOf(rc.get("NODE6").toString()).intValue());
}
cellelevenVal.setCellValue("0");

HSSFCell celltwelveVal = rowValue.createCell(12);
celltwelveVal.setCellStyle(style);
if(null!= rc.get("NODE7") ){
celltwelveVal.setCellValue(Float.valueOf(rc.get("NODE7").toString()).intValue());
}
celltwelveVal.setCellValue("0");

HSSFCell cell13 = rowValue.createCell(13);
cell13.setCellStyle(style);
if(null!= rc.get("NODE8") ){
cell13.setCellValue(Float.valueOf(rc.get("NODE8").toString()).intValue());
}
cell13.setCellValue("0");
}

// 设定标题
String fileName = null;

fileName = "按部门统计.xls";

response.setContentType("application/octet-stream");
response.setHeader(
"Content-Disposition",
"attachment;"
+ (new StringBuilder("filename=").append(encode(
request, fileName)).toString()));
response.addHeader("Content-Length", "");

ServletOutputStream localServletOutputStream = response
.getOutputStream();
wb.write(localServletOutputStream);

localServletOutputStream.flush();
localServletOutputStream.close();
}
private String encode(HttpServletRequest request, String fileName)
throws UnsupportedEncodingException {
String agent = request.getHeader("USER-AGENT");
if ((agent != null) && (-1 != agent.indexOf("MSIE")))
return URLEncoder.encode(fileName, "UTF-8");
if ((agent != null) && (-1 != agent.indexOf("Mozilla"))) {
return "=?UTF-8?B?"
+ new String(org.apache.shiro.codec.Base64.encode(fileName.getBytes("UTF-8")))
+ "?=";
}
return fileName;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: