Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 5th, 2007, 09:28 AM
Registered User
 
Join Date: Dec 2006
Location: Mumbai, Maharashtra, India.
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.


Code:
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)
             msgbox 
            Next
        Next



 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




Reply With Quote
  #2 (permalink)  
Old March 28th, 2007, 05:45 AM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Sunil,

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

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

Deepti



Reply With Quote
  #3 (permalink)  
Old April 4th, 2007, 06:41 AM
Registered User
 
Join Date: Apr 2007
Location: Pune, Maharastra, India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #4 (permalink)  
Old August 27th, 2008, 12:28 AM
hu_yang
Guest
 
Posts: n/a
Default

you can do it with OLEDB update

http://vb.net-informations.com/excel...data_oledb.htm

or ttry this

http://vb.net-informations.com/excel..._open_file.htm
Reply With Quote
  #5 (permalink)  
Old February 28th, 2014, 07:20 AM
Registered User
Points: 21, Level: 1
Points: 21, Level: 1 Points: 21, Level: 1 Points: 21, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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:
Code:
    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:
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.
                Next
            Next
        Next
    End Sub
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:37 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.