Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 5th, 2007, 09:28 AM
Registered User
Join Date: Dec 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Read an already open Excel file in VB.NET

Hi Guys,

I want to read an excel file in VB.NET. This excel file is always open and is constantly updated.

The code I have used is given below.

Dim objExcelApp As New Excel.Application
        Dim objWorkBook As Excel.Workbook = objExcelApp.Workbooks.Open("c:\ReadExcel.xls")
        Dim objWorkSheet As Excel.Worksheet = objWorkBook.Worksheets.Item(1)
        Dim objRng As Excel.Range
        Dim strCol, strCell As String
        Dim maxCol, maxRow As Integer

        Dim iRow, iCol As Integer

        maxRow = 2

        maxCol = 2

        objWorkBook = objExcelApp.Workbooks.Open("c:\ReadExcel.xls")

        objWorkSheet = objWorkBook.Worksheets(1)

        objExcelApp.Visible = True

        For iCol = 1 To maxCol 
            For iRow = 1 To maxRow 
                strCol = Chr(Asc(iRow) + 16)
                strCell = strCol + iCol.ToString

                objRng = objWorkSheet.Range(strCell)

 I have a few issues with this code.

1. Since I want to read an already open excel sheet I don't want to open it again. Here the OPEN command opens another excel sheet which I don't want.

2. In this code the columns & rows are hard coded, but in my case the excel sheet can have any number of rows & columns.
I want to loop till the end of the file.

Can anybody Please help me on an urgent basis.

Thanks in advance

Old March 28th, 2007, 05:45 AM
Registered User
Join Date: Jan 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

Hi Sunil,

Did u get any solution for giving dynamically the rows with data ?

Please let me know @ deepti.premkumar@gmail.com


Old April 4th, 2007, 06:41 AM
Registered User
Join Date: Apr 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Hi Sunil

You can use connection string and connect to excel

ConnectionString: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1";

SQL Query: SQL syntax "SELECT * FROM [sheet1$]". I.e. excel worksheet name followed by a "$" and wrapped in "[" "]" brackets.

If nothing going Right
Turn Left
Old August 27th, 2008, 12:28 AM
Posts: n/a

you can do it with OLEDB update


or ttry this

Old February 28th, 2014, 07:20 AM
Registered User
Join Date: Feb 2014
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default RE: Read an already open Excel file in VB.NET

Hi, I had a similar requirements and here is what I used:
- First I set up a FileSystemWatcher to monitor XLS file changes.
- Second read every sheet and every sheets cell in the targeted excel file.

Here is a code for first part:
    Private watcher As New FileSystemWatcher()

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        watcher.Path = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)
        watcher.Filter = "*.xls"
        watcher.NotifyFilter = NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.Size Or NotifyFilters.FileName

        AddHandler watcher.Changed, AddressOf OnChanged
        AddHandler watcher.Renamed, AddressOf OnChanged

        watcher.EnableRaisingEvents = True
    End Sub
And for the second part I used this VB.NET excel component which can greatly simplify reading of excel files in .NET. Here is a code:
    Private Sub OnChanged(source As Object, e As FileSystemEventArgs)
        Dim file As ExcelFile = ExcelFile.Load(e.FullPath)

        For Each sheet As ExcelWorksheet In file.Worksheets
            Dim range As CellRange = file.Worksheets.ActiveWorksheet.GetUsedCellRange(True)

            For i As Integer = range.FirstColumnIndex To range.LastColumnIndex
                For j As Integer = range.FirstRowIndex To range.LastRowIndex

                    Dim cell As ExcelCell = range(j - range.FirstRowIndex, i - range.FirstColumnIndex)
                    Dim cellName As String = CellRange.RowColumnToPosition(j, i)
                    Dim cellRow As String = ExcelRowCollection.RowIndexToName(j)
                    Dim cellColumn As String = ExcelColumnCollection.ColumnIndexToName(i)

                    ' Do something with cell's information.
    End Sub

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to open excel file thru vb.net sreeramulu Javascript 5 December 6th, 2013 04:19 AM
connecting Excel file in VB.NET 2005 vish_vj ADO.NET 1 March 1st, 2008 10:37 AM
Read Text file and convert to Binary file VB.net sjlsysprg1 Pro VB.NET 2002/2003 4 June 29th, 2007 06:53 AM
how can read excel file abrahamchempanal RSS and Atom 0 February 22nd, 2006 10:52 AM
Read text file and out to serial port VB.net noomkmitl Pro VB.NET 2002/2003 0 December 27th, 2004 08:44 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.