how to import data from excel vb using Microsoft.Office.Interop.Excel - vb.net

Sorry for bad English
i can export data to excel file this code
Imports Excel = Microsoft.Office.Interop.Excel
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlWorkBook = xlApp.Workbooks.Open("MY EXCEL PATH")
xlWorkSheet = xlWorkBook.Sheets("SHEET1")
With xlWorkSheet
.Range("D8").Value = "1"
.Range("D9").Value = "2"
End With
xlApp.Visible = True
I want to import data(D8,D9) from excel file to any text box .Can u help me? Thank

I am not sure where you want to import the data, but this should work and give you the value.
Imports Excel = Microsoft.Office.Interop.Excel
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim strOne As String
Dim strTwo As String
xlWorkBook = xlApp.Workbooks.Open("MY EXCEL PATH")
xlWorkSheet = xlWorkBook.Sheets("SHEET1")
With xlWorkSheet
strOne = .Range("D8").Value 'This will give you the value of cell D8 in strOne
strTwo = .Range("D9").Value 'This will give you the value of cell D9 in strTwo
End With
MsgBox strOne
MsgBox strTwo
'your code continue...

Related

Microsoft.Office.Interop.Excel - How to save Excel (Visible cells only) to CSV in vb.net

Currently I can save Excel to CSV as code below.
However, it's save all data which is including invisible cells.
Dim excelApplication As Microsoft.Office.Interop.Excel.Application
Dim workbook As Microsoft.Office.Interop.Excel.Workbook
excelApplication = New Microsoft.Office.Interop.Excel.Application
excelApplication.Visible = False
excelApplication.DisplayAlerts = False
workbook = excelApplication.Workbooks.Open(in_InputFilePath)
workbook.SaveAs(in_OutputFilePath, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV)
workbook.Close()
excelApplication.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication)
Dim excelApplication As Microsoft.Office.Interop.Excel.Application
Dim workbook1 As Microsoft.Office.Interop.Excel.Workbook
Dim workbook1worksheet1 As Microsoft.Office.Interop.Excel.Worksheet
Dim workbook1worksheet1range1 As Microsoft.Office.Interop.Excel.Range
Dim workbook2 As Microsoft.Office.Interop.Excel.Workbook
Dim workbook2worksheet1 As Microsoft.Office.Interop.Excel.Worksheet
Dim workbook2worksheet1range1 As Microsoft.Office.Interop.Excel.Range
excelApplication = New Microsoft.Office.Interop.Excel.Application
excelApplication.Visible = False
excelApplication.DisplayAlerts = False
workbook1 = excelApplication.Workbooks.Open(in_InputFilePath)
workbook1worksheet1 = CType(workbook1.Sheets("Sheet1"), Microsoft.Office.Interop.Excel.Worksheet)
workbook1worksheet1.Select(Type.Missing)
workbook1worksheet1range1 = workbook1worksheet1.UsedRange.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeVisible, Type.Missing)
workbook1worksheet1range1.Copy
workbook2 = excelApplication.Workbooks.Add()
workbook2worksheet1 = CType(workbook2.Sheets("Sheet1"), Microsoft.Office.Interop.Excel.Worksheet)
workbook2worksheet1range1 = workbook2worksheet1.Range("A1")
workbook2worksheet1range1.PasteSpecial()
workbook2.SaveAs(in_OutputFilePath, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV)
workbook2.Save()
workbook2.Close()
workbook1.Close()
excelApplication.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook2worksheet1range1)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook2worksheet1)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook2)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook1worksheet1range1)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook1worksheet1)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook1)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication)

VB.Net Interop Excel Com Exception

This code was working perfect for quite a while till now,
It throws exception :
System.InvalidCastException: Unable to cast COM object of type
'System.__ComObject' to class type
'Microsoft.Office.Interop.Excel.WorksheetClass'
Dim ds_allJobs As DataSet = DBHandling.searchJob("", "", "all open jobs")
Dim xlApp = New Microsoft.Office.Interop.Excel.Application
xlApp.Visible = False
xlApp.ScreenUpdating = False
Dim xlWorkbook = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet)
Dim xlWorksheet = New Microsoft.Office.Interop.Excel.Worksheet
xlWorksheet = xlWorkbook.ActiveSheet 'IT FAILS HERE
xlWorksheet.Name = "Open Jobs"
Any suggestions?
It is better if you specify the types of objects (e.g. exWorkbook As Excel.Workbook)
Here is a way to make a new worksheet:
'create a new excel application (window)
Dim exApp As New Excel.Application
'add a workbook to it, and take a look at the first worksheet
Dim exWorkbook As Excel.Workbook = exApp.Application.Workbooks.Add()
'create a new worksheet
Dim exWorksheet As Excel.Worksheet
'add this worksheet to the current applicatoin
exWorksheet = CType(exApp.Worksheets.Add(), Excel.Worksheet)
Doe this answer your question? The last line is Casting the object to type worksheet.

