Export the data inside the DataGridView to the table.
First, you need to add three references
Directly in the solution, right-click Add Reference to find the path. then copy the three files to the root of the project.
Then define the functions that export the table:
PublicStaticvoidDatagridviewtoexcel(DataGridView DGV) {#region Verification of Operability//Declaration Save dialog SaveFileDialog DLG =NewSaveFileDialog ();//Silent file suffix dlg. DEFAULTEXT ="Xls";//File suffix list dlg. Filter ="Excel File (*. XLS) |*.xls";//The silent path is the system current path DLG. InitialDirectory =Directory.GetCurrentDirectory ();//Open the Save dialog boxif (dlg. ShowDialog () = = DialogResult.Cancel)Return;//Return file pathString filenamestring =Dlg. FileName;//Verify that strFileName is empty or the value is invalidif (filenamestring.trim () = ="") {Return; }//Define the number of rows and columns of data within a tableint rowscount =DGV. Rows.Count;int colscount =DGV. Columns.count;//The number of rows must be greater than 0if (Rowscount <=0) {MessageBox.Show ("No data to save","Tips", MessageBoxButtons.OK, MessageBoxIcon.Information);Return; }//The number of columns must be greater than 0if (Colscount <=0) {MessageBox.Show ("No data to save","Tips", MessageBoxButtons.OK, MessageBoxIcon.Information);Return; }//The number of rows cannot be greater than 65536if (Rowscount >65536) {MessageBox.Show ("The number of data records is too many (up to 65,536) and cannot be saved","Tips", MessageBoxButtons.OK, MessageBoxIcon.Information);Return; }//The number of columns cannot be greater than 255if (Colscount >255) {MessageBox.Show ("There are too many rows of data records to save","Tips", MessageBoxButtons.OK, MessageBoxIcon.Information);Return; }//Verify that the file named Filenamestring exists, and if it exists, delete it FileInfo files =NewFileInfo (filenamestring);If(file. Exists) {Try{file. Delete (); }Catch(Exception error) {MessageBox.Show (error. Message,"Delete failed", MessageBoxButtons.OK, messageboxicon.warning);Return; } }#endregionExcel.Application objexcel =Null; Excel.Workbook Objworkbook =Null; Excel.Worksheet objsheet =Null;Try{//Declared Object objexcel =NewExcel.Application (); Objworkbook =OBJEXCEL.WORKBOOKS.ADD (Missing.Value); Objsheet =(Excel.Worksheet) Objworkbook.activesheet;//Set Excel not visible objexcel.visible =False;//Table header for writing tables to Excelint displaycolumnscount =1;for (int i =0; I <= DGV. ColumnCount-1; i++) {if (DGV. Columns[i]. Visible = =True) {objexcel.cells[1, Displaycolumnscount] =DGV. Columns[i]. Headertext.trim (); displaycolumnscount++; } }//Set the progress bar//Tempprogressbar.refresh ();//Tempprogressbar.visible = true;//Tempprogressbar.minimum=1;//TEMPPROGRESSBAR.MAXIMUM=DGV. RowCount;//Tempprogressbar.step=1;//To write data in a table row by column in Excelfor (int row =0; Row <= DGV. RowCount-1; row++) {//Tempprogressbar.performstep ();Displaycolumnscount =1;for (int col =0; Col < Colscount; col++) {if (DGV. Columns[col]. Visible = =True) {Try{Objexcel.cells[row +2, Displaycolumnscount] =DGV. Rows[row]. Cells[col]. Value.tostring (). Trim (); displaycolumnscount++; }Catch(Exception) { } } } }//Hide Progress bar//Tempprogressbar.visible = false;//Save FileObjworkbook.saveas (filenamestring, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); }Catch(Exception error) {MessageBox.Show (error. Message,"Warning", MessageBoxButtons.OK, messageboxicon.warning);Return; }Finally{//Close the Excel appif (Objworkbook! =Null) Objworkbook.close (Missing.Value, Missing.Value, Missing.Value);if (objexcel.workbooks! = null if (objexcel! = null) Objexcel.quit (); objsheet = nullnullnull< Span style= "color: #000000;" >; } MessageBox.Show (filenamestring + "\n\n Export is complete! " hint " MessageBoxButtons.OK, messageboxicon.information); }
If you want to export the table, just trigger the function and throw the DataGridView in.
void button1_click (// export Form datagridviewtoexcel (dataGridView1); }
DataGridView Export Excel, more useful code