Skip to content

Export "Sales Daily / Weekly / Monthly Report" to Excel with Java (Formula & Calculated Reports)

JQuick-ExcelFormula & Calculated Reports

Formula & Calculated ReportsScene 0001

Daily sales detail, computing Amount = Quantity × Unit Price per row with a total row at the end. Implemented declaratively in pure XML — no Java business code required.

Download the Excel fileView raw XML configBack to all demos

Sales Daily / Weekly / Monthly Report — Formula & Calculated Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Sales Daily / Weekly / Monthly Report" workbook: 1 data sheet(s); MAPPING over 5 columns (日期, 商品, 数量, 单价, 金额), 6 formula(s), 1 style rule(s). On the Java side it builds 4 data rows and calls exportSalesDailyReport() 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 (67 lines, runnable as-is)
  • The complete XML config (26 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 Sales Daily / Weekly / Monthly Report to Excel with Java

Output specs

Scene no.
0001
Group
Formula & Calculated Reports
Config lines
26
Source lines
67

Java source · Formulas0001SalesDailyReportDemo.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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class Formulas0001SalesDailyReportDemo {

    private static final String XML = "jquick/biz/formulas/0001_formulas_sales-daily-report.xml";

    /** 导出:写入 3 条销售明细 + 1 行合计,回读校验公式与合计行样式。 */
    @Test
    public void exportSalesDailyReport() throws Exception {
        List<JQuickRow> rows = new ArrayList<>();
        rows.add(detail("2026-10-01", "无线鼠标", 100, 25.50));
        rows.add(detail("2026-10-01", "机械键盘", 80, 30.00));
        rows.add(detail("2026-10-01", "显示器", 50, 12.50));
        rows.add(totalRow());

        File out = new File("D:/test/excel/biz/formulas/0001_formulas_sales-daily-report.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("mintFresh", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Formulas0001SalesDailyReportService service = factory.createApi(Formulas0001SalesDailyReportService.class);
            service.exportSalesDailyReport("field", "value");
        }

    }

    /** 构造一条销售明细(a=日期,b=商品,c=数量,d=单价;e=金额由 FORMULAS 计算,占位保列)。 */
    private static JQuickRow detail(String date, String product, int qty, double price) throws Exception {
        JQuickRow row = new JQuickRow();
        row.put("a", new SimpleDateFormat("yyyy-MM-dd").parse(date));
        row.put("b", product);
        row.put("c", qty);
        row.put("d", price);
        row.put("e", null);
        return row;
    }

    /**
     * 合计行:只写「合计」文字,其余列以空值占位。
     *
     * <p>必须补齐全部映射列,否则框架写行时只会创建已存在的列,
     * 造成样式/公式定位时单元格空洞(JRowStyleStrategy 遍历时空指针)。
     */
    private static JQuickRow totalRow() {
        JQuickRow row = new JQuickRow();
        row.put("a", "合计");
        row.put("b", null);
        row.put("c", null);
        row.put("d", null);
        row.put("e", null);
        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("mintFresh", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Formulas0001SalesDailyReportService service = factory.createApi(Formulas0001SalesDailyReportService.class);

XML config · 0001_formulas_sales-daily-report.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.Formulas0001SalesDailyReportService">

    <!-- 导出:销售日报 -->
    <excel name="exportSalesDailyReport" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="销售日报", HEADER=true,
                MAPPING={"a":"日期","b":"商品","c":"数量","d":"单价","e":"金额"},
                FORMAT={"a":"yyyy-MM-dd","c":"0","d":"#,##0.00","e":"#,##0.00"},
                FORMULAS={E2:'C2*D2',E3:'C3*D3',E4:'C4*D4',C5:'SUM(C2:C4)',E5:'SUM(E2:E4)'},
                STYLE={ROW 5:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center}}
        ]]>
    </excel>

    <!-- 导入解析:读取导出的 Excel,按表头映射回字段 -->
    <excel name="importSalesDailyReport" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="销售日报", HEADER=true,
                MAPPING={"日期":"date","商品":"product","数量":"qty","单价":"price","金额":"amount"}
        ]]>
    </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.