C # operation Excel!

xiaoxiao2021-03-06  117

public class ImportExportToExcel {private string strConn; private System.Windows.Forms.OpenFileDialog openFileDlg = new System.Windows.Forms.OpenFileDialog (); private System.Windows.Forms.SaveFileDialog saveFileDlg = new System.Windows.Forms.SaveFileDialog (); Public importexportToExcel () {// // Todo: Add constructor logic // this.openfiledlg.defaultExt = "xls"; this.openfiledlg.filter = "Excel file (* .xls) | * .xls"; This.savefiledlg.defaultExt = "xls"; this.savefiledlg.filter = "Excel file (* .xls) | * .xLs";} Import from Excel file to DataSet # Region import from Excel file to dataset /////

// /// imported from Excel file // /// // /// Excel file name // /// returns DataSet // public dataset importfromexcel (string strexcelfilename) // { // Return DOIMPORT (STREXCELENAME); //} / ** //// /// from the selected Excel file import /// /// Dataset public dataset ImportFromExcel () {DataSet DS = New DataSet (); if (OpenFiledlg.Showdialog () == System.Windows.Forms.DialogResult.ok) DS = doImport (OpenFiledlg.FileName); Return DS;} / **//// / /// Import /// /// Excel file name /// <

Returns> Public DataSet ImportFromExcel (String Strfilename) {DataSet DS = New DataSet (); DS = DOIMPORT (STRFILENAME); RETURN DS;} / ** ////

/// Execute // / /// file name /// DataSet private dataset doimport (string strhuang) {if (strfilename == ") Return null; strConn = "Provider = Microsoft.Jet.OLEDB.4.0;" "Data Source =" strFileName ";" "Extended Properties = Excel 8.0;"; OleDbDataAdapter ExcelDA = new OleDbDataAdapter ( "SELECT * FROM [Sheet1 $] ", Strconn); DataSet Excelds = new dataset (); try {excellda.fill (ExceLDS," Excelinfo ");} catch (exception err) {system.console.writeline (Err.Tostring ());} Re Turn Excelds;} #ENDREGON from DataSet to Excel # Region From DataSet to Excel / ** //// ///// /// DataSet /// To export Excel file name public void exporttoExcel (Dataset DS, String StrexcelFileName) {IF (DS. Tables.count == 0 || strexcelfilename == "" "Return; doexport (DS, strexcel1ename);

} / ** ////

/// Export user-selected Excel file /// /// dataset public void ExportToExcel (DataSet DS) {IF (SavefileDlg.showdialog () == System.windows.Forms.DialogResult.ok) DoExport (DS, SaveFiledlg.FileName);} / ** //// // / Export /// /// DataSet /// To export Private Void DoExPort (Dataset DS, String Street " ) {Excel.Application Excel = new Excel.Application (); // Excel.Workbook Obj = new Excel.WorkbookClass (); // Obj.saves ("c: / zn.xls", Excel.xlfileformat.xlexcel9795, null , NULL, FALSE, FALSE, EXCEL.XLSAVEACCESSMODE.XLNOCHANGE, NULL, NULL, NULL, NULL); int RowIndex = 1; int coLINDEX = 0; Excel.Application.workbooks.add (TRUE); System.Data.DataTable Table = DS.Tables [0]; Foreach (Datacolumn Col in Table.columns) {Colindex ; Excel.cells [1, Colindex] = col.columnname;} Foreach (DataRow Row In Table.Rows) { RowIndex ; ColIndex = 0; Foreach (Datacolumn COL in Table.columns) {Colindex ; Excel.cells [RowIndex, ColIndex] = Row [Col.columnname] .tostring ();

}} Excel.Visible = false; excel.Sheets [0] = "sss"; excel.ActiveWorkbook.SaveAs (strExcelFileName ". XLS", Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange , NULL, NULL, NULL, NULL); //wkbnew.saveas strbookname //excel.save (STREXCELFILENAME); Excel.quit (); Excel = NULL; gc.collect (); // Garbage Recycling} #ENDREGON from XML Import to DataSet # region from XML import to dataset / ** ////

/// //////// < / summary> /// XML file name /// public dataset importfromxml (string strfilename) {if (strfilename == ") Return NULL; DataSet DS = New Dataset (); try {ds.readxml (strfilename);

转载请注明原文地址:https://www.9cbs.com/read-125617.html

New Post(0)