public static System.Data.DataSet ExportWxcel(string filePath)
{
try
{
System.Data.DataSet dataSet = new System.Data.DataSet();
string format = “Provider=Microsoft.Ace.OLEDb.12.0;Data Source='{0}’;Extended Properties=’Excel 12.0;HDR=no;IMEX=1′;”;
format = string.Format(format, filePath);
using (OleDbConnection connection = new OleDbConnection(format))
{
connection.Open();
//获取所有的sheet表
System.Data.DataTable dtSheetName = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, “Table” });
for (int i = 0; i < dtSheetName.Rows.Count; i++)
{
System.Data.DataTable dt = new System.Data.DataTable();
//获取表名
string sheetName = dtSheetName.Rows[i][“TABLE_NAME”].ToString();
if (!dataSet.Tables.Contains(sheetName.Split(‘$’)[0]))
dt.TableName = sheetName.Split(‘$’)[0];
OleDbDataAdapter adapter = new OleDbDataAdapter(“select * from [” + sheetName + “]”, connection);
adapter.Fill(dt);
dataSet.Tables.Add(dt);
}
}
return dataSet;
}
catch (Exception ex)
{
Log.WriteLog(“Excel读取失败!” + ex.Message + “!” + DateTime.Now.ToString(“F”));
return null;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/51887.html