/// <summary>
/// 将Excel文件转成DataSet
/// </summary>
/// <param name="filepath">文件路径</param>
/// <returns></returns>
public static DataSet XlsConvertDataSet(string filepath)
{
DataSet ds = new DataSet();
try
{
string strCon = "";
//获取文件后缀
string gvFileExtension = System.IO.Path.GetExtension(filepath);
//判断是否为Excel文件
if (gvFileExtension == ".xls")
{
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;IMEX=1'";
}
if (gvFileExtension == ".xlsx")
{
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1;\"";
}
System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
string strCom = "SELECT * FROM [Sheet1$]";
Conn.Open();
System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
myCommand.Fill(ds, "[Sheet1$]");
Conn.Close();
}
catch (Exception ex)
{
//ex.Message;
}
return ds;
}