• 导入excle到DataTable


    public static System.Data.DataTable InputExcel(string Path, ref string exceptionMsg)
            {
                System.Data.DataTable dt = new System.Data.DataTable();
                try
                {
                    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
                    using (OleDbConnection conn = new OleDbConnection(strConn))
                    {
                        conn.Open();
                        System.Data.DataTable sheetDt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        string[] sheet = new string[sheetDt.Rows.Count];
                        for (int i = 0; i < sheetDt.Rows.Count; i++)
                        {
                            sheet[i] = sheetDt.Rows[i]["TABLE_NAME"].ToString();
                        }
                        string strExcel = string.Format("select * from [{0}]", sheet[0]);
                        OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);
                        dt = new System.Data.DataTable();
                        myCommand.Fill(dt);
                        conn.Close();
                    }
                }
                catch (Exception ex)
                {
                    exceptionMsg = ex.Message;
                }
                return dt;
            }
    


    以上转自网上

  • 相关阅读:
    第07组 Alpha冲刺(2/4)
    第07组 Alpha冲刺(1/4)
    团队项目-需求分析报告
    团队项目-选题报告
    1381 硬币游戏
    1381 硬币游戏
    1347 旋转字符串
    1344 走格子
    1305 Pairwise Sum and Divide
    1384 全排列
  • 原文地址:https://www.cnblogs.com/bantongshui/p/3170003.html
Copyright © 2020-2023  润新知