iOfficeAI/OfficeCLI · error · ArgumentException

columns.{n}.dxfId requires a numeric dxf id, got: '{preDxf}'

Error message

columns.{n}.dxfId requires a numeric dxf id, got: '{preDxf}'

What it means

Thrown by AddTable's pre-validation loop (before AddNewPart) when a columns.N.dxfId or column.N.dxfId property is present but not parseable as a uint. Validating before creating the TableDefinitionPart prevents an orphan, empty xl/tables/tableN.xml part that real Excel would refuse. The id maps to the TableColumn DataFormatId and must be a non-negative integer.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs:1304

                }
            }
            else
            {
                for (int i = 0; i < colCount; i++)
                    colNames[i] = $"Column{i + 1}";
            }
        }

        // Validate columns.N.dxfId BEFORE creating the TableDefinitionPart.
        // A non-numeric id used to throw further down (after AddNewPart and
        // before Table.Save), leaving an orphan xl/tables/tableN.xml part with
        // empty content — malformed XML that makes real Excel refuse the file.
        for (int n = 1; n <= colCount; n++)
        {
            if ((properties.TryGetValue($"columns.{n}.dxfId", out var preDxf)
                    || properties.TryGetValue($"column.{n}.dxfId", out preDxf))
                && !uint.TryParse(preDxf, out _))
                throw new ArgumentException(
                    $"columns.{n}.dxfId requires a numeric dxf id, got: '{preDxf}'");
        }

        var tableDefPart = tblWorksheet.AddNewPart<TableDefinitionPart>();
        var table = new Table
        {
            Id = (uint)tableId,
            Name = tableName,
            DisplayName = displayName,
            Reference = rangeRef,
            TotalsRowShown = hasTotalRow
        };
        if (hasTotalRow)
            table.TotalsRowCount = 1;
        if (!hasHeader)
            table.HeaderRowCount = 0;

        // An <autoFilter> is only valid on a table WITH a header row — the

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Supply a non-negative integer dxf id that exists in the workbook's dxfs collection, e.g. columns.1.dxfId=3.
  2. Remove the dxfId property if no custom data formatting is needed.
  3. Use either the columns.N.dxfId or column.N.dxfId key (both are accepted).

Example fix

// before
add /Sheet1/table --prop columns.1.dxfId=highlight
// after
add /Sheet1/table --prop columns.1.dxfId=3
Defensive patterns

Strategy: validation

Validate before calling

// Validate every columns.N.dxfId / column.N.dxfId is a uint before Add.
static bool TryValidateDxfIds(Dictionary<string,string> props, out string error)
{
    error = null;
    foreach (var (k, v) in props)
    {
        if (!k.StartsWith("columns.") && !k.StartsWith("column.")) continue;
        if (!k.EndsWith(".dxfId")) continue;
        if (!uint.TryParse(v, out _))
        { error = $"{k} requires a numeric dxf id, got: '{v}'"; return false; }
    }
    return true;
}

Type guard

static bool IsValidDxfId(string v) => uint.TryParse(v, out _);

Try / catch

try { handler.Add(parentPath, "table", null, props); }
catch (ArgumentException ex) when (ex.Message.Contains("requires a numeric dxf id"))
{ /* fix or remove the offending dxfId property, then retry */ }

Prevention

When it happens

Trigger: Calling Add('/Sheet1/table', ...) with columns.1.dxfId=abc, columns.2.dxfId=-1, or column.1.dxfId= (empty).

Common situations: Passing a style index as a name instead of an id, a negative value, or a leftover placeholder string.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/d17cd1e90764d3f3. Report an issue: GitHub.