C# 读取EXCEL数据

  
   /// <summary>
/// 读取EXCEL数据
/// </summary>
/// <param name="Path"></param>
/// <returns></returns>
public static System.Data.DataSet ExcelToDS(string Path)
{
string strConn = string.Empty;
string version = Path.Substring(Path.Length - );
if (version.Equals("s"))
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
}
else
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
}
OleDbConnection conn = new OleDbConnection(strConn);
System.Data.DataSet ds = null;
try
{
conn.Open();
//System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new Object[] { null, null, null, "TABLE" }); string tableName = schemaTable.Rows[][].ToString().Trim();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = "select * from [" + tableName + "]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new System.Data.DataSet();
myCommand.Fill(ds, tableName);
return ds;
}
catch (Exception e) { conn.Close(); }
finally { conn.Close(); }
return ds; }
上一篇:9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】


下一篇:Openxml入门---Openxm读取Excel数据