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();
                }
            }

  • 相关阅读:
    Linux下软件安装方法即路径设置
    maven和jdk版本不匹配
    jobTracker 和taskTracker
    任务调度quartz
    springside3.1.8打包
    给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组
    二进制求和
    数组中找到目标值,并返回其索引
    加1问题
    给定字符串返回最后一个单词的长度
  • 原文地址:https://www.cnblogs.com/moss_tan_jun/p/1793747.html
Copyright © 2011-2022 走看看