• Excel导入数据库,兼容Excel2003,2007


            public static System.Data.DataSet ExcelConnection(string filepath, string exName, string tableName, out string errmsg)
            {
                errmsg = "";
                string strCon = "";
                if (exName == ".xls")
                    strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
                if (exName == ".xlsx")
                    strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0;HDR=YES\"";

                System.Data.OleDb.OleDbConnection ExcelConn = new System.Data.OleDb.OleDbConnection(strCon);
                try
                {
                    ExcelConn.Open();
                    DataTable dt = ExcelConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
                    if (dt == null)
                        return null;
                    string excelSheet = dt.Rows[0]["TABLE_NAME"].ToString();

                    string strCom = string.Format("SELECT * FROM [" + excelSheet + "]");

                    System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, ExcelConn);
                    DataSet ds = new DataSet();
                    myCommand.Fill(ds, tableName);
                    ExcelConn.Close();
                  
                    return ds;
                }
                catch(Exception ex)
                {
                    errmsg = ex.Message;
                    ExcelConn.Close();
                    return null;
                }
            }

  • 相关阅读:
    查漏补缺:QT入门
    添砖加瓦:设计模式(工厂方法模式)
    Luogu 4784 [BalticOI 2016 Day2]城市
    Luogu 1606 [USACO07FEB]白银莲花池Lilypad Pond
    Luogu 3698 [CQOI2017]小Q的棋盘
    CF547D Mike and Fish
    Luogu 3066 [USACO12DEC]逃跑的BarnRunning Away From…
    Luogu 2403 [SDOI2010]所驼门王的宝藏
    NEERC17 J Journey from Petersburg to Moscow
    Luogu 3350 [ZJOI2016]旅行者
  • 原文地址:https://www.cnblogs.com/fhuafeng/p/2628566.html
Copyright © 2020-2023  润新知