Excel file Import and export, you need to refer to Microsoft Excel 11.0 Object Library
Purpose:excel file Import and export, you need to refer to Microsoft Excel 11.0 Object Library//author:dangmy//date:2007-03-09//version:1.0/////////////////////////////////////////// public class excelio{private int _returnstatus; private string _returnmessage; <summary>///Execute return status///</summary> public int ReturnStatus {Get{return _returnst ATUs;} }///<summary>//Execute return information///</summary> public string ReturnMessage {Get{return _returnmessage;} Public Excelio () {}//<summary>//import Excel to DataSet///</summary>//<PA Ram name= "filename" >excel full path file name </param>///<returns> Import successful dataset</returns> public DataSet Im Portexcel (String fileName) {//Determines whether Excel excel.application xlapp=new excel.applicationclass () is installed; if (xlapp==null) {_returnstatus =-1; _returnmessage = "Cannot create an Excel object, your computer may not have Excel installed"; return null; }//Determine if the file is used by other processes Excel.Workbook Workbook; try {workbook = XlApp.Workbooks.Open (filename,0, False, 5, "", "", False, Excel.XlPlatform.xlWindows, "", True, False, 0, true, 1, 0); } catch {_returnstatus =-1; _returnmessage = "Excel file is open, please save close"; return null; }//Get all sheet names int n = workbook. Worksheets.count; string[] Sheetset = new String[n]; System.Collections.ArrayList al = new System.Collections.ArrayList (); for (int i=1; i<=n; i++) {sheetset[i-1] = ((excel.worksheet) workbook. Worksheets[i]). Name; }//Releases the Excel related object workbook. Close (Null,null,null); Xlapp.quiT (); if (workbook! = null) {System.Runtime.InteropServices.Marshal.ReleaseComObject (workbook); workbook = null; } if (xlapp! = null) {System.Runtime.InteropServices.Marshal.ReleaseComObject (xlapp); xlapp = null; } GC. Collect (); Import Excel into the DataSet DataSet ds = new DataSet (); String connstr = "Provider = microsoft.jet.oledb.4.0; Data Source = "+ FileName +"; Extended Properties=excel 8.0 "; using (OleDbConnection conn = new OleDbConnection (CONNSTR)) {Conn. Open (); OleDbDataAdapter da; for (int i=1; i<=n; i++) {string-sql = "SELECT * FROM [" + sheetset[i-1] + "$]"; da = new OleDbDataAdapter (sql,conn); Da. Fill (Ds,sheetset[i-1]); Da. Dispose (); } conn. Close (); CoNn. Dispose (); } return DS; }///<summary>///Export the DataTable to Excel///</summary>//<param name= "ReportName" > Report Name & lt;/param>//<param name= "DT" > Data source table </param>//<param name= "savefilename" >excel full path file name </p aram>//<returns> Export Success </returns> public bool Exportexcel (string reportname,datatable dt,string s Avefilename) {if (dt==null) {_returnstatus =-1; _returnmessage = "The DataSet is empty!" "; return false; } bool Filesaved=false; Excel.Application xlapp=new Excel.applicationclass (); if (xlapp==null) {_returnstatus =-1; _returnmessage = "Cannot create an Excel object, your computer may not have Excel installed"; return false; } excel.workbooks Workbooks=xlapp.workbooks; Excel.Workbook Workbook=workbooks. ADD (Excel.XlWBATemplate.xlWBATWorksheet); ExcEl. Worksheet worksheet= (excel.worksheet) workbook. worksheets[1];//obtained Sheet1 worksheet. Cells.Font.Size = 10; Excel.Range Range; Long Totalcount=dt. Rows.Count; Long rowread=0; float percent=0; Worksheet. Cells[1,1]=reportname; ((excel.range) worksheet. cells[1,1]). Font.Size = 12; ((excel.range) worksheet. cells[1,1]). Font.Bold = true; Writes the field for (int i=0;i<dt. columns.count;i++) {worksheet. Cells[2,i+1]=dt. Columns[i]. ColumnName; Range= (excel.range) worksheet. CELLS[2,I+1]; Range. Interior.ColorIndex = 15; Range. Font.Bold = true; }//write value for (int r=0;r<dt. rows.count;r++) {for (int i=0;i<dt. columns.count;i++) {worksheet. Cells[r+3,i+1]=dt. Rows[r][i]. ToString (); } rowread++; Percent= (float) (100*rowread))/totalcount; } range=worksHeet.get_range (worksheet. Cells[2,1],worksheet. Cells[dt. Rows.count+2,dt. Columns.count]); Range. Borderaround (Excel.xllinestyle.xlcontinuous,excel.xlborderweight.xlthin, Excel.xlcolorindex.xlcolorindexautomatic,null); if (dt. Rows.Count > 0) {range. Borders[excel.xlbordersindex.xlinsidehorizontal]. ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic; Range. Borders[excel.xlbordersindex.xlinsidehorizontal]. LineStyle =excel.xllinestyle.xlcontinuous; Range. Borders[excel.xlbordersindex.xlinsidehorizontal]. Weight =excel.xlborderweight.xlthin; } if (dt. columns.count>1) {range. Borders[excel.xlbordersindex.xlinsidevertical]. ColorIndex =excel.xlcolorindex.xlcolorindexautomatic; Range. Borders[excel.xlbordersindex.xlinsidevertical]. LineStyle = Excel.XlLineStyle.xlContinuous; Range. Borders[excel.xlbordersindex.xlinsidevertical]. Weight = Excel.XlBorderWeight.xlThin; } Save the file if (savefilename!= "") {try {workbook. Saved =true; Workbook. SaveCopyAs (Savefilename); Filesaved=true; } catch (Exception ex) {filesaved=false; _returnstatus =-1; _returnmessage = "An error occurred while exporting the file, the file may be being opened!" \ n "+ex. Message; }} else {filesaved=false; }//releases Excel corresponding object if (range = null) {System.Runtime.InteropServices.Ma Rshal. ReleaseComObject (range); range = null; } if (worksheet! = null) {System.Runtime.InteropServices.Marshal.ReleaseComObject (worksheet); worksheet = null; } if (workbook! = null) {System.Runtime.InteropServices.Marshal.ReleaseComObject (workbook); workbook = null; } if(Workbooks! = null) {System.Runtime.InteropServices.Marshal.ReleaseComObject (workbooks); workbooks = null; } xlApp.Application.Workbooks.Close (); xlApp.Quit (); if (xlapp! = null) {System.Runtime.InteropServices.Marshal.ReleaseComObject (xlapp); xlapp = null; } GC. Collect (); return filesaved; }}//The code snippet is from: http://www.sharejs.com/codes/csharp/7261
C # import the generic class code that exports data to Excel