Skip to content

Export "Quality Inspection Defect Record" to Excel with Java (Styled & Formatted Reports)

JQuick-ExcelStyled & Formatted Reports

Styled & Formatted ReportsScene 0026

The QC department registers sampled quantity and defect quantity by batch, computing Defect Rate = Defect Quantity / Sampled Quantity per row, with the last row summarizing sampled total, defect total and overall defect rate; cells of batches exceeding the defect-rate limit are marked in red to flag a nonconforming verdict. Implemented declaratively in pure XML — no Java business code required.

Download the Excel fileView raw XML configBack to all demos

Quality Inspection Defect Record — Styled & Formatted Reports
Full screenshot of the exported xlsx

About this demo

This demo exports the "Quality Inspection Defect Record" workbook: 1 data sheet(s); MAPPING over 6 columns (批次号, 产品名称, 抽检数量, 缺陷数量, 缺陷率, 判定), 10 formula(s), 1 style rule(s). On the Java side it builds 6 data rows and calls exportQualityInspection() 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 (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 styles, Java Excel header style zebra rows, Apache POI CellStyle export, Java Excel conditional formatting, export Quality Inspection Defect Record to Excel with Java

Output specs

Scene no.
0026
Group
Styled & Formatted Reports
Config lines
33
Source lines
63

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

    private static final String XML = "jquick/biz/style/0026_style_quality-inspection-record.xml";

    /** 导出:纯 XML 完成缺陷率、合计行汇总与超标标红。 */
    @Test
    public void exportQualityInspection() throws Exception {
        List<JQuickRow> rows = new ArrayList<>();
        rows.add(batch("PC2026001", "A型电机", 1000, 8, "合格"));
        rows.add(batch("PC2026002", "B型水泵", 800, 12, "合格"));
        // 第 4 行:C 型阀门缺陷率 3.60% 超标,模板中 E4 标红
        rows.add(batch("PC2026003", "C型阀门", 500, 18, "不合格"));
        rows.add(batch("PC2026004", "D型轴承", 1200, 6, "合格"));
        // 第 6 行:E 型齿轮缺陷率 4.17% 超标,模板中 E6 标红
        rows.add(batch("PC2026005", "E型齿轮", 600, 25, "不合格"));
        // 合计占位行:缺陷率留给 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);
        rows.add(total);

        File out = new File("D:/test/excel/biz/style/0026_style_quality-inspection-record.xlsx");
        try (OutputStream os = new FileOutputStream(out)) {
            JQuickParseHandler parser = new JQuickExcelExportXmlParseFactory("cyan", rows, os);
            JQuickFactory factory = new JQuickXmlFactory(parser, XML);
            Style0026QualityInspectionRecordService service = factory.createApi(Style0026QualityInspectionRecordService.class);
            service.exportQualityInspection("field", "value");
        }

    }

    /** 构造一条质检批次(a=批次号,b=产品,c=抽检数量,d=缺陷数量,f=判定)。 */
    private static JQuickRow batch(String batchNo, String product, int inspectQty, int defectQty, String verdict) {
        JQuickRow row = new JQuickRow();
        row.put("a", batchNo);
        row.put("b", product);
        row.put("c", inspectQty);
        row.put("d", defectQty);
        row.put("e", null);
        row.put("f", verdict);
        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("cyan", rows, os);
JQuickFactory factory = new JQuickXmlFactory(parser, XML);
Style0026QualityInspectionRecordService service = factory.createApi(Style0026QualityInspectionRecordService.class);

XML config · 0026_style_quality-inspection-record.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.Style0026QualityInspectionRecordService">

    <!-- 导出:质量检验缺陷记录表 -->
    <excel name="exportQualityInspection" returnClass="void">
        <![CDATA[
            EXPORT WITH SHEET="质量检验缺陷记录", HEADER=true,
                MAPPING={"a":"批次号","b":"产品名称","c":"抽检数量","d":"缺陷数量","e":"缺陷率","f":"判定"},
                FORMAT={"c":"0","d":"0","e":"0.00%"},
                FORMULAS={
                    E2:'D2/C2',E3:'D3/C3',E4:'D4/C4',E5:'D5/C5',E6:'D6/C6',
                    C7:'SUM(C2:C6)',D7:'SUM(D2:D6)',E7:'D7/C7'
                },
                STYLE={
                    ROW 7:{bold:true,fillPattern:solid_foreground,fillForegroundColor:lightYellow,alignment:right,verticalAlignment:center},
                    E4:{color:red,bold:true},
                    E6:{color:red,bold:true}
                }
        ]]>
    </excel>

    <!-- 导入解析:读取导出的质量检验缺陷记录表 -->
    <excel name="importQualityInspection" returnClass="java.util.List">
        <![CDATA[
            IMPORT WITH SHEET="质量检验缺陷记录", HEADER=true,
                MAPPING={"批次号":"batchNo","产品名称":"productName","抽检数量":"inspectQty","缺陷数量":"defectQty","缺陷率":"defectRate","判定":"verdict"}
        ]]>
    </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.