本文介绍了填充DataGrid列瓦特/ VB中Excel数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我终于得到这个code劳苦小时后工作:

 昏暗路径的String = OpenFileDialog1.FileName
        昏暗myDataset作为新的数据集()
        昏暗strConn =新的OleDbConnection(供应商= Microsoft.ACE.Oledb.12.0;数据源=&放大器;路径&安培;扩展属性=创先争优12.0; HDR = YES; IMEX = 1)
        昏暗的myData作为新OleDb.OleDbDataAdapter(SELECT * FROM [表Sheet1 $],创建失败)
        myData.Fill(myDataset)
        DataGridView1.DataSource = myDataset.Tables(0).DefaultView
 

现在我想通了这一点,我要试着将数据放在一个特定的位置。在我的应用程序,我有一个DataGridView设置了4列。我想这样做是Excel中的DataGridView和C列的第1列下的excel把列A File文件,DataGridView中的第二列。

所以替换:

  DataGridView1.DataSource = myDataset.Tables(0).DefaultView
 

  DataGridView1.columns(0)= myDataset.Tables(0).columns(0)
    DataGridView1.columns(1)= myDataset.Tables(0).columns(2)
 

这显然不工作,有什么东西告诉我,我可能需要一个for循环导入数据,但我从来没有进口从Excel文件中的信息之前,并使其更糟的是我从来没有与datagridviews之前,所以我没有知道如何去这件事。

我愿做这样的事情,如果我能:

 对于x = 1到xldoc.rows.length  -  1
            DataGridView1.Item(0,x)的。价值= CTYPE(xlDoc.Cells(0,X + 1),Excel.Range)。文
        下一个
 

解决方案

这最终是比较容易的方式来导入数据。林万一别人发布此遇到这个主题。

 如果OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK然后
        xLApp =新Excel.Application
        xLBook = xLApp.Workbooks.Open(OpenFileDialog1.FileName)
        xLSheet = xLBook.Worksheets(工作表Sheet1)
        对于x = 1至xLSheet.UsedRange.Rows.Count  -  1
            DataGridView1.Rows.Add()
            DataGridView1.Item(0,X  -  1)。价值= xLSheet.Cells(1 + X,1)。价值
            DataGridView1.Item(1,X  -  1)。价值= xLSheet.Cells(1 + X,xLSheet.UsedRange.Columns.Count)。价值
        下一个
    结束如果
 

甚至不费心:

 昏暗myDataset作为新的数据集()
    昏暗strConn =新的OleDbConnection(供应商= Microsoft.ACE.Oledb.12.0;数据源=&放大器;路径&安培;扩展属性=创先争优12.0; HDR = YES; IMEX = 1)
    昏暗的myData作为新OleDb.OleDbDataAdapter(SELECT * FROM [表Sheet1 $],创建失败)
 

Alright, I finally got this code to work after hours of toiling:

        Dim path As String = OpenFileDialog1.FileName
        Dim myDataset As New DataSet()
        Dim strConn = New OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=" & path & ";Extended Properties=""Excel 12.0;HDR=YES;IMEX=1""")
        Dim myData As New OleDb.OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn)
        myData.Fill(myDataset)
        DataGridView1.DataSource = myDataset.Tables(0).DefaultView

Now that I figured that out I was going to try and place the data in a specific location. On my application I have a datagridview set up with 4 columns. What I would like to do is put column A of the excel file under the 1st column of the datagridview and column C of the Excel File in the second column of the datagridview.

So replace:

    DataGridView1.DataSource = myDataset.Tables(0).DefaultView

with:

    DataGridView1.columns(0) = myDataset.Tables(0).columns(0)
    DataGridView1.columns(1) = myDataset.Tables(0).columns(2)

Obviously this doesnt work, and something tells me I might need a for loop to import the data, but I have never imported information from an Excel file before and to make it worse I have never worked with datagridviews before so I have no idea how to go about this.

I would like to do something like this if I could:

        For x = 1 To xldoc.rows.length - 1
            DataGridView1.Item(0, x).Value = CType(xlDoc.Cells(0, x + 1), Excel.Range).Text
        Next
解决方案

This ended up being way easier to import the data. Im posting this in case anyone else comes across this thread.

    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        xLApp = New Excel.Application
        xLBook = xLApp.Workbooks.Open(OpenFileDialog1.FileName)
        xLSheet = xLBook.Worksheets("Sheet1")
        For x = 1 To xLSheet.UsedRange.Rows.Count - 1
            DataGridView1.Rows.Add()
            DataGridView1.Item(0, x - 1).Value = xLSheet.Cells(1 + x, 1).value
            DataGridView1.Item(1, x - 1).Value = xLSheet.Cells(1 + x, xLSheet.UsedRange.Columns.Count).value
        Next
    End If

Dont even bother with:

    Dim myDataset As New DataSet()
    Dim strConn = New OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=" & path & ";Extended Properties=""Excel 12.0;HDR=YES;IMEX=1""")
    Dim myData As New OleDb.OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn)

这篇关于填充DataGrid列瓦特/ VB中Excel数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 22:53
查看更多