string strConn;
        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=E:\\民政厅救灾系统\\080522\\minzhengjiuzai\\ExcelTemp\\FileName.xls;" + "Extended Properties=Excel 8.0;";
        OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [FileName$]", strConn);
        DataSet myDataSet = new DataSet();
        myCommand.Fill(myDataSet, "ExcelInfo");
        GridView1.DataSource = myDataSet.Tables["ExcelInfo"].DefaultView;
        GridView1.DataBind();
代码我是这样写的,为什么不报错,但是gridview里没有数据呢?Excel种是有数据的

解决方案 »

  1.   


    可能是你 读取的 excel 有问题,private void Form1_Load(object sender, System.EventArgs e)
    {
    DataTable myT=ExcelToDataTable("D:/文件/新武昌站点资料.xls","sheet1");
    String mystr=myT.Rows[0][0].ToString();
    this.textBox1.Text=mystr;
    }public static DataTable ExcelToDataTable(string strExcelFileName, string strSheetName)
    {
    //源的定义
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.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  [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];
    }
      

  2.   

    Data Source=E:\\民政厅救灾系统\\080522\\minzhengjiuzai\\ExcelTemp\\FileName.xls  
    这个的路径不对吧?一个"\"就可以了吧
      

  3.   

    真实的例子,本机通过,使用html上传控件
    protected void btnReadData_Click(object sender, EventArgs e)
        {
            if (File1.Value.Length > 1)
            {
                string fileName = System.IO.Path.GetFileName(File1.PostedFile.FileName);
                string filePath = "";
                if (this.File1.Value == "")
                {
                    // Comm.Jscript.Alert("请先选择您要导入的文件!");  
                }
                else
                {
                    int index = fileName.LastIndexOf(".");
                    if (index > 0)
                    {
                        if (fileName.Substring(index) == ".xls")
                        {
                            DateTime now = DateTime.Now;
                            fileName = now.ToShortDateString() + now.ToLongTimeString();
                            fileName = fileName.Replace("-", "").Replace(":", "").Replace("   ", "");
                            filePath = @"../uploads/" + fileName + ".xls";
                            this.File1.PostedFile.SaveAs(Server.MapPath(filePath));                    }
                        else
                        {
                            //Comm.Jscript.Alert("读入的文件不是XLS");  
                        }
                    }
                }
                if (filePath != "")
                {
                    fileName = Request.MapPath(filePath);
                    // string    strConn ="Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + Dir + "\\"+fileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1;\" 
                    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
                    string query = "SELECT   *   FROM   [Sheet1$]";
                    OleDbCommand oleCommand = new OleDbCommand(query, new OleDbConnection(strConn));
                    OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);
                    DataSet myDataSet = new DataSet();                //   将   Excel   的[Sheet1]表内容填充到   DataSet   对象  
                    try
                    {
                        oleAdapter.Fill(myDataSet, "[Sheet1$]");
                        //   数据绑定  
                        this.dgExportProject.DataSource = myDataSet;
                        this.dgExportProject.DataMember = "[Sheet1$]";
                        this.dgExportProject.DataBind();                    this.dgExportProject.Visible = true;
                        this.btnSave.Visible = true;
                    }
                    catch (Exception exx)
                    {
                        Response.Write(exx.Message);
                        //Comm.Jscript.Alert("注意:请用默认的Sheet1$页名称!");  
                    }
                    finally
                    {
                        if (File.Exists(filePath))
                        {
                            File.Delete(filePath);
                        }
                    }
                }        }
            else
            {
                Response.Write(" <script> alert('请选择正确的路径!') </script> ");
            } 
        }
      

  4.   

    请问 如果 sql server服务器在远程的机子上,那有怎么导入呢?
      

  5.   

    问题已经解决,是我在页面中多加了一个AutoGenerateColumns="False",把这句话去掉就好了!