81,092
社区成员
发帖
与我相关
我的任务
分享
private void exportSummarydataDetailAction(String companyid,int basenumber,HttpServletRequest request,HttpServletResponse response)throws ApplicationException{
//组织日期信息
boolean sign = request.getParameter("flag1") != null ? true : false ;
String monthstr = sign ? request.getParameter("monthid1") : request.getParameter("monthid2");
String flag = sign ? request.getParameter("flag1") : request.getParameter("flag2");
// String monthstr1 = request.getParameter("monthid1");
// String monthstr2 = request.getParameter("monthid2");
// String flag1 = request.getParameter("flag1");
// String flag2 = request.getParameter("flag2");
// String monthstr = monthstr1!=null?monthstr1:monthstr2;
// String flag = flag1!=null?flag1:flag2;
NumberFormat formatter = NumberFormat.getNumberInstance();
formatter.setMinimumIntegerDigits(2);
formatter.setGroupingUsed(false);
Calendar calendar = Calendar.getInstance(Locale.CHINESE);
int intMonth =calendar.get(Calendar.MONTH)+1;//当前月
int intYear =calendar.get(Calendar.YEAR);//当前年
//公司名称
String companyName = this.queryMenuService.getMontherCompanyName(companyid);
//下载文件名
String fileName = "";
if (!"1".equals(flag)) {
fileName = companyName+"_"+monthstr+"_office_billing.xls";
}else {
fileName = companyName+"_"+monthstr+"_billing.xls";
}
File file = new File(fileName);
//创建excel
WritableWorkbook book;
List<String> monthList = new ArrayList<String>();
try {
book = Workbook.createWorkbook(file);
if ("1".equals(flag)) {
//第一sheet页:创建2014年汇总sheet页面
monthList = this.createExcelSeet1(book, intYear, intMonth, companyid, basenumber);
//第二sheet页:创建本年度各月按指令汇总sheet页面
this.createExcelSeet2(book, intYear, intMonth, companyid, basenumber,monthList);
//第三sheet页:创建当月按日汇总sheet页面
this.createExcelSeet3(book, intYear, intMonth, companyid, basenumber,monthstr);
}
//第四sheet页:创建office明细sheet页面
this.createExcelSeet4(book,companyid, basenumber,monthstr);
book.write();
book.close();
// 设置输出的格式
response.reset();
// added by zzy
String postFix = fileName.substring(fileName.lastIndexOf(".") + 1);
if (postFix.equalsIgnoreCase("xls")){
response.setContentType("application/xls");
}
//根据浏览器设置下载文件名的编码
if(request.getHeader("User-Agent").toLowerCase().indexOf("firefox")>0){
fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
}else if(request.getHeader("User-Agent").toUpperCase().indexOf("MSIE")>0){
fileName = URLEncoder.encode(fileName, "UTF-8");
}else{
fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
}
//设置下载文件头
response.addHeader("Content-Disposition", "attachment; filename=\""
+ fileName + "\"");
//response.setContentLength((int)file.length());
response.setHeader("Connection", "close");
byte[] buffer = new byte[4096];
BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream outWrite = new BufferedOutputStream(response.getOutputStream());
int n = -1;
//遍历,开始下载
while ((n = input.read(buffer, 0, 4096)) > -1) {
outWrite.write(buffer, 0, n);
}
outWrite.flush(); //不可少
response.flushBuffer();//不可少
if (input != null){
input.close();
}
if (outWrite != null){
outWrite.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}