Dear all,
I'm trying to import data from an excel file into a windowsforms and specifically here into a datagridview.
I already set up the datagridview in my windowsform and I have a specific range in excel from where I should get the populated cells.
I've tried with two different codes but anyone of them is giving me any result:
Code 1:
Code 2:Code:Imports Microsoft.Office.Interop Imports Microsoft.Office.Interop.Excel Public Class Form14 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim xlAppP As Excel.Application Dim xlWBP As Excel.Workbook Dim xlWSP As Excel.Worksheet Dim fNameP As String If OpenFileDialog1.ShowDialog = DialogResult.OK Then TextBox1.Text = OpenFileDialog1.FileName End If fNameP = TextBox1.Text xlAppP = New Excel.Application Try xlWBP = xlAppP.Workbooks.Open(fNameP, [ReadOnly]:=True) Catch ex As Exception Stop End Try xlWSP = xlWBP.Worksheets(1) xlAppP.Visible = False Dim loadarrayMarc(,) As Object = xlWSP.Range("C24:E29").Value For i As Integer = 0 To 1 For j As Integer = 0 To 1 If i = 0 Then DataGridView1.Rows.Add() End If DataGridView1.Rows(j).Cells(i).Value = loadarrayMarc(i, j) Next 'Next
Please note that not all the cells in the range C24:E29 could be populated, and also that in the excel source columns C and D are merged.Code:Imports Microsoft.Office.Interop Imports Microsoft.Office.Interop.Excel Public Class Form14 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim xlAppP As Excel.Application Dim xlWBP As Excel.Workbook Dim xlWSP As Excel.Worksheet Dim fNameP As String If OpenFileDialog1.ShowDialog = DialogResult.OK Then TextBox1.Text = OpenFileDialog1.FileName End If fNameP = TextBox1.Text xlAppP = New Excel.Application Try xlWBP = xlAppP.Workbooks.Open(fNameP, [ReadOnly]:=True) Catch ex As Exception Stop End Try xlWSP = xlWBP.Worksheets(1) xlAppP.Visible = False Dim loadarrayMarc(,) As Object = xlWSP.Range("C24:E29").Value DataGridView1.DataSource = loadarrayMarc
Could you please help me?
Thanks,
A.




Reply With Quote
