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[]{"期间","科目代码" ...
随机推荐
- SqlServer无备份下误删数据恢复
系统已上线,给客户修改bug的时候,使用delete语句删表数据,没想到库没切换成测试库.误删了正式库的数据,而且一次备份都没有做过,玩大了 不扯了,进入主题 网上很多方法,都是针对至少有一次备份的情 ...
- win10怎么进入和退出安全模式?
在Win10系统里同时按下”Win+R“组合按键,在打开的运行对话框里输入命令:msconfig,然后点击确定,如下图所示. 点击打开系统配置窗口,选择引导选项卡,如下图所示. 在引导选项卡窗口下,将 ...
- Linux学习之路(三)Shell脚本初探
本文参考链接:http://www.runoob.com/linux/linux-shell.html 基本说明 Shell脚本(shell script)是一种为shell编写的脚本程序.其中she ...
- 记录Nginx代理的配置
server { listen 80; server_name localhost; location / { root /ect/share/nginx/html; index inde ...
- 关于Http
摘自:菜鸟教程 HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(www)服务器传输超文本到本地浏览器的传送协议. HTTP ...
- Fiddler分享
链接:https://pan.baidu.com/s/162YmGb7-aUZ6xDf8eRfgpw 密码:j6er
- Vue组件之全局组件与局部组件
1全局注册实例 <div id="app"> <com-btn></com-btn> <com-btn></com-btn&g ...
- yaml的用法
yaml是用来读配置文件的. 一般用yaml或者yml结尾创建文件,格式:key: value.然后在用的时候,像打开文件一样读,返回数据可直接转为字典 使用的时候必须先安装模块并导入.安装:pip ...
- 随心测试_数据库_001<论数据的重要性>
测试工作中,数据的重要性 软测工程师:作为综合运用多学科知识,保障软件质量的重要岗位.需要我们学以致用,在工作中不断学习提升.以下:软测人员必备_数据库核心技能学习点,供大家学习参考. Q1:什么是: ...
- jackson把json转换成LIst
把json数据转换成list ObjectMapper mapper = new ObjectMapper(); List<YeWuLuYou> readValue = mapper.r ...