0

I am trying to import data from Excel to textbox.

I want import data from Excel saved on my C: disk and i get error with System.NotImplementedException I use library for Excel and Office.

Dim ExcelApp As New Excel.Application
Dim ReteilerWorkbook As New WindowsApp1.ExcelApp.Workbooks.Open("C:\Users\TR\1.xlsx")
Dim retailerWorksheet As New RetailerWorkbook.Sheets(1)

Private Sub ZPlikuToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ZPlikuToolStripMenuItem.Click

    Me.TextBox1.Text = ""
    TextBox1.Text = retailerWorksheet.cells(1, 1).text
End Sub    

I want fill textbox1 with data from cell A1

1 Answers1

0

Here's what I do on VB.NET to import data from Excel:

First, I import Interop on the head of my VB module:

Imports Excel = Microsoft.Office.Interop.Excel

Then I declare the following INSIDE the routine:

Private Sub ...
    Dim ExcelApp As Excel.Application = New Excel.Application
    Dim ReteilerWorkbook As Excel.Workbook = ExcelApp.Workbooks.Open("C:\Users\TR\1.xlsx")
...

Could you try it this way?

To retrieve the cells(1,1) content you don't need to declare the Worksheet. It doesn't compensate the effort. So you could do this simply with:

TextBox1.Text = ReteilerWorkbook.Sheets(1).Cells(1,1).Text

And, as always, don't forget to close the workbook at the end:

ReteilerWorkbook.Close()
Pspl
  • 1,398
  • 12
  • 23
  • It helps with connection with my Excel saved on disk. But, how can i copy data from Cell "A1" to textbox1? My code: TextBox1.Text = retailerWorksheet.cells(1, 1).text Doesn't work. I think that i need another command. Greetings – Daniel Pełka Feb 08 '19 at 15:14
  • Awesome! The last thing, i see that VB "is using" that excel after I close my program. So i can't edit it (Read only mode). How can close excel 1.xlsx with code? – Daniel Pełka Feb 08 '19 at 15:22
  • Okey, maybe you know also how to browse localization of excel instead of constant localization? – Daniel Pełka Feb 08 '19 at 15:29
  • You could look on special folders with the command `Environment.SpecialFolder`. For example, if you're searching on your desktop, the command `Environment.GetFolderPath(Environment.SpecialFolder.Desktop)` will retrieve it's path. Maybe you'll consider to ask another question for your new task? – Pspl Feb 08 '19 at 15:33