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[]{"期间","科目代码" ...
随机推荐
- ThreadLocal源码解读
1. 背景 ThreadLocal源码解读,网上面早已经泛滥了,大多比较浅,甚至有的连基本原理都说的很有问题,包括百度搜索出来的第一篇高访问量博文,说ThreadLocal内部有个map,键为线程对象 ...
- Ant Design Pro+Electron+electron-builder实现React应用脱离浏览器,桌面安装运行
ant-design-pro ----> version :2.3.1 由于网上Ant Design Pro+Electron的资料太少,我就贡献一点经验 最近需要讲AntD Pro项目(以 ...
- 转://从一条巨慢SQL看基于Oracle的SQL优化
http://mp.weixin.qq.com/s/DkIPwbDKIjH2FMN13GkT4w 本次分享的内容是基于Oracle的SQL优化,以一条巨慢的SQL为例,从快速解读SQL执行计划.如何从 ...
- 浅谈 Angular 项目实战
为什么使用 Angular 我不是 Angular 的布道者,但如今痴迷 Angular,使用 Angular 做项目让我有一种兴奋感.目前的三大主流前端框架都研究过,博客中也有三者的相关教程,最早接 ...
- clipboardjs复制到粘贴板
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...
- linux 下ab压力测试
1.ab的简介 ab是apachebench命令的缩写. ab是apache自带的压力测试工具.ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试 ...
- linux定时备份学习笔记
1.iterm2链接远程中文乱码 shh端vi ~/.bash_profile export LC_CTYPE=en_US.UTF-8 source ~/.bash_profile 2.WARNI ...
- position: fixed; ios 无法滑动解决
添加以下代码搞定 -webkit-overflow-scrolling: touch; overflow-y: scroll;
- 高斯消元part2
今天整一整高斯消元的模板,正经的 高斯消元主要用于解n元一次线性方程组与判断是否有解 主要思想? 就是高斯消元啊 主要思想是理想状态下消为每行除最后一项外只有一个1,并且每行位置互异,具体看下面. 这 ...
- Django中ORM介绍
Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...