/**
* Excel导出类
* @author pll
*
*/
public class ExcelUntil {
/**
* 导出综合排名Excel
*/
public static InputStream excelRank(List<CompositeRank> list)
{
boolean success = false;
InputStream excelStream=null;
String xlsname;
String filename;
try {
// 创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("综合排名报表");
// 在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 创建单元格,并设置值表头 设置表头
HSSFFont f = wb.createFont();
//设置标题字号
f.setFontHeightInPoints((short)14);
f.setFontName("宋体");
// f.setColor(HSSFFont.COLOR_RED); //设置为红色
f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //加粗
//设置表头样式
HSSFCellStyle style = wb.createCellStyle();
style.setBorderLeft(HSSFCellStyle.BORDER_DOTTED);//左边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
style.setFont(f);
// 普通单元格
HSSFFont font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 14);
// 普通单元格样式
HSSFCellStyle columstyle = wb.createCellStyle();
columstyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
columstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中
columstyle.setFont(font);
HSSFSheet s = wb.getSheetAt(0);
//设置列的宽度
s.setColumnWidth((short)0,(short)5000);
s.setColumnWidth((short)1,(short)4000);
s.setColumnWidth((short)2,(short)4000);
s.setColumnWidth((short)3,(short)4000);
s.setColumnWidth((short)4,(short)4000);
s.setColumnWidth((short)5,(short)6000);
s.setColumnWidth((short)6,(short)5000);
s.setColumnWidth((short)7,(short)4000);
//设置列的名称
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("用户ID");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("用户名称");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("小区名称");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("用户地址");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("巡检综合分");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue("定时投放综合分");
cell.setCellStyle(style);
cell = row.createCell((short) 6);
cell.setCellValue("可回收综合分");
cell.setCellStyle(style);
cell = row.createCell((short) 7);
cell.setCellValue("最终综合分");
cell.setCellStyle(style);
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow((int) i + 1);
CompositeRank compositeRank=list.get(i);
// 创建单元格,并设置值
row.createCell((short) 0).setCellValue(compositeRank.getUserid());
row.setRowStyle(columstyle);
row.createCell((short) 1).setCellValue(compositeRank.getUsername());
row.setRowStyle(columstyle);
row.createCell((short) 2).setCellValue(compositeRank.getCname());
row.setRowStyle(columstyle);
row.createCell((short) 3).setCellValue(compositeRank.getAddress());
row.setRowStyle(columstyle);
row.createCell((short) 4).setCellValue(compositeRank.getInspectpoint().toString());
row.setRowStyle(columstyle);
row.createCell((short) 5).setCellValue(compositeRank.getThrowpoint().toString());
row.setRowStyle(columstyle);
row.createCell((short) 6).setCellValue(compositeRank.getRecyclepoint().toString());
row.setRowStyle(columstyle);
row.createCell((short) 7).setCellValue(compositeRank.getPoint().toString());
row.setRowStyle(columstyle);
}
//下载流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
wb.write(byteArrayOutputStream);
excelStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
return excelStream;
}
}
private ReportService reportService;
private CompositeRank comRank;
private InputStream rankExcelStream; //综合排名输出流
private String xlsname; //综合排名输出文件名称
/* 导出综合排名数据
* @return
*/
public String exportExcelForRank(){
init();
ActionContext context = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse)context.get(ServletActionContext.HTTP_RESPONSE);
Map session = context.getSession();
//返回值定义
String reV = "down";
// admin为空,session失效,或没权限,请重新登入
SysAdmin admin = (SysAdmin) session.get("sysAdmin");
//
CompositeRank comRank = new CompositeRank();
comRank.setYear(Integer.parseInt(request.getParameter("rankyearSelect")));
comRank.setMonth(Integer.parseInt(request.getParameter("rankmonthSelect")));
Calendar c = Calendar.getInstance();
int y = c.get(Calendar.YEAR);// 当前年
int m = c.get(Calendar.MONTH);// 上月
if (comRank.getYear() == 0) {
comRank.setYear(y);
}
if (comRank.getMonth() == 0) {
comRank.setMonth(m);
}
if (admin != null) {
if (admin.getComminid() != null && admin.getComminid() != 0) {// 如果是小区管理员
comRank.setComminid(admin.getComminid() + "");// 直接去管理员comminid赋值到条件中
} else {
if (admin.getCommid() != null && admin.getCommid() != 0) {// 如果是社区管理员
comRank.setCommid(admin.getCommid() + "");
}
}
//System.out.println(comRank.getYear()+"......"+comRank.getMonth()+"......."+comRank.getComminid());
//查询综合排名
List<CompositeRank> list= reportService.getExcelCompositeRankList(comRank);
try {
//导出综合排名数据流
rankExcelStream=ExcelUntil.excelRank(list);
String agent = request.getHeader("USER-AGENT");
xlsname = comRank.getYear()+"年"+comRank.getMonth()+"月综合排名报表.xls";
if(null != agent && -1 != agent.indexOf("MSIE")){
xlsname = URLEncoder.encode(xlsname,"UTF8");
}else if(null != agent && -1 != agent.indexOf("Mozilla")){
xlsname = MimeUtility.encodeText(xlsname,"UTF8","B");
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
reV = "login";
}
return reV;
}
<!--Excel导出综合排名数据-->
<action name="exportExcelForRank" class="userAction" method="exportExcelForRank">
<result name="down" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="contentDisposition">attachment;filename=${xlsname}</param>
<param name="inputName">rankExcelStream</param>
</result>
</action>