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 // /// summary> // /// Excel file name param> // /// returns DataSet returns> // public dataset importfromexcel (string strexcelfilename) // { // Return DOIMPORT (STREXCELENAME); //} / ** //// /// from the selected Excel file import /// summary> /// Dataset returns> public dataset ImportFromExcel () {DataSet DS = New DataSet (); if (OpenFiledlg.Showdialog () == System.Windows.Forms.DialogResult.ok) DS = doImport (OpenFiledlg.FileName); Return DS;} / **//// / /// Import /// summary> /// Excel file name param> /// <
Returns> returns> Public DataSet ImportFromExcel (String Strfilename) {DataSet DS = New DataSet (); DS = DOIMPORT (STRFILENAME); RETURN DS;} / ** //// /// Execute // / Summary> /// file name param> /// DataSet returns> 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 / ** //// ///// summary> /// DataSet param> /// To export Excel file name param> public void exporttoExcel (Dataset DS, String StrexcelFileName) {IF (DS. Tables.count == 0 || strexcelfilename == "" "Return; doexport (DS, strexcel1ename);
} / ** //// /// Export user-selected Excel file /// summary> /// dataset param> public void ExportToExcel (DataSet DS) {IF (SavefileDlg.showdialog () == System.windows.Forms.DialogResult.ok) DoExport (DS, SaveFiledlg.FileName);} / ** //// // / Export /// Summary> /// DataSet param> /// To export param> 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 / ** //// /// / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 5 () {DataSet ds = new DataSet (); System.Windows.Forms.OpenFileDialog openFileDlg = new System.Windows.Forms.OpenFileDialog (); openFileDlg.DefaultExt = "xml"; openFileDlg.Filter = "xml file (* .xml ) | * .xml "; if (openfiledlg.showdialog () == system.windows.forms.dialogResult.ok) Try {ds.readxml (OpenFiledlg.FileName, System.Data.xmlreadmode.Readschema);} catch {} return ds;} / ** //// //////// < / summary> /// XML file name param> /// returns> public dataset importfromxml (string strfilename) {if (strfilename == ") Return NULL; DataSet DS = New Dataset (); try {ds.readxml (strfilename);