Skip to content

Export "Monthly Employee Attendance Summary" to Excel with Java (Formula & Calculated Reports)

JQuick-ExcelFormula & Calculated Reports

Formula & Calculated ReportsScene 0033

Tallies employee attendance by month, computing Leave Days = Expected Attendance - Actual Attendance and Attendance Rate = Actual Attendance / Expected Attendance per row, with the last row summarizing expected attendance, actual attendance, leave days and overtime hours and giving the overall attendance rate. Implemented declaratively in pure XML — no Java business code required.

Download the Excel fileView raw XML configBack to all demos

Monthly Employee Attendance Summary — Formula & Calculated Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Monthly Employee Attendance Summary" workbook: 1 data sheet(s); MAPPING over 8 columns (工号, 姓名, 部门, 应出勤天数, 实出勤天数, 请假天数, 出勤率, 加班时数), 18 formula(s), 1 style rule(s). On the Java side it builds 6 data rows and calls exportAttendanceMonthly() once — everything else is driven by the config.

It belongs to the "Formula & Calculated Reports" group: FORMULAS — inline expressions and aggregate functions such as Quantity × Unit Price or Balance = Receivable − Received, with SUM totals on the last row.

What you can copy from this page

  • The complete Java test class (65 lines, runnable as-is)
  • The complete XML config (33 lines: mapping, formulas, styles, charts and every other rule)
  • The exported xlsx file (real output, comparable with the preview above)

Related searches: Java export Excel formulas, Java Excel SUM formula export, Apache POI write formula example, Java computed column export, export Monthly Employee Attendance Summary to Excel with Java

Output specs

Scene no.
0033
Group
Formula & Calculated Reports
Config lines
33
Source lines
65

Java source · Formulas0033AttendanceMonthlyStatDemo.java

This is the complete test class that produced the Excel file above — copy it and run as-is. Java only builds the data rows; all statistics and styling live in the XML config.

package com.github.paohaijiao.biz.formulas;

import com.github.paohaijiao.statement.JQuickRow;
import com.github.paohaijiao.xml.ex.JQuickExcelExportXmlParseFactory;
import com.github.paohaijiao.xml.factory.JQuickFactory;
import com.github.paohaijiao.xml.factory.JQuickXmlFactory;
import com.github.paohaijiao.xml.handler.JQuickParseHandler;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class Formulas0033AttendanceMonthlyStatDemo {

    private static final String XML = "jquick/biz/formulas/0033_formulas_attendance-monthly-stat.xml";

    /** 导出:纯 XML 完成请假天数、出勤率与合计行汇总。 */
    @Test
    public void exportAttendanceMonthly() throws Exception {
        List<JQuickRow> rows = new ArrayList<>();
        rows.add(record("E1001", "张伟", "研发部", 22, 22, 12));
        rows.add(record("E1002", "李娜", "市场部", 22, 21, 6));
        rows.add(record("E1003", "王强", "财务部", 22, 20, 0));
        rows.add(record("E1004", "赵敏", "研发部", 22, 22, 20));
        rows.add(record("E1005", "陈刚", "生产部", 22, 19, 8));
        // 合计占位行:请假天数、出勤率留给 FORMULAS
        JQuickRow total = new JQuickRow();
        total.put("a", "合计");
        total.put("b", null);
        total.put("c", null);
        total.put("d", null);
        total.put("e", null);
        total.put("f", null);
        total.put("g", null);
        total.put("h", null);
        rows.add(total);

        File out = new File("D:/test/excel/biz/formulas/0033_formulas_attendance-monthly-stat.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("vintageSepia", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Formulas0033AttendanceMonthlyStatService service = factory.createApi(Formulas0033AttendanceMonthlyStatService.class);
            service.exportAttendanceMonthly("field", "value");
        }

    }

    /** 构造一条考勤记录(a=工号,b=姓名,c=部门,d=应出勤,e=实出勤,h=加班时数)。 */
    private static JQuickRow record(String empNo, String name, String dept, int shouldDays, int actualDays, int overtime) {
        JQuickRow row = new JQuickRow();
        row.put("a", empNo);
        row.put("b", name);
        row.put("c", dept);
        row.put("d", shouldDays);
        row.put("e", actualDays);
        row.put("f", null);
        row.put("g", null);
        row.put("h", overtime);
        return row;
    }
}

Minimal invocation (4 lines)

The core is these four lines: hand the rows to the parser, the parser to the factory, get a typed Service and call it. Copy them straight into your project.

// rows 由业务代码构造(List<JQuickRow>),os 为输出流,见下方完整源码
JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("vintageSepia", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Formulas0033AttendanceMonthlyStatService service = factory.createApi(Formulas0033AttendanceMonthlyStatService.class);

XML config · 0033_formulas_attendance-monthly-stat.xml

Formats (FORMAT), formulas (FORMULAS) and styles (STYLE) are all declared in this XML, fully decoupled from the Java code. The full config is below — copy it as-is.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE excels PUBLIC "-//PAOHAIJIAO//DTD API EXCEL 1.0//EN"
        "classpath:paohaijiao/dtd/Jquick-excel.dtd">
<excels namespace="com.github.paohaijiao.biz.formulas.Formulas0033AttendanceMonthlyStatService">

    <!-- 导出:员工月度考勤统计表 -->
    <excel name="exportAttendanceMonthly" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="员工月度考勤统计表", HEADER=true,
                MAPPING={"a":"工号","b":"姓名","c":"部门","d":"应出勤天数","e":"实出勤天数","f":"请假天数","g":"出勤率","h":"加班时数"},
                FORMAT={"d":"0","e":"0","f":"0","g":"0.00%","h":"0"},
                FORMULAS={
                    F2:'D2-E2',G2:'E2/D2',
                    F3:'D3-E3',G3:'E3/D3',
                    F4:'D4-E4',G4:'E4/D4',
                    F5:'D5-E5',G5:'E5/D5',
                    F6:'D6-E6',G6:'E6/D6',
                    D7:'SUM(D2:D6)',E7:'SUM(E2:E6)',F7:'SUM(F2:F6)',G7:'E7/D7',H7:'SUM(H2:H6)'
                },
                STYLE={ROW 7:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center}}
        ]]>
    </excel>

    <!-- 导入解析:读取导出的员工月度考勤统计表 -->
    <excel name="importAttendanceMonthly" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="员工月度考勤统计表", HEADER=true,
                MAPPING={"工号":"empNo","姓名":"name","部门":"dept","应出勤天数":"shouldDays","实出勤天数":"actualDays","请假天数":"leaveDays","出勤率":"attendanceRate","加班时数":"overtimeHours"}
        ]]>
    </excel>

</excels>

More demos in this group

The Excel file, preview image, XML config and source code on this page were all produced by a real JQuick-Excel run and are free to copy into your own project.