/// <Summary>
/// Import data from the datagridview to a table
/// </Summary>
/// <Returns>
/// Return a table
/// </Returns>
Private datatable exporedatatotable (datagridview datagridatagri)
{
// Import data from the datagridview to the table
Datatable temptable = new datatable ("temptable ");
// Create a column
For (INT column = 0; column <datagridview. Columns. Count; column ++)
{
Datacolumn tempcolumn = new datacolumn (datagridview. Columns [column]. headertext, typeof (string ));
Temptable. Columns. Add (tempcolumn );
}
// Add the data of the row in the datagridview to the table
For (int row = 0; row <datagridview. Rows. Count; row ++)
{
Datarow temprow = temptable. newrow ();
For (INT column = 0; column <datagridview. Columns. Count; column ++)
{
Temprow [column] = datagridview. Rows [row]. cells [column]. value;
}
Temptable. Rows. Add (temprow );
}
Return temptable;
}
Private void buttonstuout_click (Object sender, eventargs E)
{
// Export data from the datagridview to a table
Datatable temptable = This. exporedatatotable (this. datagridview1 );
// Export the information to the Excel table
Microsoft. Office. InterOP. Excel. applicationclass myexcel;
Microsoft. Office. InterOP. Excel. workbooks myworkbooks;
Microsoft. Office. InterOP. Excel. Workbook myworkbook;
Microsoft. Office. InterOP. Excel. worksheet myworksheet;
Char mycolumns;
Microsoft. Office. InterOP. Excel. Range myrange;
Object [,] mydata = new object [500, 35];
Int I, j; // J indicates the row, and I indicates the column
Myexcel = new Microsoft. Office. InterOP. Excel. applicationclass ();
Myexcel. Visible = true;
If (myexcel = NULL)
{
MessageBox. Show ("the local EXCEL program cannot be started! Please check that your Microsoft Office is correctly installed and can be used properly "," prompt ");
Return;
}
Myworkbooks = myexcel. workbooks;
Myworkbook = myworkbooks. Add (system. reflection. Missing. value );
Myworksheet = (Microsoft. Office. InterOP. Excel. worksheet) myworkbook. worksheets [1];
Mycolumns = (char) (This. datagridview1.columns. Count + 64); // sets the column
Myrange = myworksheet. get_range ("A4", mycolumns. tostring () + "5"); // you can specify the column width.
Int COUNT = 1; // The first column is empty.
// Set the column name
Foreach (datacolumn mynewcolumn in temptable. columns)
{
Mydata [0, Count] = mynewcolumn. columnname;
Count = count + 1;
}
// Output data records in the datagridview and place them in a two-dimensional array
J = 1;
Foreach (datarow myrow in temptable. Rows) // loop row
{
For (I = 1; I <= temptable. Columns. Count; I ++) // loop Column
{
Mydata [J, I] = myrow [I-1]. tostring ();
}
J ++;
}
// Write the data in the two-dimensional array to excel
Myrange = myrange. get_resize (temptable. Rows. Count + 1, temptable. Columns. Count); // create columns and rows
Myrange. value2 = mydata;
Myrange. entirecolumn. autofit ();
Myworksheet. cells [2, 2] = "data information that meets your selection criteria ";
Microsoft. Office. InterOP. Excel. Range myrange22 = myworksheet. get_range ("B2", "B2 ");
Myrange22.font. Bold = true;
Myrange22. Font. size = "20 ";
Myworksheet. cells [3, 1] = "Export Date:" + datetime. Now. tostring ();
}