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[]{"期间","科目代码" ...
随机推荐
- 想知道谁是你的最佳用户?基于Redis实现排行榜周期榜与最近N期榜
本文由云+社区发表 前言 业务已基于Redis实现了一个高可用的排行榜服务,长期以来相安无事.有一天,产品说:我要一个按周排名的排行榜,以反映本周内用户的活跃情况.于是周榜(按周重置更新的榜单)诞生了 ...
- WebApi 身份认证解决方案:Basic基础认证
前言:最近,讨论到数据库安全的问题,于是就引出了WebApi服务没有加任何验证的问题.也就是说,任何人只要知道了接口的url,都能够模拟http请求去访问我们的服务接口,从而去增删改查数据库,这后果想 ...
- Windows -- 从注册表删除IE浏览器加载项
Windows -- 从注册表删除IE浏览器加载项 1. 一部分加载项从注册表以下位置直接删除 2. 一部分扩展项从注册表以下位置直接删除
- 基本服务器的AAA实验(Cisco PT)
1.实验拓扑 2.不通网段间的ping通测试 从pc-a ping到pc-b 从pc-a ping到pc-c 从pc-b ping到pc-c 3.配置过程 a.在路由器R1上配置一个本地用户账号并且利 ...
- SQLserver2008一对多,多行数据显示在一行
现在有一个需求 我们有一张表employee EmpID EmpName ---------- ------------- 张山 张大山 张小山 李菲菲 李晓梅 Result I need in th ...
- Photoshop快速给美女人像换头发
今天给大家带来的教程是应用PS来抠出人物图片的发丝和修改头发的颜色. OK开始今天的教程 1.将素材文件拖拽进PS,CTRL+J复制一层. 2.应用快速选择工具大致的将头发部分选区出来,不需要太过仔细 ...
- MySQL大小写敏感
MySQL大小写敏感说明 - TonyWu - 博客园https://www.cnblogs.com/wzmenjoy/p/4244545.html
- Android 1.7 中不支持 lambda 表达式
Error:(129, 32) 错误: -source 1.7 中不支持 lambda 表达式 (请使用 -source 8 或更高版本以启用 lambda 表达式) lambda expressio ...
- 【MySQL 读书笔记】当我们在执行更新语句的时候我们在做什么
该篇其实重点涉及两个日志的使用和处理. 一个是 server 层的 binlog 一个是服务器层的 redolog. 首先还是根据主线来介绍当我们在执行更新语句的时候我们在做什么 Redo Log M ...
- 一道B树的题目---先记一下, 还没看到B树
D