0
点赞
收藏
分享

微信扫一扫

C4认证Java基础Excel练习题

芝婵 2022-03-24 阅读 34

任务说明
请运用财务部门提供的数据(xx部-薪酬表.xlsx和员工五险一金申报表.xlsx),根据以下规则核算出四个部门的薪酬数据并将汇总数据提供到“企业人员月度工资成本支付表.xlsx“模版中。

核心业务:合并报表、计算个税

薪酬统计规则如下:

  1. 部门薪资明细中的底薪与岗位工资以及绩效、奖金、补助等各项正收益合并至“企业人员月度工资成本支付表.xlsx”的“工资”一栏。
  2. 部门薪资明细中的考勤扣除、违规处罚等负收益项全部合并至“企业人员月度工资成本支付表.xlsx”的“扣款”一栏。
  3. “工资-扣款”的计算结果放入“应发工资”一栏。
  4. “员工五险一金申报表.xlsx”中提供了员工申报五险一金的基数,根据每人的申报基数,可计算五险一金的具体缴纳数额,将计算结果填写至“企业人员月度工资成本支付表.xlsx“的五险一金的“个人缴纳部分”和“企业缴纳部分”对应栏,不缴纳的项目填写0。
    五险一金计算规则如下:
    养老保险:单位,20%,个人,8%
    医疗保险:单位,8%,个人,2%;
    失业保险:单位,2%,个人,1%;
    工伤保险:单位,0.5%,个人不用缴费;
    生育保险:单位,0.7%,个人不用缴费;
    住房公积金缴纳比例有 8%、10%、12%三档每人具体公积金缴纳比例数据也记录在“员工五险一金申报表.xlsx”中。
  5. “应发工资+企业缴纳五险一金”的结果填入“企业人员月度工资成本支付表.xlsx“的“企业支出成本”一栏。
  6. “应发工资-五险一金个人缴纳部分”的结果等于“应税金额”,根据应税金额计算个税金额填入“企业人员月度工资成本支付表.xlsx“的“个税金额”一栏,个税计算方式如下:
    不考虑个税起征点
    收入中不超过3000元的部分按3%税率缴纳个税。
    3000元-12000元的部分按10%税率缴纳个税。
    超过12000元不高于25000元的部分按税率20%计算。
    25000元-35000元的部分按税率25%计算
    35000元-55000元的部分按税率30%计算
    55000元-80000元的部分按税率35%计算
    超过80000元的部分按税率45%计算
  7. “应发工资-五险一金个人缴纳部分-个税”的结果填入“企业人员月度工资成本支付表.xlsx“的“实发工资”一栏。
  8. 将4个部门的全部员工数填充到“企业人员月度工资成本支付表.xlsx”并计算全月各部门的企业支出成本总和与四个部门企业支出成本总和。

相关表格附件
4个部门薪酬表如下:
|ID | 文件名称 | |:---- |:----- | |1 |市场部-薪酬表.xlsx | |2 |研发部-薪酬表.xlsx | |3 |销售部-薪酬表.xlsx | |4 |大客户部-薪酬表.xlsx |
员工五险一金申报表.xlsx 企业人员月度工资成本支付表.xlsx
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
共8个小问,把所有数据读取计算一次性写入表即可!
创建一个maven项目,添加poi性格依赖。

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.14.1</version>
        </dependency>
</dependencies>        

设计一个Department类,存储所有信息。代码如下:

/**
 * 部门
 */
public class Department {
    private String department;//部门
    private String jobNumber;//工号
    private String name;//姓名
    private double basicSalary;//底薪
    private double postWage;//岗位工资
    private double performanceBonus;//绩效奖金
    private double attendanceBonus;//全勤奖金
    private double attendanceDeduction;//考勤扣除
    private double penalties_for_non_compliance;//违规处罚
    private double transportationSubsidies;//交通补助
    private double communicationsGrants;//通信补助

    private double endowmentInsuranceSelf;//个人养老保险
    private double endowmentInsuranceCompany;//公司养老保险

    private double medicareSelf;//个人医疗保险
    private double medicareCompany;//公司医疗保险

    private double unemploymentInsuranceSelf;//个人失业保险
    private double unemploymentInsuranceConpany;//公司失业保险

    private double employmentInjuryInsuranceSelf;//个人工伤保险
    private double employmentInjuryInsuranceConpany;//公司工伤保险

