Skip to content

Export "Product Quality Cost Analysis" to Excel with Java (Styled & Formatted Reports)

JQuick-ExcelStyled & Formatted Reports

Styled & Formatted ReportsScene 0038

Tallies inspection results by product, computing Defect Rate = Defective Quantity / Inspected Quantity and Quality Cost = Defective Quantity × Rework Cost per Unit per row, with products exceeding the defect-rate limit marked in red as an alert and the last row giving the overall defect rate. Implemented declaratively in pure XML — no Java business code required.

Download the Excel fileView raw XML configBack to all demos

Product Quality Cost Analysis — Styled & Formatted Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Product Quality Cost Analysis" workbook: 1 data sheet(s); MAPPING over 7 columns (产品名称, 检验数量, 不良数量, 不良率, 单件返工成本, 质量成本, 控制状态), 16 formula(s), 1 style rule(s). On the Java side it builds 6 data rows and calls exportProductQualityCost() once — everything else is driven by the config.

It belongs to the "Styled & Formatted Reports" group: STYLE and conditional formatting: themed headers, zebra striping, highlighted total rows and threshold-based colouring of abnormal cells.

What you can copy from this page

  • The complete Java test class (63 lines, runnable as-is)
  • The complete XML config (36 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 styles, Java Excel header style zebra rows, Apache POI CellStyle export, Java Excel conditional formatting, export Product Quality Cost Analysis to Excel with Java

Output specs

Scene no.
0038
Group
Styled & Formatted Reports
Config lines
36
Source lines
63

Java source · Style0038ProductQualityCostDemo.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.style;

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 Style0038ProductQualityCostDemo {

    private static final String XML = "jquick/biz/style/0038_style_product-quality-cost.xml";

    /** 导出:纯 XML 完成不良率、质量成本、合计汇总与超标标红。 */
    @Test
    public void exportProductQualityCost() throws Exception {
        List<JQuickRow> rows = new ArrayList<>();
        rows.add(product("手机外壳", 5000, 120, 8.5, "受控"));
        rows.add(product("显示屏模组", 3000, 240, 25, "超标"));
        rows.add(product("锂电池", 8000, 96, 12, "受控"));
        rows.add(product("连接器", 6000, 180, 5.5, "受控"));
        rows.add(product("摄像头模组", 2500, 75, 18, "受控"));
        // 合计占位行:数量、整体不良率与质量成本留给 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);
        rows.add(total);

        File out = new File("D:/test/excel/biz/style/0038_style_product-quality-cost.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("espresso", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Style0038ProductQualityCostService service = factory.createApi(Style0038ProductQualityCostService.class);
            service.exportProductQualityCost("field", "value");
        }

    }

    /** 构造一个产品行(a=产品名称,b=检验数量,c=不良数量,e=单件返工成本,g=控制状态)。 */
    private static JQuickRow product(String name, double inspectQty, double defectQty, double reworkUnitCost, String status) {
        JQuickRow row = new JQuickRow();
        row.put("a", name);
        row.put("b", inspectQty);
        row.put("c", defectQty);
        row.put("d", null);
        row.put("e", reworkUnitCost);
        row.put("f", null);
        row.put("g", status);
        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("espresso", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Style0038ProductQualityCostService service = factory.createApi(Style0038ProductQualityCostService.class);

XML config · 0038_style_product-quality-cost.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.style.Style0038ProductQualityCostService">

    <!-- 导出:产品质量成本分析表 -->
    <excel name="exportProductQualityCost" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="产品质量成本分析", HEADER=true,
                MAPPING={"a":"产品名称","b":"检验数量","c":"不良数量","d":"不良率","e":"单件返工成本","f":"质量成本","g":"控制状态"},
                FORMAT={"b":"#,##0","c":"#,##0","d":"0.00%","e":"#,##0.00","f":"#,##0.00"},
                FORMULAS={
                    D2:'C2/B2',F2:'C2*E2',
                    D3:'C3/B3',F3:'C3*E3',
                    D4:'C4/B4',F4:'C4*E4',
                    D5:'C5/B5',F5:'C5*E5',
                    D6:'C6/B6',F6:'C6*E6',
                    B7:'SUM(B2:B6)',C7:'SUM(C2:C6)',D7:'C7/B7',F7:'SUM(F2:F6)'
                },
                STYLE={
                    ROW 7:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
                    D3:{color:red,bold:true}
                }
        ]]>
    </excel>

    <!-- 导入解析:读取导出的产品质量成本分析表 -->
    <excel name="importProductQualityCost" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="产品质量成本分析", HEADER=true,
                MAPPING={"产品名称":"product","检验数量":"inspectQty","不良数量":"defectQty","不良率":"defectRate","单件返工成本":"reworkUnitCost","质量成本":"qualityCost","控制状态":"controlStatus"}
        ]]>
    </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.