public DataTable GetExcelDataTable(string filePath, string sheetName, bool hasTitle)
{
try
{
DataTable dt = new DataTable();
string strCnn = string.Format("Provider=Microsoft.Ace.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR={1};IMEX=1'",
filePath, (hasTitle ? "Yes" : "No"));
string comand = string.Format("select * from [{0}$] where Name is not null", sheetName);
using (OleDbConnection cnn = new OleDbConnection(strCnn))
using (OleDbDataAdapter adapter = new OleDbDataAdapter(comand, cnn))
{
cnn.Open();
adapter.Fill(dt);
}
return dt;
}
catch
{
return null;
}
}
C#快速读取Excel
最新推荐文章于 2022-11-14 10:53:02 发布