导入Excel到GridView

        string filepath = FileUploadExcel.PostedFile.FileName;//取得路径
        if (filepath != "")
        {
            string filename = filepath.Substring(filepath.LastIndexOf("//") + 1);//文件名称带后缀名
            string name = filename.Substring(0, filename.Length - 4);//文件名不带后缀名
            string fileEx = filepath.Substring(filepath.LastIndexOf(".") + 1);//后缀名
            if (string.Equals(fileEx, "xls"))
            {
                string newName = Server.MapPath("App_Data//") + DateTime.Now.ToString("hhmmss") + ".xls";
                FileUploadExcel.SaveAs(newName);
                string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + newName + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1';";
                OleDbConnection conn = new OleDbConnection(connStr);
                if (conn.State.ToString() == "Closed")
                {
                    conn.Open();
                }
                OleDbDataAdapter oda = new OleDbDataAdapter("select F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11 from [Sheet1$]", conn);
                DataSet ds = new DataSet();
                oda.Fill(ds, "aa");
                conn.Close();
                ds.Tables[0].Rows.RemoveAt(0);//移除标题
                ds.Tables[0].Rows.RemoveAt(ds.Tables[0].Rows.Count - 3);
                ds.Tables[0].Rows.RemoveAt(ds.Tables[0].Rows.Count - 2);
                ds.Tables[0].Rows.RemoveAt(ds.Tables[0].Rows.Count - 1);
                gvInfor.DataSource = ds.Tables[0];
                gvInfor.DataBind();
                rsn.FillgvInfo(ds,"1",gvInfor);
                File.Delete(newName);
            }
            else
            {
                rsn.showmessage("File type is not correct!", this.Page);
                return;
            }
        }
        else
        {
            rsn.showmessage("please select file to import!", this.Page);
            return;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值