CSharp examples for Office:Excel
Excel To DataSet
using System.Collections.Generic; using System.Text.RegularExpressions; using System.Web; using System.Drawing; using System.Collections; using System.Text; using System.Data; using System.IO;/*from w ww .j a v a 2s . com*/ using System; using System.Data.OleDb; public class Main{ public static DataSet ExcelToDS(string Path,string sheetName) { try { string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); string strExcel = ""; OleDbDataAdapter myCommand = null; DataSet ds = null; strExcel = string.Format("select * from [{0}]", sheetName); ; myCommand = new OleDbDataAdapter(strExcel, strConn); ds = new DataSet(); myCommand.Fill(ds, "table1"); return ds; } catch (System.Data.OleDb.OleDbException ex) { System.Diagnostics.Debug.WriteLine( ex.Message); return null; } } public static DataSet ExcelToDS(string Path) { try { string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); string strExcel = ""; OleDbDataAdapter myCommand = null; DataSet ds = null; strExcel="select * from [Sheet1$]"; myCommand = new OleDbDataAdapter(strExcel, strConn); ds = new DataSet(); myCommand.Fill(ds,"table1"); return ds; } catch(System.Data.OleDb.OleDbException ex) { System.Diagnostics.Debug.WriteLine (ex.Message ); return null; } } }