WinForm Append sheet to Excel

Source: Internet
Author: User

/// <summary>        ///Add a sheet to an existing Excel table through a DataTable/// </summary>        /// <param name= "DT" >DataTable with Data set</param>        /// <param name= "Tofilename" >the path and name of the existing Excel</param>        /// <param name= "strSheetName" >The name of the sheet to be added</param>        Private voidDoexport (DataTable DT,stringTofilename,stringstrSheetName) {Microsoft.Office.Interop.Excel.Application Excel=NewMicrosoft.Office.Interop.Excel.Application ();//EXECL Operation class//read object to save targetMicrosoft.Office.Interop.Excel.Workbook bookdest =Excel. Workbooks._open (Tofilename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value            , Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); //open the workbook to which you want to export the Execl file. --ps: About the role of missing class here, I do not know ... EmbarrassingMicrosoft.Office.Interop.Excel.Worksheet sheetdest = BookDest.Worksheets.Add (Missing.Value, Missing.Value, Missing.Value, Missing.Value) asMicrosoft.Office.Interop.Excel.Worksheet; //Add a sheet to the workbookSheetdest.name = strSheetName;//Define your name O (∩_∩) o haha ~            intRowIndex =1; intColindex =0; Excel. APPLICATION.WORKBOOKS.ADD (true);//I don't know if I can get an error.Microsoft.Office.Interop.Excel.Range Range =NULL; foreach(DataColumn Colinchdt. Columns) {Colindex++; sheetdest.cells[1, Colindex] = Col. ColumnName;//in the first column of the EXECL, the column names of the DataTable are piloted inSheetdest.get_range (sheetdest.cells[1,1], sheetdest.cells[1Dt. Columns.count]). HorizontalAlignment =Microsoft.Office.Core.XlHAlign.xlHAlignCenterAcrossSelection; Range= (Microsoft.Office.Interop.Excel.Range) sheetdest.cells[1, Colindex]; Range. Borderaround (Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColor Index.xlcolorindexautomatic,NULL); Range. Font.Bold=true;//Bold Body            }             #region            //Import Data Rows//foreach (DataRow row in dt. Rows)//{            //rowindex++; //colindex = 0; //foreach (DataColumn col in dt. Columns)//    {            //colindex++; //Sheetdest.cells[rowindex, Colindex] = Row[col. ColumnName].            ToString (); //    }            //}            #endregion             for(inti =0; i < dt. Rows.Count; i++) {DataRow row=dt.                Rows[i];  for(intj =0; J < dt. Columns.count; J + +) {DataColumn col=dt.                    COLUMNS[J]; Sheetdest.cells[i+2, J +1] =Row[col. ColumnName].                    ToString (); Range= (Microsoft.Office.Interop.Excel.Range) Sheetdest.cells[i +2, J +1]; Range. Borderaround (Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlC Olorindex.xlcolorindexautomatic,NULL); }} sheetDest.Columns.EntireColumn.AutoFit ();//self-adapting column widths            if(Tofilename.tolower (). Contains ("Wish") {sheetdest.get_range (sheetdest.cells[1,1], Sheetdest.cells[dt. Rows.Count +1Dt. Columns.count-5]). HorizontalAlignment =Microsoft.Office.Core.XlHAlign.xlHAlignCenterAcrossSelection; }            Else{sheetdest.get_range (sheetdest.cells[1,1], Sheetdest.cells[dt. Rows.Count +1Dt. Columns.count-4]). HorizontalAlignment =Microsoft.Office.Core.XlHAlign.xlHAlignCenterAcrossSelection; } bookdest.saved=true; Bookdest.save (); Excel.            Quit (); Excel=NULL; Gc. Collect ();//Garbage Collection}

WinForm Append sheet to Excel

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.