求教,导出生成Excel 是弹出下载框。FileOutputStream fileOut new FileOutputStream(C:/Users/Administr
求教,导出生成Excel 是弹出下载框。
FileOutputStream fileOut = new FileOutputStream("C:/Users/Administrator/Desktop/测试ONG.xls");
wb.write(fileOut);
fileOut.close();
我现在是直接把文件生成在桌面上的, 我想要先弹出下载框,然后自己选择保存路径,请问怎么弄,请各位教教小弟,能有代码最好了,谢谢。 Excel Java File 导出 下载
[解决办法]public InputStream getInputStream() throws Exception {
Annex annex=annexService.find(Annex.class, id);
File file = new File(getFileBasePath() + annex.getUrl());
File file2=new File(getFileBasePath() + File.separator+"download"+File.separator+DecoderUtil.UtfDecoder(annex.getName()));
file2.getParentFile().mkdir();
FileUtil.copyFile(file, file2);
try {
this.setFileName(new String(file2.getName().getBytes(), "ISO8859-1"));
tempStream = new java.io.FileInputStream(file2);//从系统磁盘文件读取数据
bytes = new byte[tempStream.available()];
if(tempStream != null) {
tempStream.read(bytes);
}
tempStream.close();
return new ByteArrayInputStream(bytes);
} catch(Exception e) {
return null;
}finally{
FileUtil.deleteFile(file2.toString());
}
}
这个是下载的action,我写的一个简单的东西
[解决办法]这个导出成功了也是一样的,直接就下载了
/**
* wangjiafeng
* TODO 导出自行车电动车信息Excel
* @method educeExcel
* @return
* @throws Exception
* 2013 上午10:20:25
*/
public String educeEleCarExcel() throws Exception{
Member member=getMember();
if(member != null ){
Date date=new Date(System.currentTimeMillis());
String test=DateUtil.parseDateToString(date, "yyyy-MM-ddHH:mm:ss").replace("-", "").replace(":", "").trim();
String path = request.getSession().getServletContext().getRealPath("/") + "ElectCarExcel"+test+".xls";
List<ElectCarInfor> list=electCarInforService.queryByAdminAndFrontList(DecoderUtil.UtfDecoder(carriageId), DecoderUtil.UtfDecoder(electCarId),
DecoderUtil.UtfDecoder(brand), DecoderUtil.UtfDecoder(model), DecoderUtil.UtfDecoder(name),
DecoderUtil.UtfDecoder(personid), DecoderUtil.UtfDecoder(phoneNum1),robTime,DecoderUtil.UtfDecoder(robPlace),
DecoderUtil.UtfDecoder(regUnit),regDate, flag, null, null);
if(list != null && list.size() > 0){
ExcelUtil excelUtil=new ExcelUtil();
excelUtil.createExcel(path, list);
excelUtil.downLoadExcel(response, path);
FileUtil.deleteFile(path);
}
return null;
}else{
return "memberLogin";
}
public void downLoadExcel(HttpServletResponse response,String filePath ) throws IOException {
File f = new File(filePath);
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
response.setContentType("application/xls;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.flush();
out.close();
}