jxl 生成excel 并提示下载
jxl 生成了excel 文件是没有问题的,之前写死了 自能生成在D盘,现在的改为 点击下载是要提示用户进行保存。
一下是代码, 各位大哥大姐们,帮帮小弟吧,感激不尽 ,小弟会尽快给分的。
Action类
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(); }
*/
}
[解决办法]
之前用poi写过,思路是将excel文件生成在服务器端,通过response响应返回浏览器,和楼主的一样
楼主试试这里的代码
public void exportPath(String path,String fileName) throws Exception {
HttpServletResponse response = super.getResponse();
File file = new File(path);
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
response.reset();
response.addHeader("Content-Disposition", "attachment;filename="+fileName+".xls");
byte[] by = new byte[2048];
int len = 0;
while((len=(inputStream.read(by)))>0){
response.getOutputStream().write(by,0,len);
}
inputStream.close();
if(file.isFile() && file.exists()){
file.delete();
}
}