Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading values from an Excel File

Tags:

excel

vb.net

I want to get a value from 12 excel sheet. is there any way that i get the values without opening the excel sheet? I am using vb.net. Please post an example code, if there is a way to read values without opening the excel file. thanks

like image 457
reggie Avatar asked Jan 22 '10 14:01

reggie


People also ask

How do I extract a value from an Excel spreadsheet?

In order to extract data from Excel columns, you can use some combination of the VLOOKUP, MATCH, and INDEX functions. The VLOOKUP function is perhaps best equipped for data extraction, allowing you to look up and retrieve data from a specific column.

Which of the following method is used to read data from Excel files?

Excel file can be read by Java IO operation. For that, we need to use Apache POI Jar. There are two kinds of a workbook in Excel file, XLSX and XLS files. POI has different Interfaces Workbook, Sheet, Row, Cell.

Can you pull data from a closed Excel file?

Extracting data from a closed file in another workbook is a common request by most of the excel user. They would like to pull or consolidate data from closed files; however, this is not possible.


1 Answers

You can't read the values without opening the Excel file at all. But you may read the values without having to open Excel.

If the file is saved in the xml format it's going to be easier. If not, the easiest method is to still use Excel but use Office Automation to do it. The hard way is to create an excel file parser - quite hard on the non-open xml excel format (pre Office 2003) - hard but still possible.

However, it is quite impossible to read from an excel spreadsheet without opening the file at all..

Here's a snippet of code you could use to open a spreadsheet from VB.NET, by leveraging Office Automation (it still opens the file, an relies on Excel automation dlls, but doesn't require opening Excel):

DISCLAIMER

The following code is not intended to be used as is, but merely it is a sample to guide the reader to their own solution which should be thoroughly tested.

' The code below requires you to add references to Office Interop assemblies
' into your VB.NET project  (if you don't know how to do that search Google)

xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Open("<YOUR EXCEL SPREADSHEET FILE HERE")
xlWorkSheet = xlWorkBook.Worksheets("sheet1")

range = xlWorkSheet.UsedRange

For rCnt = 1 To range.Rows.Count
    For cCnt = 1 To range.Columns.Count
        Obj = CType(range.Cells(rCnt, cCnt), Excel.Range)
        ' Obj.value now contains the value in the cell.. 
    Next
Next
like image 199
Mike Dinescu Avatar answered Sep 22 '22 14:09

Mike Dinescu