java实现赋值excel模板,并在新文件中写入数据,并且下载
/**
* 生成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模板,并在新文件中写入数据,并且下载的更多相关文章
- 计算机二级-C语言-程序填空题-190117记录-对文件的处理,复制两个文件,往新文件中写入数据。
//给定程序的功能是,调用函数fun将指定源文件中的内容赋值到指定目标文件中,复制成功时函数返回1,失败时返回0,把复制的内容输出到终端屏幕.主函数中源文件名放在变量sfname中,目标文件名放在变量 ...
- Java读取、写入、处理Excel文件中的数据(转载)
原文链接 在日常工作中,我们常常会进行文件读写操作,除去我们最常用的纯文本文件读写,更多时候我们需要对Excel中的数据进行读取操作,本文将介绍Excel读写的常用方法,希望对大家学习Java读写Ex ...
- java:利用java的输入/输出流将一个文件的每一行+行号复制到一个新文件中去
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- 使用NPOI插件读取excel模版修改数据后保存到新目录新文件中
添加引用: using System.IO; using NPOI.XSSF.UserModel; using NPOI.SS.UserModel; using NPOI.HSSF.UserModel ...
- POI往word模板中写入数据
转: POI往word模板中写入数据 2018年03月24日 16:00:22 乄阿斗同學 阅读数:2977 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn ...
- 复制excel表,往excel表中写入数据
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import jav ...
- java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- 效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】) 转
效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中[附源代码下载]) 本文目录: (一)背景 (二)数据库数据导入到Excel的方法比较 ...
- Java生鲜电商平台-SpringCloud微服务开发中的数据架构设计实战精讲
Java生鲜电商平台-SpringCloud微服务开发中的数据架构设计实战精讲 Java生鲜电商平台: 微服务是当前非常流行的技术框架,通过服务的小型化.原子化以及分布式架构的弹性伸缩和高可用性, ...
随机推荐
- MySQL基础命令
目录: MySQL概述 安装及启动登陆mysql操作 MySQL基本数据类型 MySQL数据库基本SQL操作 MySQL高级SQL操作 一.MySQL概述 1.什么是数据库? 数据库(Database ...
- Spring第五弹—–配置Spring管理的bean的作用域和生命周期
singleton (默认方式) 在每个Spring IoC容器中一个bean定义只有一个对象实例.默认情况下会在容器启动时初始化bean,但我们可以指定bean节点的lazy-init=“true” ...
- Spring框架第五篇之Spring与AOP
一.AOP概述 AOP(Aspect Orient Programming),面向切面编程,是面向对象编程OOP的一种补充.面向对象编程是从静态角度考虑程序的结构,而面向切面编程是从动态角度考虑程序运 ...
- Java中的哈夫曼树
package com.ietree.basic.datastructure.tree; import java.util.ArrayDeque; import java.util.ArrayList ...
- Python的数据类型和常用方法大全
数据类型 一.数字 整形int x=10 #x=int(10) print(id(x),type(x),x) 浮点型float salary=3.1 #salary=float(3.1) print( ...
- 取MAC地址 (含多网卡),最好的方法,支持Vista,Win7
取MAC地址 (含多网卡),最好的方法,支持Vista,Win7 unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Cl ...
- 转:js获取json中key所对应的value值
<script type="text/javascript"> getJson('age'); function getJson(key){ var jsonObj={ ...
- Jenkins系统+独立部署系统
原文出自:http://os.51cto.com/art/201601/504846.htm 有了Jenkins,为什么还需要一个独立的部署系统? 现在已经有Jenkins,它自身提供了丰富的部署插件 ...
- 【转】Winform Socket通信
Socket相关概念[端口] 在Internet上有很多这样的主机,这些主机一般运行了多个服务软件,同时提供几种服务.每种服务都打开一个Socket,并绑定到一个端口上,不同的端口对应于不同的服务(应 ...
- Python3.x:pip命令安装第三方库,超时处理方案
Python3.x:pip命令安装第三方库,超时处理方案 问题: pip install splinter命令安装第三方库,报超时错误: raise ReadTimeoutError(self._po ...