• 读取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)
                {
    
                }
            }
    调用
  • 相关阅读:
    实时监听输入框值变化的完美方案:oninput & onpropertychange
    展示两行,超出用。。。表示
    修改下拉框滚动条样式
    js单线程工作
    锚点
    火狐图片乱码
    解决重复提交的几种方法
    forward和redirect的区别
    form表单刷新自动提交
    addEventListener和attachEvent的区别
  • 原文地址:https://www.cnblogs.com/junhuang/p/6438296.html
Copyright © 2020-2023  润新知