【问题标题】:Minimal style sheet for Excel Open XML with dates?带有日期的 Excel Open XML 的最小样式表?
【发布时间】:2014-09-26 01:34:53
【问题描述】:

我正在尝试使用 Open XML SDK 创建一个 Excel 文件,该文件所需的最小样式表允许我将单元格格式化为日期。以下是我对样式表的尝试:

<?xml version="1.0" encoding="utf-8"?>
<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    <x:cellXfs count="1">
        <x:xf numFmtId="14" xfId="0" applyNumberFormat="1" />
    </x:cellXfs>
</x:styleSheet>

即使没有从单元格中引用此样式,Excel 也会告诉我我的文件已损坏。如果我删除 cellXfs 元素,则文件可以正常打开。有人可以解释我还需要添加什么吗?创建样式表的 C# 代码如下。

var stylesheet = new Stylesheet();
var cellFormats = new CellFormats() { Count = 1 };
var cellFormat = new CellFormat();
cellFormat.NumberFormatId = 14;
cellFormat.FormatId = 0;
cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true);
cellFormats.Append(cellFormat);
stylesheet.Append(cellFormats);

我已尝试添加单元格样式格式和单元格样式,但仅具有上述内容似乎破坏了文档,我不确定为什么。

【问题讨论】:

    标签: excel date openxml openxml-sdk


    【解决方案1】:

    经过多次反复试验,我发现样式表需要以下内容:

    • 默认字体
    • 默认填充
    • 默认边框
    • 默认单元格格式

    忽略其中任何一个都会导致 Excel 出错,除非所有这些都被忽略。此外,我为日期添加了另一种单元格格式。

    我希望这对其他人有用。以下代码创建了一个允许日期格式(数字格式 id 22)的工作样式表:

    var stylesheet = new Stylesheet();
    
    // Default Font
    var fonts = new Fonts() { Count = 1, KnownFonts = BooleanValue.FromBoolean(true) };
    var font = new Font
                    {
                        FontSize = new FontSize() { Val = 11 },
                        FontName = new FontName() { Val = "Calibri" },
                        FontFamilyNumbering = new FontFamilyNumbering() { Val = 2 },
                        FontScheme = new FontScheme() { Val = new EnumValue<FontSchemeValues>(FontSchemeValues.Minor) }
                    };
    fonts.Append(font);
    stylesheet.Append(fonts);
    
    // Default Fill
    var fills = new Fills() { Count = 1 };
    var fill = new Fill();
    fill.PatternFill = new PatternFill() { PatternType = new EnumValue<PatternValues>(PatternValues.None) };
    fills.Append(fill);
    stylesheet.Append(fills);
    
    // Default Border
    var borders = new Borders() { Count = 1 };
    var border = new Border
                        {
                            LeftBorder = new LeftBorder(),
                            RightBorder = new RightBorder(),
                            TopBorder = new TopBorder(),
                            BottomBorder = new BottomBorder(),
                            DiagonalBorder = new DiagonalBorder()
                        };
    borders.Append(border);
    stylesheet.Append(borders);
    
    // Default cell format and a date cell format
    var cellFormats = new CellFormats() { Count = 2 };
    
    var cellFormatDefault = new CellFormat { NumberFormatId = 0, FormatId = 0, FontId = 0, BorderId = 0, FillId = 0 };
    cellFormats.Append(cellFormatDefault);
    
    var cellFormatDate = new CellFormat { NumberFormatId = 22, FormatId = 0, FontId = 0, BorderId = 0, FillId = 0, ApplyNumberFormat = BooleanValue.FromBoolean(true) };
    cellFormats.Append(cellFormatDate);
    
    stylesheet.Append(cellFormats);
    
    return stylesheet;
    

    生成的 XML 如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
        <x:fonts count="1" x14ac:knownFonts="1" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
            <x:font>
                <x:sz val="11" />
                <x:name val="Calibri" />
                <x:family val="2" />
                <x:scheme val="minor" />
            </x:font>
        </x:fonts>
        <x:fills count="1">
            <x:fill>
                <x:patternFill patternType="none" />
            </x:fill>
        </x:fills>
        <x:borders count="1">
            <x:border>
                <x:left />
                <x:right />
                <x:top />
                <x:bottom />
                <x:diagonal />
            </x:border>
        </x:borders>
        <x:cellXfs count="2">
            <x:xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" />
            <x:xf numFmtId="22" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1" />
        </x:cellXfs>
    </x:styleSheet>
    

    【讨论】:

    • 我发现它默认应该有两个填充模式,另一个是&lt;fill&gt;&lt;patternFile patternTyle="gray125"&gt;&lt;/fill&gt;。如果您不提供它不会给出错误,但是如果您尝试在 ID 1 处提供自己的填充图案,它将无法正常工作。
    • 13 年后,您帮助我解决了我一天中大部分时间都在努力解决的问题!
    猜你喜欢
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    相关资源
    最近更新 更多