zoukankan      html  css  js  c++  java
  • 读取excel 获取excel工作簿名

      static System.Data.DataTable dt = new System.Data.DataTable(); 

    /// <summary>
            /// 读取数据源
            /// </summary>
            /// <param name="sExcelFile"></param>
            /// <param name="dgBom"></param>
            public void ReadExcel(string sExcelFile)
            {
                try
                {
                    dt.Clear();
                    dt_date.Clear();
                    System.Data.DataTable ExcelTable;
                    DataSet ds = new DataSet();
                    //Excel的连接
                    string strconn = "provider=microsoft.jet.oledb.4.0;" + "data source=" + sExcelFile + ";" + "extended properties=excel 8.0;";
                    OleDbConnection objConn = new OleDbConnection(strconn);
                    objConn.Open();
                    System.Data.DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
                    string tableName = schemaTable.Rows[0][2].ToString().Trim();//获取 Excel 的表名,默认值是sheet1
                    string strSql = "select * from [" + tableName + "]";
                    OleDbDataAdapter myData = new OleDbDataAdapter(strSql, objConn);
                    myData.Fill(ds, tableName);//填充数据
                    ExcelTable = ds.Tables[tableName];
                    dt = ds.Tables[tableName];
                    //dgBom.DataSource = ExcelTable;
                    //dgBom.DataBind();
                    objConn.Close();
                }
                catch
                {
                    try {

                        dt.Clear();
                        dt_date.Clear();
                        System.Data.DataTable ExcelTable;
                        DataSet ds = new DataSet();
                        //Excel的连接
                        string strconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sExcelFile + ";Extended Properties=\"Excel 12.0;HDR=YES\"";
                        OleDbConnection objConn = new OleDbConnection(strconn);
                        objConn.Open();
                        System.Data.DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
                        string tableName = schemaTable.Rows[0][2].ToString().Trim();//获取 Excel 的表名,默认值是sheet1
                        string strSql = "select * from [" + tableName + "]";
                        OleDbDataAdapter myData = new OleDbDataAdapter(strSql, objConn);
                        myData.Fill(ds, tableName);//填充数据
                        ExcelTable = ds.Tables[tableName];
                        dt = ds.Tables[tableName];
                        //dgBom.DataSource = ExcelTable;
                        //dgBom.DataBind();
                        objConn.Close();
                    }
                    catch(Exception ex)
                    {
                        Common.Message.Show(ex.Message, this);
                    }
                }
            }

  • 相关阅读:
    20个实用便捷的CSS3工具、库及实例
    jquery插件推荐
    Jquery遮罩插件,想罩哪就罩
    font-size:100%有什么作用
    工作笔记:移动web页面前端开发总结
    移动web页面前端开发总结2
    移动端web开发
    移动WEB前端小结
    移动WEB开发常用技巧
    js 移动web 开发
  • 原文地址:https://www.cnblogs.com/zhang9418hn/p/2571043.html
Copyright © 2011-2022 走看看