Skip to content

Export "Livestream Selling Detail" to Excel with Java (Formula & Calculated Reports)

JQuick-ExcelFormula & Calculated Reports

Formula & Calculated ReportsScene 0062

Registers the products featured per livestream session, computing Sales Amount = Sales Quantity × Unit Price and Commission = Sales Amount × Commission Rate per row, with the last row summarizing sales amount and commission. Implemented declaratively in pure XML — no Java business code required.

Download the Excel fileView raw XML configBack to all demos

Livestream Selling Detail — Formula & Calculated Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Livestream Selling Detail" workbook: 1 data sheet(s); MAPPING over 8 columns (场次, 主播, 商品名称, 销售数量, 单价, 销售额, 佣金比例, 佣金), 12 formula(s), 1 style rule(s). On the Java side it builds 6 data rows and calls exportLiveStreamSales() 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 (35 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 Livestream Selling Detail to Excel with Java

Output specs

Scene no.
0062
Group
Formula & Calculated Reports
Config lines
35
Source lines
67

Java source · Formulas0062LiveStreamSalesDemo.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 Formulas0062LiveStreamSalesDemo {

    private static final String XML = "jquick/biz/formulas/0062_formulas_live-stream-sales.xml";

    /** 导出:纯 XML 完成逐行销售额 / 佣金与合计行高亮。 */
    @Test
    public void exportLiveStreamSales() throws Exception {
        List<JQuickRow> rows = new ArrayList<>();
        // f=销售额、h=佣金 两列留空,由 FORMULAS 计算
        rows.add(line("S2601", "张伟", "智能手环", 1200, 199.00, 0.15));
        rows.add(line("S2601", "张伟", "无线耳机", 800, 299.00, 0.15));
        rows.add(line("S2602", "李娜", "扫地机器人", 300, 1299.00, 0.20));
        rows.add(line("S2603", "王强", "空气炸锅", 1500, 399.00, 0.12));
        rows.add(line("S2603", "王强", "破壁机", 600, 499.00, 0.12));
        // 合计占位行:销售额与佣金合计留给 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/0062_formulas_live-stream-sales.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("denim", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Formulas0062LiveStreamSalesService service = factory.createApi(Formulas0062LiveStreamSalesService.class);
            service.exportLiveStreamSales("field", "value");
        }

    }

    /** 构造一条带货明细(a=场次,b=主播,c=商品,d=数量,e=单价,f=销售额,g=佣金比例,h=佣金)。 */
    private static JQuickRow line(String session, String anchor, String productName,
                                  int quantity, double unitPrice, double commissionRate) {
        JQuickRow row = new JQuickRow();
        row.put("a", session);
        row.put("b", anchor);
        row.put("c", productName);
        row.put("d", quantity);
        row.put("e", unitPrice);
        row.put("f", null);
        row.put("g", commissionRate);
        row.put("h", 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("denim", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Formulas0062LiveStreamSalesService service = factory.createApi(Formulas0062LiveStreamSalesService.class);

XML config · 0062_formulas_live-stream-sales.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.Formulas0062LiveStreamSalesService">

    <!-- 导出:直播带货销售明细 -->
    <excel name="exportLiveStreamSales" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="直播带货销售明细", HEADER=true,
                MAPPING={"a":"场次","b":"主播","c":"商品名称","d":"销售数量","e":"单价","f":"销售额","g":"佣金比例","h":"佣金"},
                FORMAT={"d":"#,##0","e":"#,##0.00","f":"#,##0.00","g":"0.00%","h":"#,##0.00"},
                FORMULAS={
                    F2:'D2*E2',H2:'F2*G2',
                    F3:'D3*E3',H3:'F3*G3',
                    F4:'D4*E4',H4:'F4*G4',
                    F5:'D5*E5',H5:'F5*G5',
                    F6:'D6*E6',H6:'F6*G6',
                    F7:'SUM(F2:F6)',H7:'SUM(H2:H6)'
                },
                STYLE={
                    ROW 7:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center}
                }
        ]]>
    </excel>

    <!-- 导入解析:读取导出的直播带货销售明细 -->
    <excel name="importLiveStreamSales" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="直播带货销售明细", HEADER=true,
                MAPPING={"场次":"session","主播":"anchor","商品名称":"productName","销售数量":"quantity","单价":"unitPrice","销售额":"salesAmount","佣金比例":"commissionRate","佣金":"commission"}
        ]]>
    </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.