|
| 1 | +--- |
| 2 | +title: Retrieving Themable Cell Color in RadSpreadProcessing |
| 3 | +description: Learn how to retrieve the actual cell color in RadSpreadProcessing when the color comes from the document theme. |
| 4 | +type: how-to |
| 5 | +page_title: How to Retrieve Cell Color from Theme in RadSpreadProcessing |
| 6 | +slug: retrieve-cell-color-radspreadprocessing |
| 7 | +tags: radspreadprocessing, document processing, cell color, themable color, pattern fill |
| 8 | +res_type: kb |
| 9 | +ticketid: 1656165 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Product | RadSpreadProcessing for Document Processing | |
| 15 | +| --- | --- | |
| 16 | +| Version | 2021.3.1123 | |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +I have imported an Excel file with some cells formatted with color. Is it possible for me to retrieve the cell color in my program? When the background color is set from MS Excel, my implementation does not retrieve the actual background color. |
| 21 | + |
| 22 | +This KB article also answers the following questions: |
| 23 | +- How can I get the background color of a cell in RadSpreadProcessing? |
| 24 | +- How to handle cell colors that come from a document theme in RadSpreadProcessing? |
| 25 | +- What is the method to extract the actual color value of a cell formatted in Excel through RadSpreadProcessing? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To retrieve the cell color in RadSpreadProcessing, especially when the color is applied through the document theme, follow these steps: |
| 30 | + |
| 31 | +1. Import the Excel document using the appropriate format provider. |
| 32 | +2. Access the desired cell or range of cells. |
| 33 | +3. Check if the cell's fill is of type `PatternFill`. |
| 34 | +4. Retrieve the `ThemableColor` object from the `PatternFill`. |
| 35 | +5. Use the `GetActualValue` method of the `ThemableColor` object, passing in the document's theme, to get the actual color value. |
| 36 | + |
| 37 | +Here is a sample code snippet demonstrating these steps: |
| 38 | + |
| 39 | +```csharp |
| 40 | +string filePath = "Book1.xlsx"; |
| 41 | +Workbook workbook = new Workbook(); |
| 42 | +IWorkbookFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider(); |
| 43 | + |
| 44 | +using (Stream input = new FileStream(filePath, FileMode.Open)) |
| 45 | +{ |
| 46 | + workbook = formatProvider.Import(input); |
| 47 | +} |
| 48 | +Worksheet worksheet = workbook.Worksheets.First(); |
| 49 | +CellSelection selection = worksheet.Cells[0,1]; |
| 50 | +PatternFill solidPatternFill = selection.GetFill().Value as PatternFill; |
| 51 | +if (solidPatternFill != null) |
| 52 | +{ |
| 53 | + PatternType type = solidPatternFill.PatternType; |
| 54 | + ThemableColor patternColor = solidPatternFill.PatternColor; |
| 55 | + Color color = patternColor.LocalValue; |
| 56 | + ThemableColor bg = solidPatternFill.BackgroundColor; |
| 57 | + Color bgcolor = bg.LocalValue; |
| 58 | + |
| 59 | + Color actualColor = patternColor.GetActualValue(workbook.Theme); |
| 60 | + // The actual color is the same as Accent1 color of the colorScheme |
| 61 | + Debug.WriteLine("RGB: " + actualColor.R.ToString() + ", " + actualColor.G.ToString() + ", " + actualColor.B.ToString()); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +This approach ensures that even when a cell's color is derived from the document's theme, you can obtain the actual color value as displayed in the Excel file. |
| 66 | + |
| 67 | +## See Also |
| 68 | + |
| 69 | +- [Document Themes in RadSpreadProcessing](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/features/styling/document-themes) |
| 70 | +- [Getting Actual Values](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/features/styling/document-themes#getting-actual-values) |
| 71 | + |
| 72 | +--- |
0 commit comments