/**
     * 生成excel并下载
     */
    public void exportExcel(){
       
        File newFile = createNewFile();
        //File newFile = new File("d:/ss.xls");
       
        //新文件写入数据,并下载*****************************************************
        InputStream is = null;
        HSSFWorkbook workbook = null;
        HSSFSheet sheet = null;
        try {
            is = new FileInputStream(newFile);
            workbook = new HSSFWorkbook(is);
            //获取第一个sheet
            sheet = workbook.getSheetAt(0);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
       
        if(sheet != null){
            try {
                //写数据
                FileOutputStream fos = new FileOutputStream(newFile);
                HSSFRow row = sheet.getRow(4);
                HSSFCell cell = row.getCell(1);
                System.out.println(cell.getStringCellValue());
                cell.setCellValue("ssssssssssssssssssssssssssssssssssssssssssss");
                workbook.write(fos);
                fos.flush();
                fos.close();
               
                //下载
               InputStream fis = new BufferedInputStream(new FileInputStream(newFile));
               HttpServletResponse response = this.getResponse();
               byte[] buffer = new byte[fis.available()];
               fis.read(buffer);
               fis.close();
               response.reset();
               response.setContentType("text/html;charset=UTF-8");
               OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                  response.setContentType("application/x-msdownload");
                  String newName = URLEncoder.encode("采购合同"+System.currentTimeMillis()+".xls", "UTF-8");
                  response.addHeader("Content-Disposition", "attachment;filename=\""+ newName + "\"");
                  response.addHeader("Content-Length", "" + newFile.length());
               toClient.write(buffer);
               toClient.flush();
            }
            catch(Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    if (null != is) {
                        is.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        //删除创建的新文件
        //this.deleteFile(newFile);
    }
    /**
    * 复制文件
    * @param s
    * 源文件
    * @param t
    * 复制到的新文件
    */

public void fileChannelCopy(File s, File t) {
        try {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = new BufferedInputStream(new FileInputStream(s),1024);
                out = new BufferedOutputStream(new FileOutputStream(t),1024);
                byte[] buffer = new byte[1024];
                int len;
                while ((len=in.read(buffer))!=-1) {
                    out.write(buffer,0,len);
                }
            } finally {
                if (null != in) {
                    in.close();
                }
                if (null != out) {
                    out.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
    /**
     * 读取excel模板,并复制到新文件中供写入和下载
     * @return
     */
    public File createNewFile(){
        //读取模板,并赋值到新文件************************************************************
        //文件模板路径
        String path = this.getRequest().getRealPath(SystemConfig.FILETEMPLATE);
        String fileName="purchaseContract.xls";
        File file=new File(path+"/"+fileName);

//保存文件的路径
        String realPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.UPLOAD_FILE_DIR);
        //新的文件名
        String newFileName = "采购合同"+System.currentTimeMillis() + ".xls";
        //判断路径是否存在
        File dir = new File(realPath);
        if(!dir.exists()){
            dir.mkdirs();
        }
        //写入到新的excel
        File newFile = new File(realPath, newFileName);
        try {
            newFile.createNewFile();
            //复制模板到新文件
            fileChannelCopy(file, newFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newFile;
    }

/**
     * 下载成功后删除
     *
     * @param files
     */
    private void deleteFile(File... files) {
        for (File file : files) {
            if (file.exists()) {
                file.delete();
            }
        }
    }

复制Excel模板,填写Excel模板,然后下载相关推荐

  1. 用计算机根据姓名填充地址,如何在一个excel表格中引用另一个表格的数据,并自动填充-excel自动填写,excel按照姓名自动匹配数据...

    操作如下抄: 1.首先建立好月数据表格 2.之后是要在日数据袭表格中将两个表中项目(示例,可设为其他条件)所对应的数据自动填写 3.点击输入数据的首行单元格,选择插入函百数 4.选择VLOOKUP函数 ...

  2. IDEA设置注释模板——类模板和方法模板(参数,返回值)

    一.类的注释模板 文件------设置-------------编辑器-------文件和代码模板 模板: /*** ----------------------------------------- ...

  3. excel表格内容拆分_一键批量拆分Excel工作表【模板下载】

    今年5月份我们在Excel表哥微信公众号平台首发了一系列工作表.工作簿合并的文章. 从合并相同内容的单元格到合并多个工作表直至批量合并多个Excel工作簿,所有的操作只需要一键! 专辑如下,也可以在表 ...

  4. java根据Freemarker模板渲染出Excel文件并在浏览器中下载

    **java根据Freemarker模板渲染出Excel文件并在浏览器中下载** 准备工作 1.导入的依赖 2.创建模板 Freemrker语法大全: [Freemarker语法使用请点击该链接跳转学 ...

  5. Python 根据Excel数据源批量自动填写Excel模板

    Python 根据Excel数据源批量填写Excel模板 #!/usr/bin/env python3 # -*- coding: utf-8 -*-import xlwt from xlutils. ...

  6. python 会计专用格式_excel会计专用格式模板下载|excel会计专用格式模板680个 最新版 下载 - 巴士下载站...

    680个excel会计专用格式模板里面包含了很多种类别的Excel表格模板,专门供给财务使用,主要是帮你自动生成表格,节省了你不少时间,欢迎在WWW.11684.COM 下载! 软件特色 1.涵盖了常 ...

  7. 使用EasyPoi利用excel模板导出excel表格下载

    前言:使用excel模板导出excel的好处在于可以事先在模板上定义颜色.格式等,适用于模板设计得比较灵活复杂的场景 一.添加jar包 <dependency><groupId> ...

  8. springboot使用jxls导出excel___(万能通用模板)--- SpringBoot导入、导出Excel文件___SpringBoot整合EasyExcel模板导出Excel

    springboot使用jxls导出excel 实现思路: 首先在springBoot(或者SpringCloud)项目的默认templates目录放入提前定义好的Excel模板,然后在具体的导出接口 ...

  9. Web在线打印设计器即将推出,像Excel一样在线设计模板

    软件项目中总会遇到 "单据打印" 需求,特别是国企项目,一直以来没有遇到一个免费的简单好用的打印设计器,大部分项目都是手工编码,"耗时耗力,又各种兼容调试,那个痛苦啊&q ...

  10. Java 根据Excel模板 导出Excel报表

    工作中肯定会有的报表导出的功能,咱先来理下思路 先定义好模板文件,存放到一个有权限访问的目录内 根据模板路径,获取模板文件 /*** 根据模板的路径获取模板文件对象* classpath根目录下:/t ...

最新文章

  1. 乔安监控云存储_让数据更安全可用,阿里云存储多项新功能发布
  2. iOS 打电话回到当前应用
  3. python是人工智能最大骗局-培训三个月拿“高薪”?起底人工智能“速成班”忽悠大法...
  4. SSM整合Shiro进行登陆认证和授权详细配置
  5. 鸿蒙股票深度分析,本月华为鸿蒙概念股市回顾分析(3月31日)
  6. windows主机加固和评测
  7. [USACO1.5]八皇后 Checker Challenge
  8. matlab 遗传优化算法_转载 | 遗传算法解决TSP问题的MATLAB实现
  9. latex中括号大小控制 [转]
  10. PDF超过6000页,OMG,学它
  11. 临时号码,接收短信验证码
  12. 注册表删除计算机用户密码,修改注册表删除WIN10开机密码
  13. mac注销快捷键_Mac快捷键大全
  14. 互联网快讯:饿了么宣布成立蓝骑士智囊团;极米高性能产品获好评;OPPO Reno7系列正式发布
  15. win10怎么取消开机密码
  16. 樱道,空蝉,雨空,夏恋,雨道,彩月,幻昼,惊梦,白夜。这些纯音乐
  17. 第二章补充____微分中值定理及导数应用
  18. Android开发规范
  19. Element ui 组件中用键盘事件
  20. 我访问了一个「假」的 GitHub,难道是 PronHub ,还是 GayHub ?

热门文章

  1. 【百科】中药指纹图谱研究是什么
  2. 图解AI数学基础:从入门到精通系列教程(要点速查清单·完结)
  3. Android UI设计之五自定义DrawView组件,实现数字签名效果
  4. sql、DB、DBMS分别是什么,他们之间什么关系?
  5. 解决UOS家庭版桌面图标消失,文件管理器进不去
  6. 回文联对联大全_对联大全回文联
  7. outlook如何设置定时发送邮件呢
  8. 基于R语言非参数统计Brown-Mood中位数检验和Wilcoxon-Mann-Whitney秩和检验
  9. cocos2dx卡牌翻转效果
  10. scrollView滚动视图实现商城模块