/// <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