zoukankan      html  css  js  c++  java
  • 读取Excel文件到DataTable中

    private static string[] GetExcelSheetNames(OleDbConnection conn)
            {
                DataTable dtbSheets = null;
                String[] arrExcelSheets = null;
                using (conn)
                {
                    try
                    {
                        conn.Open();

                        // Get the data table containing the schema
                        dtbSheets = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });

                        if (dtbSheets == null)
                        {
                            return null;
                        }

                        arrExcelSheets = new String[dtbSheets.Rows.Count];
                        int intI = 0;

                        // Add the sheet name to the string array.
                        foreach (DataRow dr in dtbSheets.Rows)
                        {
                            arrExcelSheets[intI] = dr["TABLE_NAME"].ToString();
                            intI++;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    finally
                    {
                        // Close the connection
                        conn.Close();
                    }
                    return arrExcelSheets;
                }
            }

            private static DataTable GetDataTableFromXls(OleDbConnection conn, string spreadSheetName)
            {
                DataTable datTemp = null;

                using (conn)
                {
                    try
                    {
                        string strComand = "select * from [" + spreadSheetName + "]";

                        conn.Open();
                        OleDbDataAdapter adapter = new OleDbDataAdapter(strComand, conn);
                        datTemp = new DataTable(spreadSheetName);
                        adapter.Fill(datTemp);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    finally
                    {
                        // Close the connection
                        conn.Close();
                    }
                }
                return datTemp;
            }
            // Get the spreadsheet that contain data
            public static DataTable GetDataTableWithData(string serverLocation)
            {
                OleDbConnection xlsConn = null;
                DataTable datXls = null;

                try
                {
                    string strConnStr = null;
                    // Connection string for Excel
                    strConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + serverLocation + ";Extended Properties="Excel 8.0; HDR=NO; IMEX=1"";
                    xlsConn = new OleDbConnection(strConnStr);
                    // Get Sheet names from an Excel Book
                    string[] arrXls = GetExcelSheetNames(xlsConn);

                    try
                    {
                        foreach (string strSheet in arrXls)
                        {
                            xlsConn = new OleDbConnection(strConnStr);
                            datXls = GetDataTableFromXls(xlsConn, strSheet);
                            if (datXls != null && datXls.Rows.Count > 0)
                            {
                                break;
                            }
                        }
                    }
                    catch// (SqlException se)
                    {
                        //throw new Exception(se.Message);
                    }

                    return datXls;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    if (xlsConn.State == ConnectionState.Open)
                    {
                        xlsConn.Close();
                    }
                    xlsConn.Dispose();
                }
            }

    原文转载:http://www.cnblogs.com/moss_tan_jun/archive/2010/08/06/1793747.html

  • 相关阅读:
    网站并发量的计算方法
    Kubernetes-1.16部署之一 ETCD+K8s+Calico
    CentOS 7和Ubuntu 18.04安装Docker
    Tomcat8的kryo序列化方式session共享
    Tomcat启动慢问题解决
    Memcached集群配置
    sed命令使用方法
    python数据分析与算法之五 算法
    python数据分析与算法之四 二叉树和排序二叉树
    python数据分析与算法之三 顺序表和链表
  • 原文地址:https://www.cnblogs.com/meimao5211/p/3293499.html
Copyright © 2011-2022 走看看