zoukankan      html  css  js  c++  java
  • C#操作xls

    public DataSet getData(string p)
            {
                //判断文件后缀
                var path = p;
                string fileSuffix = System.IO.Path.GetExtension(path);
                if (string.IsNullOrEmpty(fileSuffix))
                    return null;
                using (DataSet ds = new DataSet())
                {
                    //判断Excel文件是2003版本还是2007版本
                    string connString = "";
                    if (fileSuffix == ".xls")
                        connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";" + ";Extended Properties="Excel 8.0;HDR=YES;IMEX=1"";
                    else
                        connString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + path + ";" + ";Extended Properties="Excel 12.0;HDR=YES;IMEX=1"";
                    //读取文件
                    string sql_select = " SELECT * FROM [Sheet1$]";
                    using (OleDbConnection conn = new OleDbConnection(connString))
                    using (OleDbDataAdapter cmd = new OleDbDataAdapter(sql_select, conn))
                    {
                        conn.Open();
                        cmd.Fill(ds);
                    }
                    if (ds == null || ds.Tables.Count <= 0) return null;
                    return ds;
                }
            }

  • 相关阅读:
    poj 1328 Radar Installation (贪心)
    hdu 2037 今年暑假不AC (贪心)
    poj 2965 The Pilots Brothers' refrigerator (dfs)
    poj 1753 Flip Game (dfs)
    hdu 2838 Cow Sorting (树状数组)
    hdu 1058 Humble Numbers (DP)
    hdu 1069 Monkey and Banana (DP)
    hdu 1087 Super Jumping! Jumping! Jumping! (DP)
    必须知道的.NET FrameWork
    使用记事本+CSC编译程序
  • 原文地址:https://www.cnblogs.com/ganzhihui/p/10129763.html
Copyright © 2011-2022 走看看