public bool fSampleDataTable(string FilePath){//FilePath 文件的相对路径
try
{
string connection = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + FilePath + ";Extended Properties=Excel 8.0";
OleDbConnection thisconnection = new OleDbConnection(connection);
thisconnection.Open();
DataTable schemaTable = thisconnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
string table = schemaTable.Rows[0][2].ToString().Trim();//获取Excel的表名,默认值是sheet1
string command = "select * from [" + table + "]";
OleDbDataAdapter adapter = new OleDbDataAdapter(command, thisconnection);
DataSet ds = new DataSet();
adapter.Fill(ds, table);
thisconnection.Close();
DataTable MyNewTable = new DataTable();
int i = 0;
foreach (DataRow Row in ds.Tables[table].Rows)
{
if (i == 0) { i++; continue; } // 跳过第一列标题
//下面就是读取文件内容
var aa = row[0];
...;
}
if (_dbSession.SaveChanges() > 0)
return true;
else
return false;
}
catch (Exception)
{
return false;
}
}
如果有疑问的可以给我留言很乐意和大家一起解决困难
QQ :1665375685