Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi I am exporting a datagrid view to excel. Is there any way to make right align for a column. I a working in C# windows application
Posted
Comments
Westlmorland 21-Mar-17 8:02am    
You could use this C# code to export to Excel after which you could apply the desired styling to ExcelColumnRowBase.Style property.
But instead of that you could use a direct method, DataGridViewConverter.ImportFromDataGridView, which is provided by this c# library for Excel, you can find an example of how to do that here.

For example if you want to set the alignment of the second column, here is the code :

C#
var eApp = new Excel.Application { DisplayAlerts = false };
eApp.Workbooks.Open(@"c:\target.xlsx");
eApp.Visible = true;
Worksheet ws = eApp.ActiveSheet;
ws.Columns[2].HorizontalAlignment = XlHAlign.xlHAlignRight;


Hope it helps.
 
Share this answer
 
Comments
Dalek Dave 30-Nov-11 4:01am    
Beat me to it! :)
Have a 5.
Amir Mahfoozi 30-Nov-11 4:33am    
Thank you very much Dalek :)
You will need to the Office Interop to open Excel and adjust the properties of the cells
 
Share this answer
 
Comments
sumana shabeeb 30-Nov-11 0:40am    
i am using Interop . the number columns are aligned as right automatically but i need to arrange some text column also right allign

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