    private double birthInsuranceSelf;//个人生育保险
    private double birthInsuranceConpany;//公司生育保险

    private double reservedFundsSelf;//个人公积金
    private double reservedFundsConpany;//公司公积金

    public double getEndowmentInsuranceSelf() {
        return endowmentInsuranceSelf;
    }

    public void setEndowmentInsuranceSelf(double endowmentInsuranceSelf) {
        this.endowmentInsuranceSelf = endowmentInsuranceSelf;
    }

    public double getEndowmentInsuranceCompany() {
        return endowmentInsuranceCompany;
    }

    public void setEndowmentInsuranceCompany(double endowmentInsuranceCompany) {
        this.endowmentInsuranceCompany = endowmentInsuranceCompany;
    }

    public double getMedicareSelf() {
        return medicareSelf;
    }

    public void setMedicareSelf(double medicareSelf) {
        this.medicareSelf = medicareSelf;
    }

    public double getMedicareCompany() {
        return medicareCompany;
    }

    public void setMedicareCompany(double medicareCompany) {
        this.medicareCompany = medicareCompany;
    }

    public double getUnemploymentInsuranceSelf() {
        return unemploymentInsuranceSelf;
    }

    public void setUnemploymentInsuranceSelf(double unemploymentInsuranceSelf) {
        this.unemploymentInsuranceSelf = unemploymentInsuranceSelf;
    }

    public double getUnemploymentInsuranceConpany() {
        return unemploymentInsuranceConpany;
    }

    public void setUnemploymentInsuranceConpany(double unemploymentInsuranceConpany) {
        this.unemploymentInsuranceConpany = unemploymentInsuranceConpany;
    }

    public double getEmploymentInjuryInsuranceSelf() {
        return employmentInjuryInsuranceSelf;
    }

    public void setEmploymentInjuryInsuranceSelf(double employmentInjuryInsuranceSelf) {
        this.employmentInjuryInsuranceSelf = employmentInjuryInsuranceSelf;
    }

    public double getEmploymentInjuryInsuranceConpany() {
        return employmentInjuryInsuranceConpany;
    }

    public void setEmploymentInjuryInsuranceConpany(double employmentInjuryInsuranceConpany) {
        this.employmentInjuryInsuranceConpany = employmentInjuryInsuranceConpany;
    }

    public double getBirthInsuranceSelf() {
        return birthInsuranceSelf;
    }

    public void setBirthInsuranceSelf(double birthInsuranceSelf) {
        this.birthInsuranceSelf = birthInsuranceSelf;
    }

    public double getBirthInsuranceConpany() {
        return birthInsuranceConpany;
    }

    public void setBirthInsuranceConpany(double birthInsuranceConpany) {
        this.birthInsuranceConpany = birthInsuranceConpany;
    }

    public double getReservedFundsSelf() {
        return reservedFundsSelf;
    }

    public void setReservedFundsSelf(double reservedFundsSelf) {
        this.reservedFundsSelf = reservedFundsSelf;
    }

    public double getReservedFundsConpany() {
        return reservedFundsConpany;
    }

    public void setReservedFundsConpany(double reservedFundsConpany) {
        this.reservedFundsConpany = reservedFundsConpany;
    }

