java 导出
按钮
<a href="###" class="eui-btn eui-btn-small" onclick="Export()"><i class="eui-icon" ></i>模板下载</a>
javascript
//导出数据
function Export() {
window.open("/Summary/excelExport"); }
控制器
//导出
@RequestMapping("/excelExport")
@ResponseBody
public void excelExport(String params, HttpServletRequest
request , HttpServletResponse response,String projectCode
,String projectName,String engineeringName,Integer status,String creatorName,String creatororgname,String createTime,String providername) throws Exception{ BufferedInputStream in = null;
BufferedOutputStream out = null;
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟 // path是指欲下载的文件的路径。
/*File file = new File(path);*/
//创建excel
HSSFWorkbook wb = new HSSFWorkbook();
String fileName = "科目信息表.xls";//导出时下载的EXCEL文件名
//创建sheet
HSSFSheet sheet = wb.createSheet("科目信息");
sheet.setDefaultColumnWidth();
sheet.setDefaultRowHeightInPoints();
//创建一行
HSSFRow rowTitle = sheet.createRow();
rowTitle.setHeightInPoints(); HSSFCellStyle styleTitle = wb.createCellStyle();
styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 居中 HSSFCellStyle styleCenter = wb.createCellStyle();
styleCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 居中
styleCenter.setWrapText(true); // 设置为自动换行 // 在行上创建1列
HSSFCell cellTitle = rowTitle.createCell();
// 列标题及样式
cellTitle.setCellValue("科目ID");
cellTitle.setCellStyle(styleTitle); // 在行上创建2列
cellTitle = rowTitle.createCell();
cellTitle.setCellValue("科目名称");
cellTitle.setCellStyle(styleTitle);
cellTitle = rowTitle.createCell();
cellTitle.setCellValue("上年实际");
cellTitle.setCellStyle(styleTitle);
cellTitle = rowTitle.createCell();
cellTitle.setCellValue("本年预算");
cellTitle.setCellStyle(styleTitle);
cellTitle = rowTitle.createCell();
cellTitle.setCellValue("1月");
cellTitle.setCellStyle(styleTitle);
cellTitle = rowTitle.createCell();
cellTitle.setCellValue("2月");
cellTitle.setCellStyle(styleTitle); cellTitle = rowTitle.createCell();
cellTitle.setCellValue("3月");
cellTitle.setCellStyle(styleTitle);
cellTitle = rowTitle.createCell();
cellTitle.setCellValue("4月");
cellTitle.setCellStyle(styleTitle);
cellTitle = rowTitle.createCell();
cellTitle.setCellValue("5月");
cellTitle.setCellStyle(styleTitle);
cellTitle = rowTitle.createCell();
cellTitle.setCellValue("6月");
cellTitle.setCellStyle(styleTitle);
cellTitle = rowTitle.createCell();
cellTitle.setCellValue("7月");
cellTitle.setCellStyle(styleTitle);
cellTitle = rowTitle.createCell();
cellTitle.setCellValue("8月");
cellTitle.setCellStyle(styleTitle);
cellTitle = rowTitle.createCell();
cellTitle.setCellValue("9月");
cellTitle.setCellStyle(styleTitle);
cellTitle = rowTitle.createCell();
cellTitle.setCellValue("10月");
cellTitle.setCellStyle(styleTitle);
cellTitle = rowTitle.createCell();
cellTitle.setCellValue("11月");
cellTitle.setCellStyle(styleTitle);
cellTitle = rowTitle.createCell();
cellTitle.setCellValue("12月");
cellTitle.setCellStyle(styleTitle); List<BudgetDetail> budgetDetails=budgetDetailService.ExcelInfo();
for(int i=;i<budgetDetails.size();i++){
BudgetDetail budgetDetail = budgetDetails.get(i);
HSSFRow row = sheet.createRow(i + );
row.setHeightInPoints(); HSSFCell cell = row.createCell();
cell.setCellValue(budgetDetail.getSubid());//科目ID
cell.setCellStyle(styleCenter); cell = row.createCell();
cell.setCellValue(budgetDetail.getSubname());//科目名称
cell.setCellStyle(styleCenter);
} String path="c:/aaaa.xls";
String path2="D:/aaaa.xls";
FileOutputStream fout = null;
try{
fout = new FileOutputStream(path);
}catch (Exception e){
fout = new FileOutputStream(path2);
}
wb.write(fout);
fout.close();
wb.close();
try {
try {
InputStream inputStream = new FileInputStream(ResourceUtils.getFile(path));
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setContentType("applicationnd/octet-stream");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
in = new BufferedInputStream(inputStream);
out = new BufferedOutputStream(response.getOutputStream());
}catch (Exception e){
InputStream inputStream = new FileInputStream(ResourceUtils.getFile(path2));
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setContentType("applicationnd/octet-stream");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
in = new BufferedInputStream(inputStream);
out = new BufferedOutputStream(response.getOutputStream());
}
byte[] data = new byte[];
int len = ;
while (- != (len=in.read(data, , data.length))) {
out.write(data, , len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
} File f = new File(path);
if (f.exists()){
f.delete();//删除
}
File f1=new File(path2);
if (f1.exists()){
f1.delete();
}
} }
java 导出的更多相关文章
- java导出生成word(类似简历导出)
参考帖子: http://www.cnblogs.com/lcngu/p/5247179.html http://www.cnblogs.com/splvxh/archive/2013/03/15/2 ...
- java导出生成word
最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前来看,java导出word大致有6种解决方案: 1:Jacob是Java-COM Bridge的 ...
- JAVA导出pdf实例
一.直接导出成PDF Java代码 1. import java.io.FileNotFoundException; 2. import java.io.FileOutputStream; 3. ...
- java导出word的6种方式(复制来的文章)
来自: http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前 ...
- java导出excel报错:getOutputStream() has already been called for this response
对于java导出excel报错的问题,查了很多都说是在使用完输出流以后调用以下两行代码即可 out.clear(); out = pageContext.pushBody(); 但这也许是页面上输出时 ...
- java导出excel表格
java导出excel表格: 1.导入jar包 <dependency> <groupId>org.apache.poi</groupId> <artifac ...
- java导出excel报表
1.java导出excel报表: package cn.jcenterhome.util; import java.io.OutputStream;import java.util.List;impo ...
- Java导出Excel和CSV(简单Demo)
Java导出Excel和CSV的简单实现,分别使用POI和JavaCSV. JavaBean public class ReportInfo { int id; String date; int nu ...
- Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类
Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...
- java导出excel模板数据
Java导出excel数据模板,这里直接贴代码开发,流程性的走下去就是步骤: String[] colName=new String[]{"期间","科目代码" ...
随机推荐
- MongoDB- 简单操作命令
MongoDB是基于集合操作的数据库 1.进入与退出 mongo / exit 2.库操作 显示所有库: show dbs; 查看当前所在库: db; 切换&使用某个库: use db_nam ...
- Window10上安装Redis及其客户端
资源下载地址 Redis安装包:https://github.com/MicrosoftArchive/redis/releases Redis客户端: 链接:https://pan.baidu.co ...
- windows docker redis
拉取docker docker pull hub.c.163.com/library/redis:latest 启动docker docker run -p 6379:6379 -d hub.c.16 ...
- 解决 WordPress“正在执行例行维护,请一分钟后回来”
WordPress在升级程序.主题.插件时,都会先切换到维护模式,也就是显示 “正在执行例行维护,请一分钟后回来(Briefly unavailable for scheduled maintenan ...
- java基础-03基本语法
关键词 常用关键字53个(含2个保留字): 1.保留关键字(2个) const --常量 常数:用于修改字段或局部变量的声明. goto--转到 指定跳转到标签,找到标签后,程序将处理从下一行开始的命 ...
- python中的线程技术
#!/user/bin/env python # @Time :2018/7/7 11:42 # @Author :PGIDYSQ #@File :DaemonTest.py import threa ...
- OO第一单元总结——多项式求导
第一次作业分析 1.程序结构分析 类图: 好吧,这一次基本上完全是在面向过程编程,没有看出来任何的面向对象的特性. 复杂度: 可以看到模块间的相互耦合度很高,PolyDerive方法的非结构化程度也不 ...
- flutter Provide 状态管理篇
Provide是Google官方推出的状态管理模式.官方地址为: https://github.com/google/flutter-provide 现在Flutter的状态管理方案很多,redux. ...
- mysql主主配置
数据安装完成后 配置信息 开启二进制文件复制 [client]port=3306[mysqld]basedir=/usr/local/mysqldatadir=/usr/local/mysql/da ...
- 解决win环境下访问本机虚拟机中centos7 ftp服务器的问题
inux搭建ftp服务器 1.安装软件: yum install vsftpd 2.修改配置文件vsftpd.conf: vim /etc/vsftpd/vsftpd.conf 把anonymous_ ...