0

With the below code I have loaded datagrid in WPF from ms access DB using c# but not sure how to export the same datagrid to Excel.

try
{
    OleDbConnection connect = new OleDbConnection();
    connect.ConnectionString = 
    @"Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\Electricalcircuits.mdb";

    OleDbCommand cmd = new OleDbCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "Select * from REPORT";
    cmd.Connection = connect;
    connect.Open();
    System.Data.DataTable dt = new System.Data.DataTable();
    OleDbDataAdapter DA = new OleDbDataAdapter(cmd);
    DA.Fill(dt);
    DG1.ItemsSource = dt.AsDataView();

}
catch (OleDbException ex)
{
    MessageBox.Show(ex.ToString());
}
FaizanHussainRabbani
  • 3,256
  • 3
  • 26
  • 46
  • I tried using closedxml library after installing it from Nuget package installer. Getting error " xlworkbook.cs not found" while running the application – dineshkrishnan Feb 21 '18 at 17:14
  • code used for export: string folderPath = "C:\\Excel\\"; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } //Codes for the Closed XML using (ClosedXML.Excel.XLWorkbook wb = new ClosedXML.Excel.XLWorkbook()) { wb.Worksheets.Add(dt, "Customers"); wb.SaveAs(folderPath + "Report.xlsx"); } – dineshkrishnan Feb 21 '18 at 17:14
  • Is there any easy method to export a ms access table to Excel using c# wpf – dineshkrishnan Feb 21 '18 at 17:25

2 Answers2

2

To Export the DataGrid into Excel(Csv) on button click using C# in WPF

private void btnExport_Click(object sender, RoutedEventArgs e)
  {            
     string ExportName = (sender as System.Windows.Controls.Button).Name.ToString();
     bool result = Export.SaveToCSV(TrkDataGrid, ExportName);//pass the Datagrid and Exportname
     if (result == true)
     {
      MessageBoxResult result = System.Windows.MessageBox.Show("Exported successfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
     }
  }
 public bool SaveToCSV(System.Windows.Controls.DataGrid dataGrid,string Filename)
  {
    bool IsVaild = false;
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.Title = "Save CSV Files";
    saveFileDialog.Filter = "CSV file (*.csv)|*.csv";
    saveFileDialog.FileName = Filename;           
    string gridname = Filename;
    if (saveFileDialog.ShowDialog() == DialogResult.OK)
    {
     string  path = System.IO.Path.GetFullPath(saveFileDialog.FileName);                    
     createcsvfile(dataGrid, path);              
      IsVaild = true;
    }
    return IsVaild;      
  }
 private void createcsvfile(System.Windows.Controls.DataGrid dataGrid, string FilePath)
  {            
    dataGrid.SelectAllCells();
    dataGrid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;           
    ApplicationCommands.Copy.Execute(null, dataGrid);
    dataGrid.UnselectAllCells();
    String result = (string)System.Windows.Clipboard.GetData(System.Windows.DataFormats.CommaSeparatedValue);
    File.AppendAllText(FilePath, result, UnicodeEncoding.UTF8);         
  } 
0

You have two ways, you can use the DataTable:

How to export DataTable to Excel

or you can use the DataGridView "DG1":

https://code.msdn.microsoft.com/office/How-to-Export-DataGridView-62f1f8ff

polzka90
  • 123
  • 9
  • I tried using closedxml library after installing it from Nuget package installer. Getting error " xlworkbook.cs not found" while running the application – dineshkrishnan Feb 21 '18 at 16:56
  • code used for export: string folderPath = "C:\\Excel\\"; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } //Codes for the Closed XML using (ClosedXML.Excel.XLWorkbook wb = new ClosedXML.Excel.XLWorkbook()) { wb.Worksheets.Add(dt, "Customers"); wb.SaveAs(folderPath + "Report.xlsx"); } – dineshkrishnan Feb 21 '18 at 16:58