读取CSV XLSX XLS文件

xiaoxiao2021-02-27  164

public static DataSet ReadFile(string path, string name)         {             if (string.IsNullOrWhiteSpace(path) || string.IsNullOrWhiteSpace(name) || !File.Exists(path +"\\"+ name))                 return null;             // ?excel             string connstring = string.Empty;             string strSql = string.Empty;             if (name.EndsWith(".xls") || name.EndsWith(".xlsx"))             {                 connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + name + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";                 strSql = "select * from [sheet1$]";             }             // ?csvゅン             else if (name.EndsWith(".csv"))             {                 connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='text;HDR=YES;FMT=Delimited';";                 strSql = "select * from " + name;             }             else             {                 return null;             }             DataSet ds = null;             OleDbConnection conn = null;             try             {                 conn = new OleDbConnection(connstring);                 conn.Open();                 OleDbDataAdapter myCommand = null;                 myCommand = new OleDbDataAdapter(strSql, connstring);                 ds = new DataSet();                 myCommand.Fill(ds, "table1");             }             catch (Exception e)             {                 throw e;             }             finally             {                 conn.Close();             }             return ds;         }
转载请注明原文地址: https://www.6miu.com/read-12262.html

最新回复(0)