Santhosh

Santhosh

  • NA
  • 154
  • 2.5k

How to set column width when export datagrid to excel in wpf

May 15 2017 10:05 AM
private void Export_Click(object sender, RoutedEventArgs e)
{
ExportToExcel();
}
private void ExportToExcel()
{
datagrid10.SelectAllCells();
datagrid10.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, datagrid10);
String resultat = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
String result = (string)Clipboard.GetData(DataFormats.Text);
datagrid10.UnselectAllCells();
System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Santhosh\Desktop\Exportdata.xls");
file.WriteLine(result.Replace(',', ' '));
file.Close();
MessageBox.Show(" Exporting DataBase to Excel file created", "Infromation", MessageBoxButton.OK, MessageBoxImage.Information);
}
This is my code 
 when i excute excel is created but in excel file columns are smaller and date format is in #### when i expand it then it will show correct date fromat.
How to set column width to excel sheet when i'm export. 
 
 

Answers (2)