Winfrom Export of Excel leverage

Source: Internet
Author: User



Private voidForm1_Load (Objectsender, EventArgs e) { stringSQL2 ="SELECT * from Ss_yb where cwzw like '% time wrong% '"; DataTable DT=dbhelper.getdatatable (SQL2); printall (DT); } Public voidprintall (System.Data.DataTable dt) {//Export to Execl Try { //no data, no execution. if(dt. Rows.Count = =0) //instantiate a Excel.Application objectMessageBox.Show ("no data at this moment! "); Microsoft.Office.Interop.Excel.Application Excel=NewMicrosoft.Office.Interop.Excel.Application (); if(Excel = =NULL) {MessageBox.Show ("cannot create Excel object, Excel may not be installed"); return; } //Add a new workbook, workbook is directly saved, will not pop up the Save dialog box, plus application will pop up the Save dialog box, the value of false will errorExcel. APPLICATION.WORKBOOKS.ADD (true); //let background execution be set to invisible, true if you see Open an Excel, then data is written inExcel. Visible =false; //Generate column header names in Excel for(inti =0; i < dt. Columns.count; i++) {Excel. cells[1, i +1] = dt. Columns[i]. ColumnName;//Output DataGridView Column header } //save data from the current page of DataGridView in Excel if(dt. Rows.Count >0) { for(inti =0; i < dt. Rows.Count; i++)//control Excel line, the distance between the top and bottom, is the number of lines can be to Excel, the data is longer than the error, the data is shorter than the display will not end { for(intj =0; J < dt. Columns.count; J + +)//control the column in Excel, the distance between the left and right, that is, the number of columns to Excel, more than the data length of the error, shorter than the data will be displayed { stringstr =dt. ROWS[I][J]. ToString (); Excel. Cells[i+2, J +1] ="'"+ str;//I control the row, starting from line 2nd in Excel, output the first row of data, J control column, output 1th column data from column 1th in Excel, "'" + is saved as String, so encountered number does not turn into 16 binary } } } //set up a query prompt to suppress pop-up saves and overridesExcel. DisplayAlerts =false; Excel. Alertbeforeoverwriting=false; //Save the workbook with a value of false errorExcel. APPLICATION.WORKBOOKS.ADD (true). Save (); //Save Excel FileExcel. Save ("D:"+"\\KKHMD.xls"); //Make sure the Excel process is closedExcel. Quit (); Excel=NULL; } Catch(Exception) {//killprocess ("EXCEL");//Kill process Excel//int generation = System.GC.GetGeneration (Excel); //excelapp = null;System.GC.Collect (); //MessageBox.Show (ex. Message, "error prompt"); } }

Winfrom Export of Excel leverage

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.