日期:2014-05-20 浏览次数:20873 次
public String exportExcel(){
try {
//查询所有的部位
String hql="from Baojiabuwei";
List<Baojiabuwei> buweilist=new ArrayList<Baojiabuwei>();
HttpServletResponse response = ServletActionContext.getResponse();
String str_quoprice_id=getSession.getTrequest().getParameter("quotedpriceid");
StringBuffer sb=new StringBuffer("FROM Enginnernum e,Quotedprice q WHERE e.quotedpriceid=q.id ");
//查询各个项目部分小计的总额度
//String sumbuweihql="SELECT SUM(number*offerprice) AS he ,buweiid FROM Enginnernum WHERE 1=1 ";
String baoijiamessagehql="SELECT s.username,s.projectnumber,s.usertelphone,s.projectaddress ,s.shejishiname,s.sjstelphone ,s.huxing FROM Sprojects s , Quotedprice q WHERE s.id=q.projectname ";
if(null!=str_quoprice_id&&!str_quoprice_id.equals("")){
sb.append(" and q.id="+str_quoprice_id);
//sumbuweihql=sumbuweihql+" and quotedpriceid="+str_quoprice_id+" GROUP BY buweiid";
baoijiamessagehql=baoijiamessagehql+" AND q.id="+str_quoprice_id;
}
sb.append(" order by buweiid");
List listsumbuwei=new ArrayList();
List list = new ArrayList();
List list2=new ArrayList();
list2=sprojectsService.findprojectsList(baoijiamessagehql);
list=quotedpriceService.getQuotedpricesList(sb.toString());
//listsumbuwei=enginnernumService.getEnginnernumslist(sumbuweihql);
List<Enginnernum> nlist=new ArrayList<Enginnernum>();
Object[] obj=null;
for (int i = 0; i < list.size(); i++) {
obj=(Object[])list.get(i);
nlist.add((Enginnernum)obj[0]);
}
buweilist=baojiabuweiService.getBaojiabuweiList(hql+" where quotedpriceid="+nlist.get(0).getQuotedpriceid());
// 创建当前日子
// Date date = new Date();
// 格式化日期
// SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
// 格式化日期(产生文件名)
// String filename = sdf.format(date);
// 创建文件
File f = new File(System.getProperty("user.dir").replace("bin", "webapps")+ "南国装饰公司报价.xls");
f.createNewFile();
JxlExportExcel jxlExportExcel=new JxlExportExcel();
jxlExportExcel.getExcelTitlesandRows(new FileOutputStream(f), Arguments.TITLE, nlist, buweilist,list2,Arguments.SHEETNAME, 0);
Files.exportFile(response, f, true);
//DWZrefresh.jrefresh(Arguments.EMPTY, Arguments.EMPTY, Arguments.EMPTY, "导出数据成功,数据在D盘", Arguments.EMPTY, Arguments.EMPTY, Arguments.R_STATUS);
return SUCCESS;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return ERROR;
}
}
public static void exportFile(HttpServletResponse response, File file,
boolean isDel) throws IOException {
OutputStream os = null;
InputStream in = null;
// 获得文件名
String filename = URLEncoder.encode(file.getName());
// 定义输出类型下载
response.reset();
response.setContentType("application/msexcel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=\""
+ filename + ".xls" + "\"");
try {
os.flush();
os = response.getOutputStream();
in = new FileInputStream(file.getPath());
byte[] buffer = new byte[1024];
int i = -1;
while ((i = in.read(buffer)) != -1) {
response.getOutputStream().write(buffer, 0, i);
// out.write(buffer,0,i);
}
os.close();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("os is null");
} finally {
if (in != null) {
in.close();
in = null;
}
if (os != null) {
os.flush();
os.close();
os = null;
}
}
/*
* if(isDel){ //删除文件,删除之前关闭所有的stream file.delete(); }
*/
}