根据模板生成带公式的excel

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013447988/article/details/84098809

将模板(带公式计算)放在项目路径下某个文件夹

//1.生成工资表excel文件
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(request.getServletContext().getRealPath("static/template/工资条模板.xlsx"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new MyException("找不到工资条模板.xlsx文件");
        }
        XSSFWorkbook excel = null;
        try {
            excel = new XSSFWorkbook(fileInputStream);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException("读取文件出错");
        }
        XSSFSheet sheet =  excel.getSheetAt(0);
        //获取模板中的标题
        String title = sheet.getRow(0).getCell(0).getStringCellValue();
        //获取模板中的列名
        String[] rowValue1 = new String[18];
        String[] rowValue2 = new String[3];
        for (int i = 0; i< sheet.getRow(1).getPhysicalNumberOfCells() ; i++) {
            if(13 == i && 14 == i) {
                continue;
            }
            rowValue1[i] = sheet.getRow(1).getCell(i).getStringCellValue();
        }

        rowValue2[0] = sheet.getRow(2).getCell(12 ).getStringCellValue();
        rowValue2[1] = sheet.getRow(2).getCell(13 ).getStringCellValue();
        rowValue2[2] = sheet.getRow(2).getCell(14 ).getStringCellValue();

        String webAppPath = request.getSession().getServletContext().getRealPath("/");
        String uploadfilePath = webAppPath+"\\static\\temp";
        File uploadfile = new File(uploadfilePath);
        //判断文件夹是否存在,如果不存在则创建文件夹
        if (!uploadfile.exists()) {//先创建temp文件夹
            uploadfile.mkdir();
        }

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(uploadfilePath + "\\" +  p.getPersonName() + "工资表.xlsx");
            //创建excel
            Workbook workbook = new XSSFWorkbook();
            //读取个人工资条模板文件,将模板复制到新建excel文件
            String filePath = webAppPath + "\\static\\template\\个人工资条模板.xlsx";
            FileInputStream tps = new FileInputStream(new File(filePath));
            XSSFWorkbook tpWorkbook = new XSSFWorkbook(tps);
            workbook = tpWorkbook;
            //创建sheet--子工作表
            Sheet sheetTemp = workbook.getSheetAt(0);
            //设置公式自动计算
            sheetTemp.setForceFormulaRecalculation(true);
            //设置标题
            sheetTemp.getRow(0).getCell(0).setCellValue(title);
            //设置列名
            for (int i = 0; i< 18 ; i++) {
                if(13 == i && 14 == i) {
                    continue;
                }
                sheetTemp.getRow(1).getCell(i).setCellValue(rowValue1[i]);
            }
            sheetTemp.getRow(2).getCell(12).setCellValue(rowValue2[0]);
            sheetTemp.getRow(2).getCell(13).setCellValue(rowValue2[1]);
            sheetTemp.getRow(2).getCell(14).setCellValue(rowValue2[2]);
            //设置个人工资明细
            sheetTemp.getRow(3).getCell(0).setCellValue(1);
            sheetTemp.getRow(3).getCell(1).setCellValue(salarySchedule.getStaffName());
            sheetTemp.getRow(3).getCell(2).setCellValue(salarySchedule.getFinanAccountUnit());
            sheetTemp.getRow(3).getCell(3).setCellValue(salarySchedule.getBasePay());
            sheetTemp.getRow(3).getCell(4).setCellValue(salarySchedule.getPostWage());
            sheetTemp.getRow(3).getCell(5).setCellValue(salarySchedule.getAchievementBonus());
            sheetTemp.getRow(3).getCell(6).setCellValue(salarySchedule.getMealSupplement());
            sheetTemp.getRow(3).getCell(7).setCellValue(salarySchedule.getOtherWelfareCosts());
            sheetTemp.getRow(3).getCell(8).setCellValue(salarySchedule.getPerformanceDeductions());
            sheetTemp.getRow(3).getCell(9).setCellValue(salarySchedule.getDeductionsAttend());
            sheetTemp.getRow(3).getCell(11).setCellValue(salarySchedule.getHousingStock());
            sheetTemp.getRow(3).getCell(12).setCellValue(salarySchedule.getPension());
            sheetTemp.getRow(3).getCell(13).setCellValue(salarySchedule.getMedicalCare());
            sheetTemp.getRow(3).getCell(14).setCellValue(salarySchedule.getUnemployment());

            workbook.write(fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

猜你喜欢

转载自blog.csdn.net/u013447988/article/details/84098809