/**
* 生成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();
}
}
}

  

出处:https://www.cnblogs.com/kisstear/p/5461494.html

java实现赋值excel模板,并在新文件中写入数据,并且下载的更多相关文章

  1. 计算机二级-C语言-程序填空题-190117记录-对文件的处理,复制两个文件,往新文件中写入数据。

    //给定程序的功能是,调用函数fun将指定源文件中的内容赋值到指定目标文件中,复制成功时函数返回1,失败时返回0,把复制的内容输出到终端屏幕.主函数中源文件名放在变量sfname中,目标文件名放在变量 ...

  2. Java读取、写入、处理Excel文件中的数据(转载)

    原文链接 在日常工作中,我们常常会进行文件读写操作,除去我们最常用的纯文本文件读写,更多时候我们需要对Excel中的数据进行读取操作,本文将介绍Excel读写的常用方法,希望对大家学习Java读写Ex ...

  3. java:利用java的输入/输出流将一个文件的每一行+行号复制到一个新文件中去

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  4. 使用NPOI插件读取excel模版修改数据后保存到新目录新文件中

    添加引用: using System.IO; using NPOI.XSSF.UserModel; using NPOI.SS.UserModel; using NPOI.HSSF.UserModel ...

  5. POI往word模板中写入数据

    转: POI往word模板中写入数据 2018年03月24日 16:00:22 乄阿斗同學 阅读数:2977  版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn ...

  6. 复制excel表,往excel表中写入数据

    import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import jav ...

  7. java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  8. 效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】) 转

    效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中[附源代码下载])    本文目录: (一)背景 (二)数据库数据导入到Excel的方法比较   ...

  9. Java生鲜电商平台-SpringCloud微服务开发中的数据架构设计实战精讲

    Java生鲜电商平台-SpringCloud微服务开发中的数据架构设计实战精讲 Java生鲜电商平台:   微服务是当前非常流行的技术框架,通过服务的小型化.原子化以及分布式架构的弹性伸缩和高可用性, ...

随机推荐

  1. POJ3903Stock Exchange&&POJ1631Bridging signals最长上升子序列 &&POJ1887Testing the CATCHER(最长下降子序列)(LIS模版题)

    题目链接:http://poj.org/problem?id=3903 题目链接:http://poj.org/problem?id=1631 题目链接:http://poj.org/problem? ...

  2. matplotlib的一些代码

    Matplotlib Python 画图教程 (莫烦Python)_演讲•公开课_科技_bilibili_哔哩哔哩 https://www.bilibili.com/video/av16378354/ ...

  3. java static成员变量方法和非static成员变量方法的区别 ( 二 )

    原创文章,未经作者允许,禁止转载!!! 静态成员变量不用new对象,在类加载的过程中就已经初始化存放在数据区域,静态成员变量是类和所有对象共有的,类和对象都可以改变它的值,每一次改变值之后,静态成员变 ...

  4. Java中Map用法详解

    原文地址http://blog.csdn.net/guomutian911/article/details/45771621 原文地址http://blog.csdn.net/sunny2437885 ...

  5. day5-正则表达式 re

    re模块用于对python的正则表达式的操作. 字符: . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的开始或结束 ^ 匹配 ...

  6. python中is和==区别

    is比较两个对象的id值是否相等,是否指向同一个内存地址 ==比较的是两个对象的内容是否相等,值是否相等 is运算符比==效率高,在变量和None进行比较时,应该使用is

  7. hihocoder1479 三等分

    地址:http://hihocoder.com/problemset/problem/1479 题目: 三等分 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近 ...

  8. 金融 贷款类 App 审核被拒 4.3 1.2 2.1 4.2.2 问题总结

    辛辛苦苦搞了一两个月,开发测试修bug,一路艰辛,到了审核这最后一关,各位同仁,咬紧牙关!接下来是鄙人遇到过的被拒问题,望能帮到诸君! ******************************** ...

  9. 分布式存储系统 Ceph

    你了解Ceph吗? Ceph是一种分布式存储系统,它可以将多台服务器组成一个超大集群,把这些机器中的磁盘资源整合到一块儿,形成一个大的资源池(PB级别),然后按需分配给应用使用. 那么你知道Ceph的 ...

  10. nginx添加sticky cookie 分流模块

    需要下载nginx源码和sticky,在nginx配置文件中添加sticky模块,然后重新编译nginx. #准备安装基础环境:yum install gcc openssl-devel pcre-d ...