    public Department(){

    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getJobNumber() {
        return jobNumber;
    }

    public void setJobNumber(String jobNumber) {
        this.jobNumber = jobNumber;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getBasicSalary() {
        return basicSalary;
    }

    public void setBasicSalary(double basicSalary) {
        this.basicSalary = basicSalary;
    }

    public double getPostWage() {
        return postWage;
    }

    public void setPostWage(double postWage) {
        this.postWage = postWage;
    }

    public double getPerformanceBonus() {
        return performanceBonus;
    }

    public void setPerformanceBonus(double performanceBonus) {
        this.performanceBonus = performanceBonus;
    }

    public double getAttendanceBonus() {
        return attendanceBonus;
    }

    public void setAttendanceBonus(double attendanceBonus) {
        this.attendanceBonus = attendanceBonus;
    }

    public double getAttendanceDeduction() {
        return attendanceDeduction;
    }

    public void setAttendanceDeduction(double attendanceDeduction) {
        this.attendanceDeduction = attendanceDeduction;
    }

    public double getPenalties_for_non_compliance() {
        return penalties_for_non_compliance;
    }

    public void setPenalties_for_non_compliance(double penalties_for_non_compliance) {
        this.penalties_for_non_compliance = penalties_for_non_compliance;
    }

    public double getTransportationSubsidies() {
        return transportationSubsidies;
    }

    public void setTransportationSubsidies(double transportationSubsidies) {
        this.transportationSubsidies = transportationSubsidies;
    }

    public double getCommunicationsGrants() {
        return communicationsGrants;
    }

    public void setCommunicationsGrants(double communicationsGrants) {
        this.communicationsGrants = communicationsGrants;
    }

    //得到正收益和
    public double getAdd(){
        return basicSalary+postWage+performanceBonus+attendanceBonus+transportationSubsidies+communicationsGrants;
    }
    考勤扣除、违规处罚等负收益项
    public double getMinusYield(){
        return attendanceDeduction+penalties_for_non_compliance;
    }

    //应发工资=工资-扣款
    public double getSalary(){
        return getAdd()-getMinusYield();
    }

    //个人缴纳部分
    public double getSelf(){
        return endowmentInsuranceSelf+medicareSelf+unemploymentInsuranceSelf+employmentInjuryInsuranceSelf+birthInsuranceSelf+reservedFundsSelf;
    }
    //企业缴纳部分
    public double getConpany(){
        return endowmentInsuranceCompany+medicareCompany+unemploymentInsuranceConpany+employmentInjuryInsuranceConpany+birthInsuranceConpany+reservedFundsConpany;
    }
    //应发工资+企业缴纳五险一金”的结果填入“企业人员月度工资成本支付表.xlsx“的“企业支出成本”一栏。
    public double getAllConpany(){
        return getSalary()+getConpany();
    }
    //应发工资-五险一金个人缴纳部分”的结果等于“应税金额” 个人税
    public double getTax(){
        double gz = getSalary()-getSelf();
        double s;
        if(gz<3000){
            s=gz*0.003;
        }else if(gz<=12000){
            s=gz*0.1;
        }else if(gz<=25000){
            s=gz*0.2;
        }else if(gz<=35000){
            s=gz*0.25;
        }else if(gz<=55000){
            s=gz*0.3;
        }else if(gz<=80000){
            s=gz*0.35;
        }else {
            s=gz*0.45;
        }
        return s;
    }

    //“应发工资-五险一金个人缴纳部分-个税”的结果填入“企业人员月度工资成本支付表.xlsx“的“实发工资”一栏。
    public double getNetPayroll(){
        return getSalary()-getSelf()-getTax();
    }

    @Override
    public String toString() {
        return "工号:"+jobNumber+
                " 姓名:"+name+
                " 部门:"+department+
                " 工资:"+getAdd()+
                " 扣款:"+getMinusYield()+
                "养老(个人):"+endowmentInsuranceSelf+
                "医疗(个人):"+medicareSelf+
                "失业(个人):"+unemploymentInsuranceSelf+
                "工伤(个人):"+employmentInjuryInsuranceSelf+
                "生育(个人):"+birthInsuranceSelf+
                "公积金(个人):"+reservedFundsSelf+
                "合计(个人):"+getSelf()+
                "养老(公司):"+endowmentInsuranceCompany+
                "医疗(公司):"+medicareCompany+
                "失业(公司):"+unemploymentInsuranceConpany+
                "工伤(公司):"+employmentInjuryInsuranceConpany+
                "生育(公司):"+birthInsuranceConpany+
                "公积金(公司):"+reservedFundsConpany+
                "合计(公司):"+getConpany()+
                "个税金额:"+getTax()+
                " 应发工资:"+getSalary()+
                "实发工资"+getNetPayroll()+
                "企业支出成本:"+getAllConpany();

    }

}

编写一个ExcelUtil 工具类,读入excel表信息和写入excel表。代码如下:

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.*;

public class ExcelUtil {
    /**
     * 获取各个部门的数据
     * @param departmentName 部门名
     * @param inputStream 输入流
     * @param map 存储数据的集合
     * @throws IOException
     */
    public void readExcel_Department(String departmentName, InputStream inputStream,Map<String,Department> map) throws IOException {
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);
        XSSFSheet sheetAt = xssfWorkbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheetAt.rowIterator();
        //排除掉表头
        rowIterator.hasNext();
        Row head = rowIterator.next();
        while (rowIterator.hasNext()){
            Row row = rowIterator.next();
            Department department = new Department();
            department.setDepartment(departmentName);
            department.setJobNumber(row.getCell(0).getStringCellValue());
            department.setName(row.getCell(1).getStringCellValue());
            department.setBasicSalary(row.getCell(2).getNumericCellValue());
            department.setPostWage(row.getCell(3).getNumericCellValue());
            department.setPerformanceBonus(row.getCell(4).getNumericCellValue());
            department.setAttendanceBonus(row.getCell(5).getNumericCellValue());
            department.setAttendanceDeduction(row.getCell(6).getNumericCellValue());
            department.setPenalties_for_non_compliance(row.getCell(7).getNumericCellValue());
            department.setTransportationSubsidies(row.getCell(8).getNumericCellValue());
            department.setCommunicationsGrants(row.getCell(9).getNumericCellValue());
            map.put(department.getJobNumber(),department);
        }
        xssfWorkbook.close();
    }

    /**
     * 读取五险一金的数据
     * @param inputStream 输入流
     * @param map 计算出五险一金的数据存储在map集合中
     * @throws IOException
     */
    public void readExcel_Department(InputStream inputStream,Map<String,Department> map) throws IOException {
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);
        XSSFSheet sheetAt = xssfWorkbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheetAt.rowIterator();
        //排除掉表头
        rowIterator.hasNext();
        Row head = rowIterator.next();
        while (rowIterator.hasNext()){
            Row row = rowIterator.next();
            int id = (int)row.getCell(0).getNumericCellValue();
            double num1 = row.getCell(2).getNumericCellValue(); //工资基数
            double num2 = row.getCell(3).getNumericCellValue(); //公积金比例

            Department department = map.get(""+id);
            department.setEndowmentInsuranceSelf(num1*0.08);
            department.setEndowmentInsuranceCompany(num1*0.2);

            department.setMedicareSelf(num1*0.02);
            department.setMedicareCompany(num1*0.08);

            department.setUnemploymentInsuranceSelf(num1*0.01);
            department.setUnemploymentInsuranceConpany(num1*0.02);

            department.setEmploymentInjuryInsuranceSelf(0);
            department.setEmploymentInjuryInsuranceConpany(num1*0.005);

            department.setBirthInsuranceSelf(num1*0.0);
            department.setBirthInsuranceConpany(num1*0.007);

            department.setReservedFundsSelf(num1*num2);
            department.setReservedFundsConpany(num1*num2);

            map.put(""+id,department);
        }
        xssfWorkbook.close();
    }

