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(10); sheet.setDefaultRowHeightInPoints(20); //创建一行 HSSFRow rowTitle = sheet.createRow(0); rowTitle.setHeightInPoints(20); 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(0); // 列标题及样式 cellTitle.setCellValue("科目ID"); cellTitle.setCellStyle(styleTitle); // 在行上创建2列 cellTitle = rowTitle.createCell(1); cellTitle.setCellValue("科目名称"); cellTitle.setCellStyle(styleTitle); cellTitle = rowTitle.createCell(2); cellTitle.setCellValue("上年实际"); cellTitle.setCellStyle(styleTitle); cellTitle = rowTitle.createCell(3); cellTitle.setCellValue("本年预算"); cellTitle.setCellStyle(styleTitle); cellTitle = rowTitle.createCell(4); cellTitle.setCellValue("1月"); cellTitle.setCellStyle(styleTitle); cellTitle = rowTitle.createCell(5); cellTitle.setCellValue("2月"); cellTitle.setCellStyle(styleTitle); cellTitle = rowTitle.createCell(6); cellTitle.setCellValue("3月"); cellTitle.setCellStyle(styleTitle); cellTitle = rowTitle.createCell(7); cellTitle.setCellValue("4月"); cellTitle.setCellStyle(styleTitle); cellTitle = rowTitle.createCell(8); cellTitle.setCellValue("5月"); cellTitle.setCellStyle(styleTitle); cellTitle = rowTitle.createCell(9); cellTitle.setCellValue("6月"); cellTitle.setCellStyle(styleTitle); cellTitle = rowTitle.createCell(10); cellTitle.setCellValue("7月"); cellTitle.setCellStyle(styleTitle); cellTitle = rowTitle.createCell(11); cellTitle.setCellValue("8月"); cellTitle.setCellStyle(styleTitle); cellTitle = rowTitle.createCell(12); cellTitle.setCellValue("9月"); cellTitle.setCellStyle(styleTitle); cellTitle = rowTitle.createCell(13); cellTitle.setCellValue("10月"); cellTitle.setCellStyle(styleTitle); cellTitle = rowTitle.createCell(14); cellTitle.setCellValue("11月"); cellTitle.setCellStyle(styleTitle); cellTitle = rowTitle.createCell(15); cellTitle.setCellValue("12月"); cellTitle.setCellStyle(styleTitle); List<BudgetDetail> budgetDetails=budgetDetailService.ExcelInfo(); for(int i=0;i<budgetDetails.size();i++){ BudgetDetail budgetDetail = budgetDetails.get(i); HSSFRow row = sheet.createRow(i + 1); row.setHeightInPoints(20); HSSFCell cell = row.createCell(0); cell.setCellValue(budgetDetail.getSubid());//科目ID cell.setCellStyle(styleCenter); cell = row.createCell(1); 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[1024]; int len = 0; while (-1 != (len=in.read(data, 0, data.length))) { out.write(data, 0, 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(); } } }