This is not the best solution, but here is what I did, it opens a new excel document and copies what is in the data set, all you have to do is sort the columns and save it.
Btw is posting his first post to answer a question, hope it helps
private void cmdExport_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("excel.exe"); try { copyAlltoClipboard(); Microsoft.Office.Interop.Excel.Application xlexcel; Microsoft.Office.Interop.Excel.Workbook xlWorkBook; Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; xlexcel = new Excel.Application(); xlexcel.Visible = true; xlWorkBook = xlexcel.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1]; CR.Select(); xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true); } catch (Exception ex) { MessageBox.Show("Error :" + ex.Message); } } private void copyAlltoClipboard() { dataGridViewItems.SelectAll(); DataObject dataObj = dataGridViewItems.GetClipboardContent(); if (dataObj != null) Clipboard.SetDataObject(dataObj); }
source share