• 读取Excel


      public static class ExcelHelper
        {
            #region 查询Excel
            /// <summary>
            /// 将Excel文件导出至DataTable(第一行作为表头)
            /// </summary>
            /// <param name="ExcelFilePath">Excel文件路径</param>
            /// <param name="TableName">数据表名,如果数据表名错误,默认为第一个数据表名</param>
            public static DataTable GetDataFromExcel(string ExcelFilePath, string TableName = null)
            {
                if (!File.Exists(ExcelFilePath))
                {
                    throw new Exception("Excel文件不存在!");
                }
    
                //如果数据表名不存在,则数据表名为Excel文件的第一个数据表
                List<string> TableList = GetExcelTables(ExcelFilePath);
                if (string.IsNullOrEmpty(TableName) || !TableList.Contains(TableName))
                {
                    TableName = TableList[0].ToString().Trim();
                }
    
                DataTable table = new DataTable();
                OleDbConnection dbcon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExcelFilePath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1' ");
                OleDbCommand cmd = new OleDbCommand("select * from [" + TableName + "$]", dbcon);
                OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
    
                try
                {
                    if (dbcon.State == ConnectionState.Closed)
                    {
                        dbcon.Open();
                    }
                    adapter.Fill(table);
                }
                catch (Exception exp)
                {
                    // throw exp;
                }
                finally
                {
                    if (dbcon.State == ConnectionState.Open)
                    {
                        dbcon.Close();
                    }
                }
    
                return table;
            }
    
            /// <summary>
            /// 获取Excel文件数据表列表
            /// </summary>
            public static List<string> GetExcelTables(string ExcelFileName)
            {
                DataTable dt = new DataTable();
                List<string> TablesList = new List<string>();
                if (File.Exists(ExcelFileName))
                {
                    using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" + ExcelFileName))
                    {
                        try
                        {
                            conn.Open();
                            dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                        }
                        catch (Exception exp)
                        {
                            throw exp;
                        }
    
                        //获取数据表个数
                        int tablecount = dt.Rows.Count;
                        for (int i = 0; i < tablecount; i++)
                        {
                            string tablename = dt.Rows[i][2].ToString().Trim().TrimEnd('$');
                            if (TablesList.IndexOf(tablename) < 0)
                            {
                                TablesList.Add(tablename);
                            }
                        }
                    }
                }
                return TablesList;
            }
            #endregion
        }
    ExcelHelper
            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                //ofd.InitialDirectory = AppPath;
                ofd.ShowDialog();
                string fileUrl = ofd.FileName;
                if (string.IsNullOrEmpty(fileUrl))
                {
                    return;
                }
                try
                {
                    DataTable exclTd = new DataTable();
                    exclTd = ExcelHelper.GetDataFromExcel(fileUrl, "sheet1");
                }
                catch (Exception ex)
                {
    
                }
            }
    调用
  • 相关阅读:
    Webpack4不求人(5) ——编写自定义插件
    Webpack4不求人(4)——编写自定义Loader
    Webpack4不求人(3) ——性能优化
    Webpack4不求人(2) ——手把手搭建TypeScript+React16+ReactRouter5同构应用脚手架
    Shell脚本快速入门(1)
    kafka二进制协议分析与PHP客户端开发
    深入浅出ES6的标准内置对象Proxy
    ES6的Set类型
    深入浅出ES6的迭代器
    Javascript事件系统
  • 原文地址:https://www.cnblogs.com/junhuang/p/6438296.html
Copyright © 2020-2023  润新知