zoukankan      html  css  js  c++  java
  • 读取Excel数据绑定到Gridview进行显示

    读取Excel数据绑定到Gridview进行显示示例代码。

    读取excel代码

    /// <summary>
            /// 读取Excel
            /// authon:codeo.cn
            /// </summary>
            /// <param name="strExcelFileName"></param>
            /// <param name="strSheetName"></param>
            /// <returns>DataTable</returns>
            public static DataTable ExcelToDataTable(string strExcelFileName, string strSheetName)
            {
                //源的定义
                string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
    
                //Sql语句
                //string strExcel = string.Format("select * from [{0}$]", strSheetName); 这是一种方法
                string strExcel = "select * from  [" + strSheetName + "$]";//[sheet1$]
    
                //定义存放的数据表
                DataSet ds = new DataSet();
    
                //连接数据源
                OleDbConnection conn = new OleDbConnection(strConn);
    
                conn.Open();
    
                //适配到数据源
                OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
                adapter.Fill(ds, strSheetName);
    
                conn.Close();
    
                return ds.Tables[strSheetName];
            }

    GridView绑定daTable数据

     protected void Page_Load(object sender, EventArgs e)
            {
                string path = GetRootPath();
                DataTable daTable = ExcelToDataTable(path + "Content\demo.xlsx", "Sheet1");
                GridView1.DataSource = daTable;
                GridView1.DataBind();
            }

    asp.net取得网站根目录的物理路径

    /// <summary>
            /// 取得网站根目录的物理路径
            /// authon:codeo.cn
            /// </summary>
            /// <returns></returns>
            public static string GetRootPath()
            {
                string AppPath = "";
                HttpContext HttpCurrent = HttpContext.Current;
                if (HttpCurrent != null)
                {
                    AppPath = HttpCurrent.Server.MapPath("~");
                }
                else
                {
                    AppPath = AppDomain.CurrentDomain.BaseDirectory;
                    if (Regex.Match(AppPath, @"\$", RegexOptions.Compiled).Success)
                        AppPath = AppPath.Substring(0, AppPath.Length - 1);
                }
                return AppPath;
            }

    补充

    如何修改表头信息?
    只需在绑定数据前将datatable的列名修改。

    daTable.Columns[0].ColumnName = "表头1";
    daTable.Columns[2].ColumnName = "表头2";
    daTable.Columns[3].ColumnName = "表头3";
  • 相关阅读:
    阶段性总结(PHP-JSON)
    阶段性总结(PHP-Array函数)
    JavaScript异步加载的三种方式——async和defer、动态创建script
    event.target 和 event.currentTarget 的区别
    面试题:常用的http状态码
    JS变量重复声明以及忽略var 声明的问题及其背后的原理
    line-height:1.5和line-height:150%的区别
    Web前端性能优化——如何提高页面加载速度
    Promise和setTimeout执行顺序 面试题
    过目不忘JS正则表达式
  • 原文地址:https://www.cnblogs.com/huhangfei/p/4991965.html
Copyright © 2011-2022 走看看