zoukankan      html  css  js  c++  java
  • C#读取Excel,或者多个excel表,返回dataset

    把excel 表作为一个数据源进行读取

     1  /// <summary>
     2         /// 读取Excel单个Sheet
     3         /// </summary>
     4         /// <param name="Path">Excel路径</param>
     5         /// <returns>返回dataset</returns>
     6         public DataSet ExcelToDS(string Path)
     7         {
     8             string filename = "sheet1";//可以指定excel  sheet
     9             string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", Path);
    10             //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
    11             OleDbConnection conn = new OleDbConnection(strConn);
    12             conn.Open();
    13             string strExcel = "";
    14             OleDbDataAdapter myCommand = null;
    15             DataSet ds = null;
    16             strExcel = string.Format("select * from [{0}$]", filename);
    17             myCommand = new OleDbDataAdapter(strExcel, strConn);
    18             ds = new DataSet();
    19             myCommand.Fill(ds, "table1");
    20             return ds;
    21         }
    代码

    一个excel存在多表读取方法

     /// <summary>
            ///  读取Excel多个Sheet
            /// </summary>
            /// <param name="filePath">文件路径</param>
            /// <param name="fileName">文件名称</param>
            /// <returns></returns>
            public static DataSet ToDataTable(string filePath,string fileName)
            {
             
                string connStr = "";
                string fileType = System.IO.Path.GetExtension(fileName);
                if (string.IsNullOrEmpty(fileType)) return null;
    
                if (fileType == ".xls")
                    connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + ";Extended Properties="Excel 8.0;HDR=YES;IMEX=1"";
                else
                    connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + ";Extended Properties="Excel 12.0;HDR=YES;IMEX=1"";
                string sql_F = "Select * FROM [{0}]";
    
                OleDbConnection conn = null;
                OleDbDataAdapter da = null;
                DataTable dtSheetName = null;
    
                DataSet ds = new DataSet();
                try
                {
                    // 初始化连接,并打开
                    conn = new OleDbConnection(connStr);
                    conn.Open();
    
                    // 获取数据源的表定义元数据                        
                    string SheetName = "";
                    dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                    dtSheetName.DefaultView.Sort = "TABLE_NAME";//针对excel进行排序,
                    // 初始化适配器
                    da = new OleDbDataAdapter();
                    for (int i = 1; i < dtSheetName.Rows.Count; i++)
                    {
                        SheetName = (string)dtSheetName.Rows[i]["TABLE_NAME"];
    
                        if (SheetName.Contains("$") && !SheetName.Replace("'", "").EndsWith("$"))
                        {
                            continue;
                        }
    
                        da.SelectCommand = new OleDbCommand(String.Format(sql_F, SheetName), conn);
                        DataSet dsItem = new DataSet();
                        da.Fill(dsItem, SheetName);
    
                        ds.Tables.Add(dsItem.Tables[0].Copy());
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    // 关闭连接
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        da.Dispose();
                        conn.Dispose();
                    }
                }
                return ds;
            }
    读取Excel多个Sheet
    每一份坚持,以后都会有收获
  • 相关阅读:
    机器学习(二)分类决策树
    机器学习(一):朴素贝叶斯
    NLP(五):关键词提取补充(语料库和向量空间)
    oracle变量的定义和使用【转】
    一些开发技巧和工具使用心得
    醒狮赋
    C# 字符串的截取和替换
    C# 判断字符串是否为日期格式
    重装系统
    电脑桌面所有图标消失
  • 原文地址:https://www.cnblogs.com/wilsons/p/5816770.html
Copyright © 2011-2022 走看看