如何解决springmvc用jxl下载excel文件指定文件名的问题

如下是用jxl下载excel的代码代码:
@RequestMapping(value = "downexceljxl")
@ResponseBody
public void downexceljxl(HttpServletRequest request, HttpServletResponse response) {
try {
response.setContentType("application/vnd.ms-excel");
OutputStream os=response.getOutputStream();
WritableWorkbook workbook = Workbook.createWorkbook(os);
WritableSheet sheet = workbook.createSheet("First Sheet",0);
Label xuexiao = new Label(0,0,"学校");//列,行,值
sheet.addCell(xuexiao);
Label zhuanye = new Label(1,0,"专业");
sheet.addCell(zhuanye);
Label jingzhengli = new Label(2,0,"专业竞争力");
sheet.addCell(jingzhengli);

        Label qinghua = new Label(0,1,"清华大学");
        sheet.addCell(qinghua);
        Label jisuanji = new Label(1,1,"计算机专业");
        sheet.addCell(jisuanji);
        Label gao = new Label(2,1,"高");
        sheet.addCell(gao);
        workbook.write();
        workbook.close();
        response.setHeader("Content-disposition", "attachment;filename="+java.net.URLEncoder.encode("excel.xls", "UTF-8"));
        os.close();
    }catch (Exception e){
        e.printStackTrace();
    }
}

简单的springmvc代码,用jxl下载excel, 发现谷歌浏览器左下角下载下来的文件名是
downexceljxl.do,为什么不是excel.xls,可以像poi下载excel一样指定文件名吗,望技术牛人帮忙解答一下,谢谢

如下是poi下载excel的例子可以生成指定文件名(**希望能对需要的人带来帮助**),
  @RequestMapping(value = "downexcelpoi")
@ResponseBody
public void downexcelpoi(HttpServletRequest request, HttpServletResponse response)  {
    try {
        //response.reset();//重置输出流
        response.setContentType("application/vnd.ms-excel");

        final String sheetname = "sheet";
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFFont font = workbook.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        style.setFont(font);
        HSSFSheet sheet;
        sheet = workbook.createSheet(sheetname);

        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        cell.setCellValue(new HSSFRichTextString("姓名"));
        cell = row.createCell(1);
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        cell.setCellValue(new HSSFRichTextString("年龄"));
        for (int i=1;i<10;i++){
            row = sheet.createRow(i);
            cell = row.createCell(0);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(new HSSFRichTextString("姓名"+i));
            cell = row.createCell(1);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(new HSSFRichTextString("年龄"+i));
        }

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            workbook.write(bos);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            throw new Exception("导出失败");
        }
        byte[] ba = bos.toByteArray();
        bos.close();
        ByteArrayInputStream bis = new ByteArrayInputStream(ba);
        HSSFWorkbook xwb = (HSSFWorkbook) WorkbookFactory.create(bis);
        OutputStream out=response.getOutputStream();
        xwb.write(bos);
        response.setHeader("Content-disposition", "attachment;filename="+java.net.URLEncoder.encode("excel.xls", "UTF-8"));
    }catch (Exception e){
        e.printStackTrace();
    }
}

4个回答

具体没太理解你的意思;但是springMVC用jxl导出excel就相当于jxl生成一个excel子啊你的某个目录下;再用下载的方式下载下来;生成的路径无所谓
只要你能够读取到那个生成的excel就行
这是我写的;希望能帮到你

//导出
@RequestMapping(value = "/dingdanExcel")
public void downloadExcel(HttpServletRequest request,HttpServletResponse response, OrderQueryParms orderParam) throws IOException {
    //导出文件内容到到项目下
    String savePath=request.getSession().getServletContext().getRealPath("/webpage/device");
    String fileName="123.csv";
    ResultDto<DataGridVo<OrderDto>> queryOrder = orderService.queryOrder(orderParam);
    DingdanExcel.toDingdanExcel(queryOrder.getData().getList(),savePath+"/123.csv");
    //下载
    response.setContentType("application/vnd.ms-excel");
    File file = new File(savePath+System.getProperty("file.separator")+fileName);
    // 清空response
    response.reset();
    // 设置response的Header
    response.addHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("gbk"),"iso-8859-1"));  //转码之后下载的文件不会出现中文乱码
    response.addHeader("Content-Length", "" + file.length());
    try{
        //以流的形式下载文件
        InputStream fis = new BufferedInputStream(new FileInputStream(savePath+System.getProperty("file.separator")+fileName));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        toClient.write(buffer);
        toClient.flush();
        toClient.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}
jiajiax16
jiajiax16 看到你的回复,尝试了一下果然可以,马上登陆给你一个赞.谢谢
大约一年之前 回复
higherzjm
higherzjm 谢谢你的分享,你这种方式是可以,但就是需要一个临时文件中转站,服务器上先生成一个文件,然后浏览器下载完再删除。如果说能够像poi一样api内部就生成了文件的inputstream最好啊
接近 2 年之前 回复

具体下载 下来的文件名你是可以设置的;比如我的代码中我就写成123.csv

higherzjm
higherzjm 如果下载普通的txt文件就可以啊,excel文件就不行,普通的文件可以直接response.getOutputStream().write("我的文件".getBytes("UTF-8")),excel不行,1楼的做法是可以的
接近 2 年之前 回复

楼上回答正确无误,希望能解决困惑。

       response.setContentType("application/x-msdownload");
       response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode("这里就是你要指定的文件名称.xls","UTF-8"));
higherzjm
higherzjm excel不行
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!