static DataSetParse(string fileName)
{
DataSetds = new DataSet();
try
{
//Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0
stringconnectionString = string.Format("provider=Microsoft.ACE.OLEDB.12.0; data source={0};Extended Properties=Excel 12.0;", fileName);
foreach(var sheetName inGetExcelSheetNames(connectionString))
{
using(OleDbConnection con = new OleDbConnection(connectionString))
{
var dataTable = new System.Data.DataTable();
string query = string.Format("SELECT * FROM [{0}]", sheetName);
con.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(query, con);
adapter.Fill(dataTable);
ds.Tables.Add(dataTable);
}
}
}
catch(Exception ex)
{
throwex;
}
returnds;
}
static string[] GetExcelSheetNames(string connectionString)
{
String[] excelSheetNames;
try
{
OleDbConnectioncon = null;
System.Data.DataTable dt = null;
con = newOleDbConnection(connectionString);
con.Open();
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if(dt == null)
{
returnnull;
}
excelSheetNames = new String[dt.Rows.Count];
inti = 0;
foreach(DataRow row indt.Rows)
{
excelSheetNames[i] = row["TABLE_NAME"].ToString();
i++;
}
}
catch(Exception ex)
{
throwex;
}
returnexcelSheetNames;
}
}
No comments:
Post a Comment
Thank you for visiting my blog