CSharp examples for Office:Excel
Excel To Data Table
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;// w w w . j a v a 2 s . com using System; using System.Data.OleDb; public class Main{ public static DataTable ExcelToDataTable(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"); DataTable dt = null; if (ds.Tables.Count > 0) { dt = ds.Tables[0]; } return dt ; } catch (System.Data.OleDb.OleDbException ex) { System.Diagnostics.Debug.WriteLine( ex.Message); return null; } } }