    /**
     * 存储数据
     * @param data 存储数据的集合
     * @param destFile 存储目标文件
     * @throws IOException
     */
    public void fillExcel_Department(Map<String,Department> data, File destFile) throws IOException {
        //创建一个工作簿
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        //在工作簿里创建一张表
        XSSFSheet sheet = xssfWorkbook.createSheet("用户");
        //
        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("工号");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("部门");
        row.createCell(3).setCellValue("工资");
        row.createCell(4).setCellValue("扣款");
        row.createCell(5).setCellValue("养老(个人)");
        row.createCell(6).setCellValue("医疗(个人)");
        row.createCell(7).setCellValue("失业(个人)");
        row.createCell(8).setCellValue("工伤(个人)");
        row.createCell(9).setCellValue("生育(个人)");
        row.createCell(10).setCellValue("公积金(个人)");
        row.createCell(11).setCellValue("合计(个人)");
        row.createCell(12).setCellValue("养老(公司)");
        row.createCell(13).setCellValue("医疗(公司)");
        row.createCell(14).setCellValue("失业(公司)");
        row.createCell(15).setCellValue("工伤(公司)");
        row.createCell(16).setCellValue("生育(公司)");
        row.createCell(17).setCellValue("公积金(公司)");
        row.createCell(18).setCellValue("合计(公司)");
        row.createCell(19).setCellValue("个税金额");
        row.createCell(20).setCellValue("应发工资");
        row.createCell(21).setCellValue("实发工资");
        row.createCell(22).setCellValue("企业支出成本");
        //行中添加数据
        Set<String> keys = data.keySet();
        int i=1;
        for(String str:keys){
            Department department = data.get(str);
            //正收益
            double add =department.getAdd();
            //考勤扣除、违规处罚等负收益项
            double decrease=department.getMinusYield();
            //应发工资=工资-扣款
            double wages = department.getSalary();
            XSSFRow r = sheet.createRow(i++);
            //工号
            r.createCell(0).setCellValue(department.getJobNumber());
            //姓名
            r.createCell(1).setCellValue(department.getName());
            //部门
            r.createCell(2).setCellValue(department.getDepartment());
            //工资
            r.createCell(3).setCellValue(department.getAdd());
            //扣款
            r.createCell(4).setCellValue(department.getMinusYield());
            //养老(个人)
            r.createCell(5).setCellValue(department.getEndowmentInsuranceSelf());
            //医疗(个人)
            r.createCell(6).setCellValue(department.getMedicareSelf());
            //失业(个人)
            r.createCell(7).setCellValue(department.getUnemploymentInsuranceSelf());
            //工伤(个人)
            r.createCell(8).setCellValue(department.getEmploymentInjuryInsuranceSelf());
            //生育(个人)
            r.createCell(9).setCellValue(department.getBirthInsuranceSelf());
            //公积金(个人)
            r.createCell(10).setCellValue(department.getReservedFundsSelf());
            //合计(个人)
            r.createCell(11).setCellValue(department.getSelf());
            //养老(公司)
            r.createCell(12).setCellValue(department.getEndowmentInsuranceCompany());
            //医疗(公司)
            r.createCell(13).setCellValue(department.getMedicareCompany());
            //失业(公司)
            r.createCell(14).setCellValue(department.getUnemploymentInsuranceConpany());
            //工伤(公司)
            r.createCell(15).setCellValue(department.getEmploymentInjuryInsuranceConpany());
            //生育(公司)
            r.createCell(16).setCellValue(department.getBirthInsuranceConpany());
            //公积金(公司)
            r.createCell(17).setCellValue(department.getReservedFundsConpany());
            //合计(公司)
            r.createCell(18).setCellValue(department.getConpany());
            //个税金额
            r.createCell(19).setCellValue(department.getTax());
            //应发工资
            r.createCell(20).setCellValue(department.getSalary());
            //实发工资
            r.createCell(21).setCellValue(department.getNetPayroll());
            //企业支出成本
            r.createCell(22).setCellValue(department.getAllConpany());
        }
        FileOutputStream fileOutputStream = new FileOutputStream(destFile);
        xssfWorkbook.write(fileOutputStream);
        fileOutputStream.close();
        xssfWorkbook.close();
    }
}

