1. 程式人生 > >java 導出

java 導出

reat ams 模板 res onclick string tor gin sub

按鈕

   <a href="###" class="eui-btn eui-btn-small" onclick="Export()"><i class="eui-icon" >&#xe7fb;</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(); } } }

java 導出