页面设计了一个fileupload,通过button导入excel到Gridview显示,导入的数据保存到sql server,最好带删除键,删除导入错误的数据!

解决方案 »

  1.   

    protected void btn_导入_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile.FileName != "")
            {
                //定义上传路径(在当前目录下的uploadfile文件下)
                string uploadpath = "c:";
                //取得文件名
                string tmpfilename = FileUpload1.PostedFile.FileName;
                //文件名
                filename = tmpfilename.Substring(tmpfilename.LastIndexOf("\\") + 1);
                //原文件的保存路径
                string fileSavePath = uploadpath + "\\" + filename;
                //保存            
                FileUpload1.SaveAs(fileSavePath);
                //求取后缀名
                string suffix = filename.Substring(filename.LastIndexOf("."));            string url = "C:\\" + filename;
              
                    string Sql = "insert into L_排班表 select * from OPENROWSET ('MICROSOFT.ACE.OLEDB.12.0','Excel 5.0;HDR=YES;DATABASE=" + url + "',sheet1$)";
                    if (dataOperate.execSQL(Sql))
                    {                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('导入成功!');</script>");
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('导入失败!');</script>");
                    }
                }
    }
      

  2.   

    SQL 2005 开启OpenRowset/OpenDatasource的办法 
    1、开始 —> 所有程序  —> Microsoft SQL Server 2005  —> 配置工具  —> SQL Server外围应用配置器  —> 功能的外围应用配置器  —> 实例名  —> Database Engine  —> 即席远程查询  —> 启用OpenRowset和OpenDatasource支持。     2.代码启用 启用:  exec sp_configure 'show advanced options',1 reconfigure  exec sp_configure 'Ad Hoc Distributed Queries',1 Reconfigure  关闭:  exec sp_configure 'Ad Hoc Distributed Queries',0 reconfigure  exec sp_configure 'show advanced options',0 reconfigure 
      

  3.   

    先上传,再导入数据库,再将数据显示在GridView中,这样不行么?实在不行,将上传后的数据存在ViewState中(对页面有点过载),再点保存的时候,将ViewState中的数据存入数据库
      

  4.   

    就跟三楼说的一样,先上传,在导入,然后在显示,代码就是上面的,只不过显示没写 注意下excel版本上传Excel要和数据表结构相同