C #. net import the query data to an Excel table for output

Source: Internet
Author: User

. Net encapsulates Excel-related classes. The following are events triggered by clicking the "Excel output" button in the tool bar of the program, provided that excel is installed in your system.
Here is my code, with notes:
Private void toolbar1_buttonclick (Object sender, system. Windows. Forms. toolbarbuttonclickeventargs E)
{// Click events for each Toolbar
If (E. Button = excelout)
{
Excel. Application excelkccx = new excel. Application (); // create an Excel Object
Excelkccx. workbooks. Add (true); // create an Excel worksheet

Datatable mydatatable = mydataset. Tables ["inventory information"]; // create a data table to obtain the data in the "inventory information" table of dataset.
Int ROW = 2;

// Input the information of the data table to the Excel table.
For (INT I = 0; I <mydatatable. Columns. Count; I ++) // obtain the field name
{
Excelkccx. cells [1, I + 1] = mydatatable. Columns [I]. columnname. tostring ();
}
For (INT I = 0; I <mydatatable. Rows. Count; I ++) // obtain the record value
{

For (Int J = 0; j <mydatatable. Columns. Count; j ++)
{
Excelkccx. cells [row, J + 1] = mydatatable. Rows [I] [J]. tostring ();
}
Row ++;
}
Excelkccx. Visible = true; // make Excel visible */
}
Else if (E. Button = reportform)
{
// Kcrptform myreport = new kcrptform ();
// Myreport. mdiparent = this;
// Myreport. Show ();
}

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.