POI-Ecel模板導出

/**
     * 
     * @param excelPath    excel模板路徑
     * @param downloadPath    文件下載路徑
     * @param list    報表數據
     * @param response
     * @return
     * @throws Exception
     */
    @SuppressWarnings("resource")
    public AjaxResult excel(String excelPath, String downloadPath, List<QuestionReport> list, HttpServletResponse response) throws Exception{
        OutputStream out = null;
        //獲取excel模板路徑
        File fi = new File(excelPath);
        //獲取文件名稱
        String fileName = fi.getName(); 
        //讀取excel模板  
        InputStream inp = new FileInputStream(fi);
        //創建xlsx-excel文件
        XSSFWorkbook wb = new XSSFWorkbook(inp);
        //獲取第一個sheet
        XSSFSheet sheet = wb.getSheetAt(0);
        //填充數據
        for (int i = 0; i < list.size(); i++) {
            //從第三行開始創建
            XSSFRow row = sheet.createRow(2 + i);
            QuestionReport questionReport = list.get(i);
            //填充第三行第一個數據
            row.createCell(0).setCellValue(1 + i);
            row.createCell(1).setCellValue(questionReport.getProjectCode());
            row.createCell(2).setCellValue(questionReport.getProjectName());
            row.createCell(3).setCellValue(questionReport.getProjectDept());
            row.createCell(4).setCellValue(questionReport.getBuildQualityHead());
            row.createCell(5).setCellValue(questionReport.getProjectBuildingNos());
            row.createCell(6).setCellValue(questionReport.getZlQuestion());
            row.createCell(7).setCellValue(questionReport.getZlYZG());
            row.createCell(8).setCellValue(questionReport.getZlDzg());
            row.createCell(9).setCellValue(questionReport.getAqQuestion());
            row.createCell(10).setCellValue(questionReport.getAqYZG());
            row.createCell(11).setCellValue(questionReport.getAqDzg());
            row.createCell(12).setCellValue(questionReport.getCbQuestion());
            row.createCell(13).setCellValue(questionReport.getCbYZG());
            row.createCell(14).setCellValue(questionReport.getCbDzg());
            row.createCell(15).setCellValue(questionReport.getCbQuestion());
            row.createCell(16).setCellValue(questionReport.getCbYZG());
            row.createCell(17).setCellValue(questionReport.getCbDzg());
        }
        
        //修改模板內容導出新模板  
        fileName ="報事工單報表" + ".xlsx";
        out = new FileOutputStream(downloadPath + fileName);  
        wb.write(out);  
        out.close();
        out.flush();
        return AjaxResult.success(fileName);
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章