Wimform export data Excel

Source: Internet
Author: User

When exporting data version 2, I simplified the method and passed the test. However, I still haven't figured it out when I click Cancel. Please wait...

Reference:

// Connect to the database --- global
Sqlconnection conn = new sqlconnection (configurationmanager. connectionstrings ["loginthree"]. tostring ());

/// <Summary> /// export data button /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void button2_click (Object sender, eventargs e) {// string sqlall = "select * from student"; datatable dat = testone (sqlall); printall (DAT );}

Testone () method:

/// <Summary> /// obtain the data returned from the dataset to the database // </Summary> /// <Param name = "SQL"> </param> /// <returns> datatable </returns> private datatable testone (string sqlall) {try {Conn. open (); dapter = new sqldataadapter (sqlall, Conn); // automatically generates a single table command to coordinate the changes made to dataset with the changes made to the associated SQL Server database. This class cannot be inherited. Sqlcommandbuilder SCB = new sqlcommandbuilder (dapter); DS = new dataset (); dapter. fill (DS); // dapter. fill (DS, "class"); // datatable mermertable = DTC ["product"]; return Ds. tables [0];} catch (exception e) {console. writeline ("{0} exception caught. ", e) ;}finally {Conn. close ();} return NULL ;}

Printall method:

    
/// <Summary> /// export Excel method 1 /// </Summary> /// <Param name = "DT"> </param>
Public void printall (system. data. datatable DT) {// export to execl // if no data exists, the IF (DT. rows. count = 0) return; // instantiate an Excel file. application Object Microsoft. office. interOP. excel. application Excel = new Microsoft. office. interOP. excel. application (); // excel. application Excel = new excel. application (); // Add a new Workbook. The workbook is saved directly. The save dialog box is not displayed. The save dialog box is displayed when the application is added. If the value is false, an error occurs. Microsoft. office. interOP. excel. workbook xls_book = excel. workbooks. add (Microsoft. office. interOP. excel. xlwbatemplate. xlwbatworksheet); excel. visible = true; try {// set the background execution to invisible. If it is true, an Excel file is opened and the data is written in the Excel file. visible = false; // generate the column header name in Excel for (INT I = 0; I <DT. columns. count; I ++) {excel. cells [1, I + 1] = dagvtwo. columns [I]. headertext; // output the first name of the datagridview column} // Save the data on the current page of The datagridview in Excel if (DT. rows. count> 0) {for (INT I = 0; I <DT. rows. count; I ++) {for (Int J = 0; j <DT. columns. count; j ++) {string STR = DT. rows [I] [J]. tostring (); excel. cells [I + 2, J + 1] = "'" + STR ;}}// you can specify a prompt box for saving and overwriting files in Excel. displayalerts = false; excel. alertbeforeoverwriting = false; // Save the workbook. If it is set to false, an error occurs in Excel. application. workbooks. add (true ). save (); // Save the Excel file. save ("d :\\ kkhmd.xls");} catch (exception ex) {MessageBox. show (ex. message, "error message");} finally {// make sure that the Excel process is disabled. quit (); Excel = NULL ;}

}

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.