用jxl将查询结果导出到服务器上,如何用java或者js,Ext js将文件下载到本地? 功能是:点击导出的时候,把查询出来的内容导出到Excel表中,并把文件上传到服务器上,弹出一个保存的对话框.。哪位高手请指点一下,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 网上虽然很多用法,用例啊,但是我表示我还没有搞明白,而且赋值他们的代码之后我的还没有成功,特来询问。我的程序大致的意思是将excel与sql server2000的数据导入和导出.导入导出功能我用jxl.jar都已经实现了.但是我用tomcat发布之后,发现了一个问题.就是别人登录后,无法使用导入导出的功能,在服务器就可以.(这是肯定的了,导入导出的功能都是在服务器实现的,我需要将导入的excel表上传到服务器,这就引出了文件的上传和下载,由此知道了smartupload这个jar包).弄了一中午,都睡着了还没弄出明白.直接上代码<input name="fileupload" type="file" /> <input type="checkbox" name="isDeleteTableData" value="checked"> <p> <input name ="upload" type="submit" value="确定" /><input name="cancel" type="reset" value="取消"/><% String fPathUp=request.getParameter("fileupload"); SmartUpload su=null; if(fPathUp!=null) { String thisfPathUp=""; try { su = new SmartUpload(); // 上传初始化 su.initialize(pageContext); //其中这一句我不明白什么意思,我怎么觉得搞来搞去,我用表单提交的文件哪去了 // 设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。 su.setAllowedFilesList("xls,XLS"); // 上传文件 su.upload(); // 将上传文件全部保存到指定目录 int count = su.save("/myimport.xls"); out.println(count+"个文件上传成功!<br>"); thisfPathUp="/myimport.xls"; } catch(Exception ex) { out.println("上传失败!"); } 文件下载:protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //processRequest(request, response); String fileName = "", fileName1 = "",flag="",filePath="",logPath=""; FileInputStream fis = null; BufferedOutputStream out = null; try { if (request.getParameter("filename") != null) { fileName = request.getParameter("filename"); } fileName1 = StringUtil.toUtf8String(fileName.substring(fileName.lastIndexOf("/")+1)); response.setContentType("application/octet-stream"); response.setContentType("application/OCTET-STREAM;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName1); filePath = yourFilePath; File file = new File(filePath + fileName); try { fis = new FileInputStream(file); out = new BufferedOutputStream(response.getOutputStream()); byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) != -1) { out.write(buffer, 0, len); out.flush(); } } catch (FileNotFoundException e) { throw new AppException("您下载的文件文件不存在。"); //e.printStackTrace(); } } catch (Exception e) { String msg=""; response.reset(); response.setContentType("text/html;charset=UTF-8"); if(e.getLocalizedMessage()!=null) msg=e.getMessage(); else msg="系统错误,捕捉的错误类型为"+e.getClass(); response.getWriter().println("<script>parent.downback('"+ fileName + "','" + msg + "');</script>"); } finally { try { if (fis != null) { fis.close(); } if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } }} 你可以用jxl将这个excel文件产生在流里面,然后内容赋好之后,流可以下载的。 文件生成时,直接读取成流文件,out.ptint(io) 我是这样做的 页面里有个iframe这个是下载方法function(){dowmurl = "t_BUSI_ACOUNT.do?method=statisticsWord&"+searchPanel.getForm().getValues(true); downf.location.href = dowmurl;}//进入这个方法@RequestMapping(params = RMP_EXPORTBYCONDITION) public String exportByCondition(HttpServletRequest request,HttpServletResponse response) { String filename="我的存取款记录.xls"; String configName="bankcash.xml"; Map<String, String> params = RequestUtils.buildParamsByNamesFromRequest(request); String rqs = request.getParameter("rqs"); String rqe = request.getParameter("rqe"); String type = request.getParameter("type"); User user = (User)RequestUtils.getCurrentUser(request); OutputStream outStream=null; try { String tempfileName = new String(filename.getBytes("GBK"),"ISO8859_1"); response.setContentType("application/vnd.ms-excel"); response.addHeader("Content-Disposition", "attachment; filename="+tempfileName); //获取系统所在服务器本地路径 String webPath = request.getSession().getServletContext().getRealPath("/"); System.out.println(webPath); //创建一个存放下载文件的临时目录 String tempFile = "tempDownLoad"; File tempDir = new File(webPath + "/" + tempFile); if(!tempDir.exists()){ tempDir.mkdirs(); } outStream = response.getOutputStream(); myBankCashService.exportByCondition(outStream,user,rqs,rqe,type, configName); outStream.flush(); outStream.close(); }catch(Exception e) { e.printStackTrace(); } return "BPM/client/blank"; }//这个是内部实现Workbook hssf;hssf = BuildXLSHelper.buildXLS(configure, record);//out是前面传进来的hssf.write(out);记得给分 这个在服务器上见一个文件夹先把文件传到服务器上在进行读写InputStream is = null; OutputStream fos = null; int byteSum = 0;//文件字节数 try{ is = formFile.getInputStream(); if(is.available()==0){ return 0; } fos = new FileOutputStream(filePath + "/" + fileName);//建立一个上传文件的输出流 int bytesRead = 0; byte[] buffer = new byte[8192]; while ( (bytesRead = is.read(buffer, 0, 8192)) != -1) { fos.write(buffer, 0, bytesRead);//将文件写入服务器 byteSum += bytesRead; } logger.debug(filePath+"/"+formFile.getFileName()+"已经建立"); 分页代码的jsp代码有问题 对jasypt的加解密 为什么要设置无关的密码? sp业务开发问题,100分 问一个监控程序 Quartz的cron表达式 100分 急求答案 hibernate sql-query问题 在hibernate中只取一个表中记录条数,除了from Table外,还有什么优化的方法吗? 第一次配B/S系统!tomcat+mysql网站不运行! 请教关于DOM的一个很基础问题 oc4j的问题,百分求解 hibernate保存外键问 谁用过 facebook 的 scribe 做过日志系统 ? 用 java 能写客户端么
<input name="fileupload" type="file" />
<input type="checkbox" name="isDeleteTableData" value="checked">
<p>
<input name ="upload" type="submit" value="确定" /><input name="cancel" type="reset" value="取消"/>
<%
String fPathUp=request.getParameter("fileupload");
SmartUpload su=null;
if(fPathUp!=null)
{
String thisfPathUp="";
try
{
su = new SmartUpload();
// 上传初始化
su.initialize(pageContext); //其中这一句我不明白什么意思,我怎么觉得搞来搞去,我用表单提交的文件哪去了
// 设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
su.setAllowedFilesList("xls,XLS");
// 上传文件
su.upload();
// 将上传文件全部保存到指定目录
int count = su.save("/myimport.xls");
out.println(count+"个文件上传成功!<br>");
thisfPathUp="/myimport.xls";
}
catch(Exception ex)
{
out.println("上传失败!");
}
throws ServletException, IOException {
//processRequest(request, response);
String fileName = "", fileName1 = "",flag="",filePath="",logPath="";
FileInputStream fis = null;
BufferedOutputStream out = null;
try {
if (request.getParameter("filename") != null) {
fileName = request.getParameter("filename");
} fileName1 = StringUtil.toUtf8String(fileName.substring(fileName.lastIndexOf("/")+1)); response.setContentType("application/octet-stream");
response.setContentType("application/OCTET-STREAM;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName1); filePath = yourFilePath;
File file = new File(filePath + fileName); try {
fis = new FileInputStream(file);
out = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
out.write(buffer, 0, len);
out.flush();
}
} catch (FileNotFoundException e) {
throw new AppException("您下载的文件文件不存在。");
//e.printStackTrace();
}
} catch (Exception e) {
String msg="";
response.reset();
response.setContentType("text/html;charset=UTF-8");
if(e.getLocalizedMessage()!=null)
msg=e.getMessage();
else
msg="系统错误,捕捉的错误类型为"+e.getClass();
response.getWriter().println("<script>parent.downback('"+ fileName + "','" + msg + "');</script>");
} finally {
try {
if (fis != null) {
fis.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
页面里有个iframe
这个是下载方法
function(){
dowmurl = "t_BUSI_ACOUNT.do?method=statisticsWord&"+searchPanel.getForm().getValues(true);
downf.location.href = dowmurl;
}
//进入这个方法
@RequestMapping(params = RMP_EXPORTBYCONDITION)
public String exportByCondition(HttpServletRequest request,HttpServletResponse response) {
String filename="我的存取款记录.xls";
String configName="bankcash.xml";
Map<String, String> params = RequestUtils.buildParamsByNamesFromRequest(request);
String rqs = request.getParameter("rqs");
String rqe = request.getParameter("rqe");
String type = request.getParameter("type");
User user = (User)RequestUtils.getCurrentUser(request);
OutputStream outStream=null;
try {
String tempfileName = new String(filename.getBytes("GBK"),"ISO8859_1");
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment; filename="+tempfileName);
//获取系统所在服务器本地路径
String webPath = request.getSession().getServletContext().getRealPath("/");
System.out.println(webPath);
//创建一个存放下载文件的临时目录
String tempFile = "tempDownLoad";
File tempDir = new File(webPath + "/" + tempFile);
if(!tempDir.exists()){
tempDir.mkdirs();
}
outStream = response.getOutputStream();
myBankCashService.exportByCondition(outStream,user,rqs,rqe,type, configName);
outStream.flush();
outStream.close();
}catch(Exception e)
{
e.printStackTrace();
}
return "BPM/client/blank";
}//这个是内部实现
Workbook hssf;
hssf = BuildXLSHelper.buildXLS(configure, record);
//out是前面传进来的
hssf.write(out);记得给分
OutputStream fos = null;
int byteSum = 0;//文件字节数
try{
is = formFile.getInputStream();
if(is.available()==0){
return 0;
}
fos = new FileOutputStream(filePath + "/" + fileName);//建立一个上传文件的输出流
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ( (bytesRead = is.read(buffer, 0, 8192)) != -1) {
fos.write(buffer, 0, bytesRead);//将文件写入服务器
byteSum += bytesRead;
}
logger.debug(filePath+"/"+formFile.getFileName()+"已经建立");