2009-10-06 119 views
1

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

如下我定义样式:

 
<Style ss:ID="GreenText"> 
    <Font ss:FontName="Arial" ss:Size="9" ss:Color="#8CBE50" /> 
</Style> 

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

任何人都可以阐明为什么文本颜色不能正确呈现吗?

谢谢!

回答

0

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

编辑:
我没有用过的Office XML文档,但是这可能帮助(indexedColors标签定义调色板):
http://openxmldeveloper.org/forums/thread/309.aspx

也有一个Workbook.Colors属性从VBA改变调色板。

1

David是正确的,Excel 2003和以前版本的Excel仅限于56色调色板。

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

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

您可以下载免费试用here并自己尝试。

声明:我自己的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);