Creating Excel Files using VB2010

I have a form with 1 button to create an Excel file on my desktop.
I get error message:
NullReferenceException Was Unhandled
Object reference not set to an instance of an object
and it highlights the code:
WB = excelapp.workbooks.add
I did add the reference "Microsoft excel 14.0" and my full code is below:
imports excel = microsoft.office.interop.excel
dim excelapp as excel.application
dim WB as excel.workbook
sub button1()
WB = excelapp.workbooks.add
excelapp.visible=true
end sub
Missing New on your Excel instance for starters:
Dim xlApp As New Excel.Application
Dim xlWorkbook As Excel.Workbook = xlApp.Workbooks.Add()
Dim xlWorksheet As Excel.Worksheet = CType(xlWorkbook.Sheets("sheet1"), Excel.Worksheet)
xlWorksheet.Cells(1, 1) = "data in first cell"
xlWorksheet.SaveAs(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\" & "Test.xlsx")
xlWorkbook.Close()
xlApp.Quit()
xlApp = Nothing
xlWorkBook = Nothing
xlWorkSheet = Nothing
You should probably put this in Try/Catch/Finally block to catch errors in case you run into issues, but mostly because if the program doesn't continue properly finish that code block, an EXCEL.EXE will remain open in your task manager, as well as whatever Excel file it was accessing will be "in use by another program" when you try to access/modify/delete it.
just add one line, then it'll work
imports excel = microsoft.office.interop.excel
dim excelapp as excel.application
dim WB as excel.workbook
sub button1()
excelapp = new excel.application
WB = excelapp.workbooks.add
excelapp.visible=true
end sub

How to manipulate an excel workbook with Excel.Workbook

So, I can't find any methods with file opening in Excel.Workbook or Excel.Application, and I am wondering why.
I have path to an Excel file, and I would like to manipulate it. I understand I have to use Excel.Application, Excel.Workbook, and Excel.Worksheet. The file to the Excel file is ExcelFilePath, what should I do to make it possible?
You first need to add the reference to Microsoft.Office.Interop.Excel to your project in order to use the Excel API, then fill the variables:
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWS As Excel.Worksheet
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Open(sFilePath)
xlWS = CType(xlWorkBook.Worksheets(sheetNameOrIndex), Excel.Worksheet)
And then you only need to use the methods they have. For example:
Dim sVar as String = xlWS.Range("C5").Value.ToString()
Try this: from my project
Dim xapp As New Microsoft.Office.Interop.Excel.Application
Dim wb As Workbook = xapp.Workbooks.Add
Dim ws As Worksheet = wb.Worksheets(1)
ws.Activate()
'Fill header of the sheet----------------------------------
For i As Integer = 1 To dgvcustomer.Columns.Count
ws.Cells(1, i) = dgvcustomer.Columns(i - 1).HeaderText
Next
'End header------------------------------------------------
Dim Dgrow, Dgcell, Dgcol As Integer
Dgrow = 1
Dgcell = 1
'Fill Sheet ------------------------------------------------------------------------------------------------
While (Dgrow <= dgvcustomer.Rows.Count)
Dgcol = 1
While (Dgcol <= ws.UsedRange.Columns().Count)
ws.Cells(Dgrow + 1, Dgcol).value = dgvcustomer.Rows(Dgrow - 1).Cells(ws.Cells(1, Dgcol).value).Value
Dgcol += 1
End While
Dgrow += 1
End While
'End fill sheet---------------------------------------------------------------------------------------------
wb.SaveAs(dlgSaveFile.FileName)
wb.Close()
xapp.Quit()

Modifying a template based excel in VB.NET

I am tyring to run code like this
xlWorkSheet.Cells(rownumber, 1) = "Some Value"
the problem is when I open the excel document the cell value is not updated.
any help will be appreciated
I use the following code to open the file
Dim pathtofile As String = Server.MapPath("~/UserControls/Upload/MyUpload.xlsx")
Dim xlApp As Excel.Application =New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlworkbooks As Excel.Workbooks
Dim xlWorksheets As Excel.Worksheets
Dim misValue As Object = System.Reflection.Missing.Value
Dim xlWorkSheet As Excel.Worksheet = Nothing
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlworkbooks = xlApp.Workbooks
xlWorkBook = xlworkbooks.Open(pathtofile )
xlWorkSheet = xlWorkBook.Sheets("Sheet1")
You need to save, close, and release the com objects to make sure excel doesn't remain running as a process.
Sub save(filename As String) ' Saves the sheet under a specfic name
xlWorkBook.SaveAs(filename)
End Sub
Sub closexl()
xlWorkBooks.Close()
xlApp.Quit()
End Sub
Sub cleanup() ' Releases all of the com objects to object not have excel running as a process after this method finishes
GC.Collect()
GC.WaitForPendingFinalizers()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
xlApp = Nothing
End Sub