按钮

   <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();
}
} }

最新文章

  1. 跟我一起云计算(5)——Shards
  2. python内置函数
  3. 使用ASP.NET MVC局部视图避免JS拼接HTML,编写易于维护的HTML页面
  4. 《深入浅出WPF》笔记三
  5. Linux安装配置JDK
  6. git 查看某个文件的历史修改版本
  7. 转:H2 入门
  8. ZZCMS8.1|代码审计
  9. absolute和relative元素 设置百分比宽高的差异
  10. bootstrap datepicker 属性设置 以及方法和事件
  11. c语言提高篇 第一天
  12. jquery on()方法重复绑定解决方法
  13. future then
  14. Ping 笔记
  15. SQL优化笔记一:索引和explain
  16. Hadoop之MapReduce思维导图
  17. vue里的渲染以及computed的好处
  18. Spring中实现多数据源事务管理
  19. 用函数打印Hello js
  20. Java中包的介绍

热门文章

  1. MATLAB中“repmat”与“cat”函数的用法
  2. MYSQL内置MYSQL数据库中你可以得到的信息
  3. [LeetCode] 18. 四数之和
  4. 记录使用nodejs时,未正确使用import导致的错误
  5. VMware Workstation 14安装VMware Tools
  6. centos6.8 安装jenkins
  7. 使用C++进行WMI查询的简单封装
  8. Binding介绍
  9. Linux(Ubuntu)使用日记------trash-cli防止误删文件
  10. delphi7 编译的程序在win7下请求获得管理员权限的方法