zoukankan      html  css  js  c++  java
  • ASP.net连接Excel

    首先添加命名空间

    using System.Data.OleDb;   

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                strfile = Request.QueryString["filename"];//从其他页面传过来的文件路径
                Excel(strfile);
            }
        }

    private void Excel(string filepath)
        {
            try
            {
                Dataset ds = new DataSet();

                string ConnStr = "Provider=Microsoft.Jet.OleDb.4.0;data source=" + filepath +
                                 ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";//连接Excel的字符串
                string query = "SELECT   *   FROM   [student$]";//Excel中的表名称

                OleDbCommand oleCommand = new OleDbCommand(query, new OleDbConnection(ConnStr));
                OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);
                oleAdapter.Fill(ds, "[student$]");
                rowcount = ds.Tables[0].Rows.Count;
                gridview1.DataSource = ds;
                gridview1.DataBind();

                lblmes.Text = "上传成功,数据如下所示,请确认:";//lblmes为label,显示提示信息
            }
            catch (OleDbException)
            {
                string filename = filepath.Substring(filepath.LastIndexOf('/') + 1);
                lblmes.Text = "错误!请确认上传文件是否正确!当前上传的文件为:" + filename;
                lbtnSure.Visible = false;
            }

           catch(Exception ee)
            {
                lblmes.Text = ee.Message;
            }
        }

  • 相关阅读:
    es6
    ES6
    ES6
    css3
    滚动穿透的6种解决方案【已自测】
    css特效
    css布局
    css布局
    js
    【消灭代办】第5周
  • 原文地址:https://www.cnblogs.com/kangjin0828/p/1686899.html
Copyright © 2011-2022 走看看