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

后端 未结 6 653
醉话见心
醉话见心 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:44

    Check out Create Excel files from C# without office as this refers to using EPPlus which works very well - I was able to create my CSV data from the data table and the bulk load in memory the Excel file to stream out. Simply a few lines of code. Varible csvData is string value of all your csvData.

        using( ExcelPackage pck = new ExcelPackage( ) )
        {
          //Create the worksheet
          ExcelWorksheet ws = pck.Workbook.Worksheets.Add( "Sheet1" );
    
          // set the delimiter
          etf.Delimiter = ',';
          etf.EOL = "\n";
          etf.TextQualifier = "\"";
    
          //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
          ws.Cells["A1"].LoadFromText( csvData, etf );
          return pck.GetAsByteArray( );
       }
    

提交回复
热议问题