Tag: .net 4.0

使用Open XML 2.0在Excel中获取单元格背景颜色

我正在尝试在Excel电子表格中获取单元格的背景颜色。 我正在使用Open XML 2.0 SDK,我可以打开* .xlsx文件并获取单元格值。 我的代码获取背景颜色如下: public BackgroundColor GetCellBackColor(Cell theCell, SpreadsheetDocument document) { BackgroundColor backGroundColor = null; WorkbookStylesPart styles = SpreadsheetReader.GetWorkbookStyles(document); int cellStyleIndex = (int)theCell.StyleIndex.Value; CellFormat cellFormat = (CellFormat)styles.Stylesheet.CellFormats.ChildElements[cellStyleIndex]; Fill fill = (Fill)styles.Stylesheet.Fills.ChildElements[(int)cellFormat.FillId.Value]; backGroundColor = fill.PatternFill.BackgroundColor; return backGroundColor; } 我的问题在于, PatternFill.BackgroundColor只返回一个自然数,我认为它是样式的id。 我的问题是,那行代码 DocumentFormat.OpenXml.Spreadsheet.Color c = (DocumentFormat.OpenXml.Spreadsheet.Color)styles.Stylesheet.Colors.ChildElements[Int32.Parse(backGroundColor.InnerText)]; 返回一个错误,因为Stylesheet.Colors为null ……也许是因为我在excel中使用了“内置”颜色 – 而不是自定义的颜色?! 任何想法如何能从“backGroundColor-Value”“计算”真实的颜色数?

互操作程序集embedded到.NET 4.0中时缺lessExcel get_Range

我build立一个引用COM interop DLL的程序集。 如果通过在引用的属性(VS2010)中将Embed Interop Types设置为True来embeddedCOM互操作types,则在运行时会发生错误“对象不包含get_Range的定义”。 如果COM互操作types没有embedded,则不会发生错误。 有谁知道为什么应该省略一个特定的方法,Worksheet.get_Range或如何解决这个问题或有任何其他相关的见解? 我应该感激任何帮助。 interop dll包含对Worksheet.get_Range(object,[object])的引用。 在我的调用程序集上使用reflection器时,没有提及Worksheet下的get_Range。 我embedded的互操作程序集是从Excel9.olb生成的。 我不使用PIA作为应用程序的目标多个Excel版本。