What is the fastest way to export dataGridView rows to Excel or into an SQL Server database

后端 未结 6 652
醉话见心
醉话见心 2021-01-18 07:02

What is the fastest way to export DataGridView rows in the range of 460328 - 800328 to Excel or into an SQL Server database table with out using Microsoft office interop as

6条回答
  •  深忆病人
    2021-01-18 07:55

    This may be faster way,

        using Excel = Microsoft.Office.Interop.Excel;
    
        public static void SaveGridToExcel(DataGridView DGV)
        {
    
            if (DGV.Rows.Count > 0)
            {
                string filename = "";
                SaveFileDialog SV = new SaveFileDialog();
                SV.Filter = "EXCEL FILES|*.xlsx;*.xls";
                DialogResult result = SV.ShowDialog();
    
    
                if (result == DialogResult.OK)
                {
    
                    filename = SV.FileName;
                    bool multiselect = DGV.MultiSelect;
                    DGV.MultiSelect = true;
                    DGV.SelectAll();
                    DGV.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
                    Clipboard.SetDataObject(DGV.GetClipboardContent());
                    var results = System.Convert.ToString(Clipboard.GetData(DataFormats.Text));
                    DGV.ClearSelection();
                    DGV.MultiSelect = multiselect;
                    Microsoft.Office.Interop.Excel.Application XCELAPP = null;
                    Microsoft.Office.Interop.Excel.Workbook XWORKBOOK = null;
                    Microsoft.Office.Interop.Excel.Worksheet XSHEET = null;
                    object misValue = System.Reflection.Missing.Value;
                    XCELAPP = new Excel.Application();
                    XWORKBOOK = XCELAPP.Workbooks.Add(misValue);
                    XCELAPP.DisplayAlerts = false;
                    XCELAPP.Visible = false;
                    XSHEET = XWORKBOOK.ActiveSheet;
                    XSHEET.Paste();
                    XWORKBOOK.SaveAs(filename, Excel.XlFileFormat.xlOpenXMLWorkbook);
                    XWORKBOOK.Close(false);
                    XCELAPP.Quit();
                    try
                    {                      
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(XSHEET);
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(XWORKBOOK);
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(XCELAPP);
                    }
                    catch { }
                }
            }
        }
    

提交回复
热议问题