Click here to Skip to main content
15,794,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Problem statement -
WPF Datagrid is formatted with

1.Datagrid on full page (100 Rows)
2.Condition rows ID < 50 then cell = light green
else cell = blue

Now when i tried to export the excel, the cell formatted properly.
it is not correctly skips rows or alter colors.
the same grid work with 20 rows where i split condition to 10(lightgreen)-10(blue) working perfect with exporting.

What I have tried:

What i tried :
Loop through DataGrid.
public static System.Windows.Controls.DataGridCell GetCell(System.Windows.Controls.DataGrid host, DataGridRow row, int columnIndex)

Copy Cell content to Object
2.xCell =

3. xCell =

4. Also tried

5. apply that style to excel cell

the Datagrid formatted correctly. and export perfectly with colors if only of less records which is not required scrolling.
i need to develop one open source datagrid for other developers
which export with same font and colors for any number of rows formatted.
Updated 27-Oct-21 21:31pm

1 solution

All you need to do is to loop through the collection of rows in datagridview and export them do Excel via using, for example: EPPlus library[^].

How to use it? See: Home · JanKallman/EPPlus Wiki · GitHub[^]
Share this answer
iAnkitBhatt 28-Oct-21 2:50am    
Thanks for quick reply.using EPPLUS only.

Exporting + formatting all things works fine.
BUT Formatting on data is issue after certain rows to excel.

i am doing the same with EPPLUS and exporting it including formatting when data is within one page. but when data is large formatting is not working proper.

issue is with "cell-style" which is not copying after certain rows let say 50.
all looping and execution logic is correct.datagrid is formatting all rows. data is also exporting with color formatting for rows which are visible on screen. the issue is when next rows required scroll that are not formatting at Excel side.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900