@John.He , @amjad.sahi, @peyton.xu
// Example: Apply background color priority - Top, Middle, Bottom
Style topStyle = worksheet.Workbook.CreateStyle();
topStyle.ForegroundColor = Color.LightGreen;
topStyle.Pattern = BackgroundType.Solid;
Style middleStyle = worksheet.Workbook.CreateStyle();
middleStyle.ForegroundColor = Color.LightYellow;
middleStyle.Pattern = BackgroundType.Solid;
Style bottomStyle = worksheet.Workbook.CreateStyle();
bottomStyle.ForegroundColor = Color.LightBlue;
bottomStyle.Pattern = BackgroundType.Solid;
// Apply based on row position (example rows)
worksheet.Cells.Rows[1].ApplyStyle(topStyle, new StyleFlag { CellShading = true });
worksheet.Cells.Rows[1].ApplyStyle(middleStyle, new StyleFlag { CellShading = true });
worksheet.Cells.Rows[1].ApplyStyle(bottomStyle, new StyleFlag { CellShading = true });
After Excel output First row see this Color.LightGreen, not this one Color.LightBlue without any code order change is it possible???.
@imparthgalani
Cause
The issue you’re encountering arises because when you apply multiple styles to the same row, the last applied style takes precedence, effectively hiding the previous styles. This behavior is similar to how z-index works in CSS, where the last element in the stacking order is displayed on top.
Solution
To achieve the desired effect of having multiple background colors visible in the same row, you can use a workaround by applying styles to individual cells instead of entire rows. This way, you can control the order and visibility of the colors more effectively.
Code
Here’s an example of how to apply different background colors to individual cells in the same row:
// Create styles for each color
Style topStyle = worksheet.Workbook.CreateStyle();
topStyle.ForegroundColor = Color.LightGreen;
topStyle.Pattern = BackgroundType.Solid;
Style middleStyle = worksheet.Workbook.CreateStyle();
middleStyle.ForegroundColor = Color.LightYellow;
middleStyle.Pattern = BackgroundType.Solid;
Style bottomStyle = worksheet.Workbook.CreateStyle();
bottomStyle.ForegroundColor = Color.LightBlue;
bottomStyle.Pattern = BackgroundType.Solid;
// Apply styles to individual cells in the same row
worksheet.Cells[1, 0].ApplyStyle(topStyle, new StyleFlag { CellShading = true }); // Cell A2
worksheet.Cells[1, 1].ApplyStyle(middleStyle, new StyleFlag { CellShading = true }); // Cell B2
worksheet.Cells[1, 2].ApplyStyle(bottomStyle, new StyleFlag { CellShading = true }); // Cell C2
In this example, each cell in the second row (A2, B2, C2) is styled individually, allowing you to see all colors without them overlapping.
Next Steps
- Adjust the cell references in the code to match your specific requirements.
- Test the implementation to ensure that the colors display as expected.
- If you need more complex layering or effects, consider using shapes or images as overlays.
Please implement these changes, and let me know if you need further assistance!
@Professionalize.Discourse , Not using individual cells in the same row, apply all row
@imparthgalani
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
Style topStyle = worksheet.Workbook.CreateStyle();
topStyle.ForegroundColor = Color.LightGreen;
topStyle.Pattern = BackgroundType.Solid;
Style middleStyle = worksheet.Workbook.CreateStyle();
middleStyle.ForegroundColor = Color.LightYellow;
middleStyle.Pattern = BackgroundType.Solid;
Style bottomStyle = worksheet.Workbook.CreateStyle();
bottomStyle.ForegroundColor = Color.LightBlue;
bottomStyle.Pattern = BackgroundType.Solid;
// Apply based on row position (example rows)
worksheet.Cells.Rows[1].ApplyStyle(topStyle, new StyleFlag { CellShading = true });
worksheet.Cells.Rows[1].ApplyStyle(middleStyle, new StyleFlag { CellShading = true });
worksheet.Cells.Rows[1].ApplyStyle(bottomStyle, new StyleFlag { CellShading = true });
workbook.Save(dir + "dest.xlsx");
dest.zip (6.0 KB) is generated with above codes. The color LightBlue is applied.
This will ok but in my case Color.LightGreen visible top like priority witch one show first without applying style order changes.
@imparthgalani
Could you create an excepted file in Excel and share it here? We will check it soon.
1 Like
dest (2).zip (5.5 KB)
Output like this attachment file without order change, applying based on row position and style change it is possible ???.
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
Style topStyle = worksheet.Workbook.CreateStyle();
topStyle.ForegroundColor = Color.LightGreen;
topStyle.Pattern = BackgroundType.Solid;
Style middleStyle = worksheet.Workbook.CreateStyle();
middleStyle.ForegroundColor = Color.LightYellow;
middleStyle.Pattern = BackgroundType.Solid;
Style bottomStyle = worksheet.Workbook.CreateStyle();
bottomStyle.ForegroundColor = Color.LightBlue;
bottomStyle.Pattern = BackgroundType.Solid;
// Apply based on row position (example rows)
worksheet.Cells.Rows[1].ApplyStyle(topStyle, new StyleFlag { CellShading = true });
worksheet.Cells.Rows[1].ApplyStyle(middleStyle, new StyleFlag { CellShading = true });
worksheet.Cells.Rows[1].ApplyStyle(bottomStyle, new StyleFlag { CellShading = true });
workbook.Save(dir + "dest.xlsx");
@imparthgalani
Sorry, the example code cannot achieve the expected result. The basic rule is that when setting the same attribute, the value set later will overwrite the value set earlier. If you want to keep the first set value, do not set the same attribute multiple times.
1 Like
@John.He, Thank you for the early response.
@imparthgalani
You are welcome. If you have any questions, please feel free to contact us at any time.
1 Like