Export data of the datagridview to excel (the sorting format in the datagridview is still retained)

Source: Internet
Author: User

 

/// <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 ();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.