We need to set the worksheet tab color using various custom colors. The worksheets are created from templates that contain smart markers. Each template’s worksheet tab is set to a specific custom color.
Initially, I thought I could simply use Worksheet.Copy, and the tab color would be preserved. However, occasionally the tab color ends up incorrect, and I’m not sure why.
To work around this, I now set the TabColor programmatically using Color.FromArgb with the custom RGB values. But occasionally, Color.FromArgb seems to return an incorrect color for the same RGB input. For example, #76933C should correspond to ARGB (255, 118, 147, 60), but I sometimes get ARGB (255, 89, 110, 45) instead. This usually happens to the last worksheet of my workbook.
The issue you’re experiencing with inconsistent worksheet tab colors when using Color.FromArgb may stem from how colors are being interpreted or applied in the context of your workbook. Sometimes, copying worksheets can lead to unexpected results if the source and destination formats differ.
Solution
To ensure consistent tab colors, it’s essential to explicitly set the tab color for each worksheet after copying. This approach helps avoid any discrepancies that might arise from the copying process.
Here’s a code snippet that demonstrates how to set the tab color programmatically:
// Instantiate a new Workbook
Workbook workbook = new Workbook("path/to/your/template.xlsx");
// Copy the first worksheet to a new one
Worksheet sourceWorksheet = workbook.Worksheets[0];
Worksheet newWorksheet = workbook.Worksheets.Add("NewSheet");
newWorksheet.Copy(sourceWorksheet);
// Set the tab color using Color.FromArgb
newWorksheet.TabColor = Color.FromArgb(255, 118, 147, 60); // For #76933C
// Save the workbook
workbook.Save("path/to/save/your/output.xlsx");
Next Steps
Ensure that you are setting the tab color immediately after copying the worksheet.
If the issue persists, check if there are any specific settings or properties in your templates that might affect color rendering.
Test with different RGB values to see if the issue is isolated to specific colors.
If you follow these steps and the problem continues, please provide additional details about your environment or any error messages you encounter. I’m here to help!
My logic is similar to the above. But during debugging, I observe that Color.FromArgb returns a wrong value for the same inputs (255, 118, 147, 60) so newWorksheet.TabColor is assigned a wrong value. I don’t know why this happens.
Thank you for the reply. The logic you showed works most of the time. But occasionally, one of tabs would have a wrong color.
Sorry I’m not able to share any templates because it’s against company’s policy.
I asked DS about this question. DS suggests me to use palette colors instead. I don’t know if this would work or not. But I will give it a try on Monday.
@sanshi
You can remove all data in the template file, then check whether this issue will happen again.
If yes, please share the desensitized file.
And which version are you using?
Thanks for the reply. We are always behind. I don’t know when we’ll have 25.x. For the time being, I recreated some templates from the scratch. I don’t see bad tab colors anymore.
@sanshi
Thank you for your feedback. I’m glad your issue has been resolved. If you have any questions, please feel free to contact us at any time.
In addition, Cells will release a version in the first half of each month, with version numbers combined by year and month. You can download and view the release version through the following link.