NutzCN Logo
问答 poi导出excel没有抛出异常 但是没有输出文件
发布于 2335天前 作者 qq_412bd180 2230 次浏览 复制 上一个帖子 下一个帖子
标签:

代码:
util类

if(workbook !=null){
            	OutputStream out=null;
                try
                {
                    String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";
                    String headStr = "attachment; filename=\"" + fileName + "\"";
                    //
                    //response.reset();
                    response.setContentType("APPLICATION/OCTET-STREAM");
                    response.setHeader("Content-Disposition", headStr);
                    out = response.getOutputStream();
                    System.out.println("fileName:"+fileName);
                    System.out.println("开始写文件#####################");
                    workbook.write(out);
                    out.close();
                    
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                finally {
                	out.close();
                }

controller:

ExportExcel ex = new ExportExcel(title, rowsName, dataList);
        HttpServletResponse response=Mvcs.getResp();
        ex.export(response);
        System.out.println();
10 回复

这是写到resp流里面,哪来文件

应该怎么写呢 搞了一上午 谢谢了

out = new FileOutputStream("D://学生表.xls");
                	workbook.write(out);
                    out.close();

这样写可以写到文件里了 那么之前的代码修改一下能实现弹出保存excel文件的功能吗?

你想ajax下载文件??

一般是要求a标签或者form下载, 如果想ajax下载的话, 得开个iframe或者找个ajaxfiledownload的js插件

ajax的下载文件我会 需要提交一个表单 能直接下载吗?

那你现在是怎么做的?

浏览器没有动静?chrome控制台看看服务器响应的内容

//导出excel
	@At
	@Ok("raw")
	public void exportExcel() throws Exception {
		String title = "故障统计";
        String[] rowsName = new String[]{"序号","货物运输批次号","提运单号","状态","录入人","录入时间"};
        List<Object[]>  dataList = new ArrayList<Object[]>();
        Object[] objs = null;
        ExportExcel ex = new ExportExcel(title, rowsName, dataList);
        HttpServletResponse response=Mvcs.getResp();
        ex.export(response);
        System.out.println();
	}

点击导出 查看network 有输出流 双击可以打开下载excel 问题是怎么让它直接下载呢

当前是怎么触发的?ajax吗?

添加回复
请先登陆
回到顶部