将样式表添加到openxml excel文件

时间:2018-03-25 16:17:37

标签: openxml openxml-sdk

我试图在excel文件中插入一些样式。现在数据部分工作正常,但是当我打开excel时样式会抛出错误。它抱怨样式表文件在xml的第19行中有错误。我不知道那条线有什么问题。它是字体集合节点的起始位置。 任何想法为什么...... 这是样式的xml

<?xml version="1.0" encoding="utf-8"?>
<x:styleSheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main" mc:Ignorable="x14ac">
    <x:borders count="1">
        <x:border>
            <x:left style="thin">
                <x:color indexed="64" rgb="000000" />
            </x:left>
            <x:right style="thin">
                <x:color indexed="64" rgb="000000" />
            </x:right>
            <x:top style="thin">
                <x:color indexed="64" rgb="000000" />
            </x:top>
            <x:bottom style="thin">
                <x:color indexed="64" rgb="000000" />
            </x:bottom>
        </x:border>
    </x:borders>
    <x:fonts count="1">
        <x:font>
            <x:b />
            <x:sz val="12" />
            <x:color rgb="000000" />
            <x:name val="Arial" />
        </x:font>
    </x:fonts>
    <x:cellXfs>
        <x:xf fontId="0" borderId="0" applyFont="1" applyBorder="1" />
        <x:xf fontId="0" borderId="0" applyFont="1" applyBorder="1" />
        <x:xf fontId="0" borderId="0" applyFont="1" applyBorder="1" />
        <x:xf fontId="0" borderId="0" applyFont="1" applyBorder="1" />
        <x:xf fontId="0" borderId="0" applyFont="1" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf fontId="0" borderId="0" applyFont="1" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf fontId="0" borderId="0" applyFont="1" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf fontId="0" borderId="0" applyFont="1" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf fontId="0" borderId="0" applyFont="1" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf fontId="0" borderId="0" applyFont="1" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf fontId="0" borderId="0" applyFont="1" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
        <x:xf borderId="0" applyBorder="1" />
    </x:cellXfs>
</x:styleSheet>

更新

经过一些测试,谷歌搜索,敲打头......我想出了以下样式表xml

<?xml version="1.0" encoding="utf-8"?>
<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:x16r2="http://schemas.microsoft.com/office/spreadsheetml/2015/02/main" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" mc:Ignorable="x14ac x16r2 xr">
    <x:fonts count="2" x14ac:knownFonts="1">
        <x:font />
        <x:font>
            <x:b />
            <x:sz val="12" />
            <x:color rgb="FF000000" />
            <x:name val="Arial" />
            <x:family val="2" />
            <x:scheme val="minor" />
        </x:font>
    </x:fonts>
    <x:borders count="2">
        <x:border />
        <x:border>
            <x:left style="thin">
                <x:color rgb="FF000000" />
            </x:left>
            <x:right style="thin">
                <x:color rgb="FF000000" />
            </x:right>
            <x:top style="thin">
                <x:color rgb="FF000000" />
            </x:top>
            <x:bottom style="thin">
                <x:color rgb="FF000000" />
            </x:bottom>
        </x:border>
    </x:borders>
    <x:cellXfs count="3">
        <x:xf />
        <x:xf fontId="1" borderId="1" applyFont="1" applyBorder="1" />
        <x:xf borderId="1" applyBorder="1" />
    </x:cellXfs>
</x:styleSheet>

仍然excel抱怨文件在恢复文件后损坏它显示应用于每个单元格的样式会出现错误&#34;修复的记录:格式来自/xl/styles.xml部分(样式)&#34; 。 LibreOffice正确打开文件,OPEN XML SDK 2.0 Productivity Tool验证文件是否正确...为什么excel抱怨.....

更新 在玩完代码之后我解决了这个问题。 这两个问题也有帮助 Stylesheet 1 Stylesheet 2

似乎有一个命令来创建节点,特别是cellStyleXfs和cellXfs,cellStyleXfs将在cellXfs之前出现

工作XML

    <?xml version="1.0" encoding="utf-8"?>
<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:x16r2="http://schemas.microsoft.com/office/spreadsheetml/2015/02/main" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" mc:Ignorable="x14ac x16r2 xr">
    <x:fonts count="3" x14ac:knownFonts="1">
        <x:font />
        <x:font>
            <x:b />
            <x:sz val="12" />
            <x:color rgb="FF000000" />
            <x:name val="Arial" />
            <x:family val="2" />
            <x:scheme val="minor" />
        </x:font>
        <x:font>
            <x:b />
            <x:sz val="12" />
            <x:color rgb="FFFF0000" />
            <x:name val="Arial" />
            <x:family val="2" />
            <x:scheme val="minor" />
        </x:font>
    </x:fonts>
    <x:fills count="1">
        <x:fill />
    </x:fills>
    <x:borders count="2">
        <x:border />
        <x:border>
            <x:left style="thin">
                <x:color rgb="FF000000" />
            </x:left>
            <x:right style="thin">
                <x:color rgb="FF000000" />
            </x:right>
            <x:top style="thin">
                <x:color rgb="FF000000" />
            </x:top>
            <x:bottom style="thin">
                <x:color rgb="FF000000" />
            </x:bottom>
            <x:diagonal />
        </x:border>
    </x:borders>
    <x:cellStyleXfs count="1">
        <x:xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" />
    </x:cellStyleXfs>
    <x:cellXfs count="4">
        <x:xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" />
        <x:xf numFmtId="0" fontId="1" fillId="0" borderId="1" xfId="0" applyNumberFormat="1" applyFont="1" applyFill="1" applyBorder="1" />
        <x:xf numFmtId="0" fontId="2" fillId="0" borderId="1" xfId="0" applyNumberFormat="1" applyFont="1" applyFill="1" applyBorder="1" />
        <x:xf numFmtId="0" fontId="0" fillId="0" borderId="1" xfId="0" applyNumberFormat="1" applyFont="1" applyFill="1" applyBorder="1" />
    </x:cellXfs>
</x:styleSheet>

1 个答案:

答案 0 :(得分:0)

Excel仅限于palette of 56 colors。存储索引而不是您提供的实际RGB值。我不知道你是如何生成xmls的,但是你的字体节点应该是这样的:

<x:fonts count="1">
    <x:font>
        <x:b />
        <x:sz val="12" />
        <x:color theme="1" />
        <x:name val="Arial" />
    </x:font>
</x:fonts>  

如果您使用C#或VBA,则可以通过访问Workbook.Colors Property

来根据自己的喜好设置颜色

修改

与字体类似,边框需要存储索引而不是您提供的实际RGB值。您的边界节点应该是:

<x:left style="thin">
    <x:color indexed="64" />
</x:left>

另请注意,边框需要按正确顺序定义:左,右,上,下,对角线。在你的xml中似乎是正确的但是仍然想在这里提到它。

这是一个很好的example