假如我想把Excel中的某一工作表中的内容显示在DataGrid中怎么办呢?请帮忙?

解决方案 »

  1.   

    Private Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImport.Click
            Try
                '定义连接字符串
                Dim strConn As String
                Dim strPath As String
                strPath = Me.txtFileName.Text.ToString.Replace("\", "\\")
                strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strPath + ";Extended Properties=Excel 8.0;"            '连接文件,取Excel数据到DataSet中
                Dim connection As New OleDb.OleDbConnection(strConn)            '取得 Excel workSheet 的名字
                Dim xlWorkSheetName As String
                xlWorkSheetName = GetExcelWorkSheet(excel文件路径包括文件名)            '填充数据集
                Dim adapter As New OleDb.OleDbDataAdapter("select * from [" + xlWorkSheetName + "$]", connection)
                Dim ds As New DataSet
                adapter.Fill(ds, "Excel")            '关闭连接
                connection.Close()
                '绑定dataGrid
                Me.dgData.DataSource = ds.Tables("Excel").DefaultView            '强制垃圾回收,用于kill Excel Process
                GC.Collect()        Catch err As Exception
                '捕获错误信息;弹出,结束
                MsgBox(err.Message, MsgBoxStyle.OKOnly)
                Return
            End Try
        End Sub    Function GetExcelWorkSheet(ByVal sPath As String) As String
            '返回给定路径 Excel 文件的 workSheet 名字
            Dim xlApp As New Excel.Application
            Dim xlWorkSheet As New Excel.Worksheet
            Dim xlName As String        '得到worksheet名字
            xlApp.Workbooks.Open(sPath)
            xlWorkSheet = xlApp.Worksheets.Item(1)
            xlName = xlWorkSheet.Name        '关闭excel处理
            xlApp.Workbooks.Close()
            xlApp.Quit()
            xlWorkSheet = Nothing
            xlApp = Nothing        '返回名字字符串
            Return xlName
        End Function
      

  2.   

    xlWorkSheet = xlApp.Worksheets.Item(1)
    为什么Worksheets没有Item属性呢?
      

  3.   

    Function GetExcelWorkSheet(ByVal sPath As String) As String
            '返回给定路径 Excel 文件的 workSheet 名字
            Dim xlApp As New Excel.Application
            Dim xlWorkSheet As New Excel.Worksheet
            Dim xlName As String        '得到worksheet名字
            xlApp.Workbooks.Open(sPath)
            xlWorkSheet = xlApp.Worksheets.Item(1)
            xlName = xlWorkSheet.Name        '关闭excel处理
            xlApp.Workbooks.Close()
            xlApp.Quit()
            xlWorkSheet = Nothing
            xlApp = Nothing        '返回名字字符串
            Return xlName
        End Function
    用c#怎么写?
      

  4.   

    string GetExcelWorkSheet(string sPath)
    {       // '返回给定路径 Excel 文件的 workSheet 名字
            Excel.Application xlApp = new Excel.Application();
            Excel.Worksheet xlWorkSheet = newExcel.Worksheet();
            string xlName;
            //'得到worksheet名字
            xlApp.Workbooks.Open(sPath);
            xlWorkSheet = xlApp.Worksheets.Item[1];
            xlName = xlWorkSheet.Name;        //'关闭excel处理
            xlApp.Workbooks.Close();
            xlApp.Quit();
            xlWorkSheet = null;
            xlApp = null;
            //'返回名字字符串
            return xlName;
    }
      

  5.   

    xlApp.Workbooks.Open(sPath); //重载“open”方法未获取“1”参数
    xlWorkSheet = xlApp.Worksheets.Item[1];//Worksheets中无Item属性
      

  6.   

    你的vb.net的程序可以运行吗?
      

  7.   

    Tonet_lover(孟子E章) :那段代码在vb.net中可以用行的,在c#不知怎么修改?