java 生成execl下载

/**
     * execl Export
     */
    private void createExecl(HttpServletRequest request, HttpServletResponse response, List<PlaneStatement> list)
    {
        String docsPath = request.getSession().getServletContext().getRealPath("docs");
        String fileName = "export2007_" + System.currentTimeMillis() + ".xlsx";
        String filePath = docsPath + FILE_SEPARATOR + fileName;
        System.out.println("docsPath = " + docsPath);
        System.out.println("filePath=" + filePath);
        File file = new File(docsPath);
        if(!file.exists()){
            file.mkdir();
        }

        OutputStream os = null;
        try
        {
            // 输出流
            os = new FileOutputStream(filePath);
            // 工作区

            XSSFWorkbook wb = new XSSFWorkbook();
            XSSFSheet sheet = wb.createSheet("sheet1");
            
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            for (int i = 0; i <= list.size(); i++)
            {
                // 创建第一个sheet
                // 生成第一行
                XSSFRow row = sheet.createRow(i);
                if (i != 0)
                {
                      PlaneStatement p = list.get(i-1);
                      System.out.println("execl:"+p);
                    if(p.getId()!=null){
                        row.createCell(0).setCellValue(p.getId());
                    }
                   
                }
                else
                {
                    row.createCell(0).setCellValue("序号");
                }
            }
            // 写文件
            wb.write(os);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        } finally
        {
            if (os != null)
            {
                try
                {
                    os.close();
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        SysUtils.download(filePath, response);
    }

  

/**
     * 下载
     * 
     * @param path
     * @param response
     */
    public static void download(String path, HttpServletResponse response)
    {
        InputStream fis = null;
        OutputStream toClient = null;
        File file = null;
        try
        {
            // path是指欲下载的文件的路径。
            file = new File(path);
            // 取得文件名。
            String filename = file.getName();
            // 以流的形式下载文件。
            fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);

            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            response.addHeader("Content-Length", "" + file.length());
            toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/vnd.ms-excel;charset=gb2312");
            toClient.write(buffer);
            toClient.flush();

        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        } finally
        {

            try
            {
                if (fis != null)
                {
                    fis.close();
                }
                if (toClient != null)
                {
                    toClient.close();
                }
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
//             删除文件
            if(file.exists()){
                file.delete();
            }
        }
    }

  

    private static final String   FILE_SEPARATOR = System.getProperties().getProperty("file.separator");

  

原文地址:https://www.cnblogs.com/yanqin/p/6163607.html