测试代码如下:

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) throws IOException {
        //存储各个部门的数据
        Map<String, Department> map = new TreeMap<String, Department>();
        ExcelUtil excelUtil = new ExcelUtil();
        //获取到研发部部门员工的数据
        InputStream resourceAsStream1 = excelUtil.getClass().getClassLoader().getResourceAsStream("研发部-薪酬表.xlsx");
        excelUtil.readExcel_Department("研发部", resourceAsStream1,map);
        //获取到大客户部员工的数据
        InputStream resourceAsStream2 = excelUtil.getClass().getClassLoader().getResourceAsStream("大客户部-薪酬表.xlsx");
        excelUtil.readExcel_Department("大客户部",resourceAsStream2,map);
        //获取到市场部部门员工的数据
        InputStream resourceAsStream3 = excelUtil.getClass().getClassLoader().getResourceAsStream("市场部-薪酬表.xlsx");
        excelUtil.readExcel_Department("市场部",resourceAsStream3,map);
        //获取到销售部部门员工的数据
        InputStream resourceAsStream4 = excelUtil.getClass().getClassLoader().getResourceAsStream("销售部-薪酬表.xlsx");
        excelUtil.readExcel_Department("销售部",resourceAsStream4,map);

        //读取员工五险一金申报表
        InputStream resourceAsStream5 = excelUtil.getClass().getClassLoader().getResourceAsStream("员工五险一金申报表.xlsx");
        excelUtil.readExcel_Department(resourceAsStream5,map);

        String str = "src\\main\\resources\\企业员工月度工资成本支付表.xlsx";
        File file = new File(str);
        //把数据写入到表中
        excelUtil.fillExcel_Department(map,file);

    }
}

最终效果如下图
在这里插入图片描述

举报

相关推荐

0 条评论