1

我正在编写一个工具来生成一些电子表格 ML (XML) 来为我的用户创建一个 Excel 电子表格。

我定义了一种风格如下:

<样式 ss:ID="GreenText">
  <字体 ss:FontName="Arial" ss:Size="9" ss:Color="#8CBE50" />
</样式>

这在一定程度上有效,但是当我在 Excel 中打开它时,为文本呈现的颜色不是我指定的颜色 - 它是一个更亮的版本。我可以对单元格边框使用相同的颜色参考,并且可以正确渲染颜色。

任何人都可以解释为什么文本颜色没有正确呈现吗?

谢谢!

4

2 回答 2

1

David 是正确的,Excel 2003 和早期版本的 Excel 仅限于 56 个调色板。

Excel 2007 添加了对 24 位颜色和主题颜色的支持。Excel 2007 可以编写包含此附加颜色信息的 xls 工作簿,并且 Excel 2003 可以读取这些工作簿,但 Excel 2003 仍将仅限于 56 种调色板。Excel 2007 可以加载这些工作簿并显示准确的颜色。

SpreadsheetGear for .NET支持新的 24 位颜色和主题颜色,以及旧的调色板索引颜色,就像 Excel 2007 一样。您可以使用 SpreadsheetGear 创建一个 24 位颜色的工作簿,该工作簿将在 Excel 2007 中正确显示,或者修改调色板,它们将在 Excel 2007 和 Excel 2003 中正确显示。下面是两者的示例。

您可以在此处下载免费试用版并亲自试用。

免责声明:我拥有 SpreadsheetGear LLC

这是示例代码:

            // Create a new empty workbook with one worksheet.
            IWorkbook workbook = Factory.GetWorkbook();
            // Get the worksheet and change it's name to "Person".
            IWorksheet worksheet = workbook.Worksheets[0];
            worksheet.Name = "Colors";
            // Put "Hello World!" into A1.
            IRange a1 = worksheet.Cells["A1"];
            a1.Value = "Hello World!";
            a1.Font.Color = System.Drawing.Color.FromArgb(0x8C, 0xBE, 0x50);
            // Save the workbook as xls (Excel 97-2003 / Biff8) with default palette.
            //
            // This workbook will display the exact color in Excel 2007 and
            // SpreadsheetGear 2009, but will only display the closest available 
            // palette indexed color in Excel 2003.
            workbook.SaveAs(@"C:\tmp\GreenDefaultPalette.xls", FileFormat.Excel8);
            // Save as xlsx / Open XML which will also display the exact color.
            workbook.SaveAs(@"C:\tmp\GreenDefaultPalette.xlsx", FileFormat.OpenXMLWorkbook);
            // Now, modify the palette and save. This workbook will display the exact
            // color in Excel 2003 as well as in SpreadsheetGear 2009 and Excel 2007.
            // 
            // Note that modifying the palette will change the color of any cells which
            // already reference this palette indexed color - so be careful if you are
            // modifying pre-existing workbooks.
            workbook.Colors[0] = a1.Font.Color;
            workbook.SaveAs(@"C:\tmp\GreenModifiedPalette.xls", FileFormat.Excel8);
于 2009-10-07T16:16:02.257 回答
0

Excel 仅限于 56 种颜色的调色板。它只存储颜色索引而不是实际的 RGB 值。它们确实允许调色板中的自定义颜色,但我不知道如何以编程方式更改它们

编辑:
我没有使用过 office xml 文档,但这可能会有所帮助(用于定义调色板的 indexedColors 标记):
http ://openxmldeveloper.org/forums/thread/309.aspx

还有一个 Workbook.Colors 属性用于从 VBA 更改调色板。

于 2009-10-06T22:42:28.180 回答