473,418 Members | 2,075 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Get Value in Excel Spreadsheet From C#

I am opening an Excel Workbook from C# (VS2005) and I need to get a value
from a Spreadsheet before closing the C# application. Does anyone know if
this can be done? Here is the code to open the Workbook:
//Open the Excel Spreadsheet
Microsoft.Office.Interop.Excel.Application excelApp = new
Microsoft.Office.Interop.Excel.ApplicationClass();
excelApp.Visible = true;
string workbookPath = @"\\server\path$\AssignmentTest.xls";
Microsoft.Office.Interop.Excel.Workbook excelWorkbook =
excelApp.Workbooks.Open(workbookPath,
0, false, 5, "password", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindow s, "",
true, false, 0, true, false, false);
--
Robert Hill

Aug 6 '07 #1
4 9323
On 6 Aug, 14:44, Robert <rhill...@hotmail.comwrote:
I am opening an Excel Workbook from C# (VS2005) and I need to get a value
from a Spreadsheet before closing the C# application. Does anyone know if
this can be done? Here is the code to open the Workbook:
//Open the Excel Spreadsheet
Microsoft.Office.Interop.Excel.Application excelApp = new
Microsoft.Office.Interop.Excel.ApplicationClass();
excelApp.Visible = true;
string workbookPath = @"\\server\path$\AssignmentTest.xls";
Microsoft.Office.Interop.Excel.Workbook excelWorkbook =
excelApp.Workbooks.Open(workbookPath,
0, false, 5, "password", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindow s, "",
true, false, 0, true, false, false);
--
Robert Hill
Worksheet w = (Worksheet)excelApp.ActiveSheet;
Range r = (Range)w.Cells[Row, Column];
string s = (string) r.Text

where Row and Column are int's

Worksheet w = (Worksheet)excelApp.ActiveSheet;
Range r = (Range)w.Cells.get_Range(aRange, _missing);
string s = (string) r.Text

where aRange could be "A1" for example.

you've also got
r.Value2 which returns the range's value
r.Formula for the formula obviously

and less obvious r.set_Value to set the value.

Aug 6 '07 #2
Thanks for the response. My Workbook has numerous sheets and I need to get
the values in column A of the "Security" sheet. I can set a worksheet
variable like this:

Worksheet w = (Worksheet)excelApp.Sheets["Security"];

Now that I have 'w' I can't seem to get the right syntax to read any values
in column A.
--
Robert Hill

"DeveloperX" wrote:
On 6 Aug, 14:44, Robert <rhill...@hotmail.comwrote:
I am opening an Excel Workbook from C# (VS2005) and I need to get a value
from a Spreadsheet before closing the C# application. Does anyone know if
this can be done? Here is the code to open the Workbook:
//Open the Excel Spreadsheet
Microsoft.Office.Interop.Excel.Application excelApp = new
Microsoft.Office.Interop.Excel.ApplicationClass();
excelApp.Visible = true;
string workbookPath = @"\\server\path$\AssignmentTest.xls";
Microsoft.Office.Interop.Excel.Workbook excelWorkbook =
excelApp.Workbooks.Open(workbookPath,
0, false, 5, "password", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindow s, "",
true, false, 0, true, false, false);
--
Robert Hill

Worksheet w = (Worksheet)excelApp.ActiveSheet;
Range r = (Range)w.Cells[Row, Column];
string s = (string) r.Text

where Row and Column are int's

Worksheet w = (Worksheet)excelApp.ActiveSheet;
Range r = (Range)w.Cells.get_Range(aRange, _missing);
string s = (string) r.Text

where aRange could be "A1" for example.

you've also got
r.Value2 which returns the range's value
r.Formula for the formula obviously

and less obvious r.set_Value to set the value.

Aug 6 '07 #3
On 6 Aug, 16:46, Robert <rhill...@hotmail.comwrote:
Thanks for the response. My Workbook has numerous sheets and I need to get
the values in column A of the "Security" sheet. I can set a worksheet
variable like this:

Worksheet w = (Worksheet)excelApp.Sheets["Security"];

Now that I have 'w' I can't seem to get the right syntax to read any values
in column A.
--
Robert Hill

"DeveloperX" wrote:
On 6 Aug, 14:44, Robert <rhill...@hotmail.comwrote:
I am opening an Excel Workbook from C# (VS2005) and I need to get a value
from a Spreadsheet before closing the C# application. Does anyone know if
this can be done? Here is the code to open the Workbook:
//Open the Excel Spreadsheet
Microsoft.Office.Interop.Excel.Application excelApp = new
Microsoft.Office.Interop.Excel.ApplicationClass();
excelApp.Visible = true;
string workbookPath = @"\\server\path$\AssignmentTest.xls";
Microsoft.Office.Interop.Excel.Workbook excelWorkbook =
excelApp.Workbooks.Open(workbookPath,
0, false, 5, "password", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindow s, "",
true, false, 0, true, false, false);
--
Robert Hill
Worksheet w = (Worksheet)excelApp.ActiveSheet;
Range r = (Range)w.Cells[Row, Column];
string s = (string) r.Text
where Row and Column are int's
Worksheet w = (Worksheet)excelApp.ActiveSheet;
Range r = (Range)w.Cells.get_Range(aRange, _missing);
string s = (string) r.Text
where aRange could be "A1" for example.
you've also got
r.Value2 which returns the range's value
r.Formula for the formula obviously
and less obvious r.set_Value to set the value.- Hide quoted text -

- Show quoted text -
Well column A is column one, so the easiest way would be to do:

for(row = 1; row< 10; row++) // loop through first 10 rows excel is
mainly 1 based.
{
Range r = (Range)w.Cells[row, 1];
string s = (string) r.Text;
}

Alternatively you can

do the Range r = (Range)w.Cells.get_Range("A:A", _missing);

method where _missing is
private static object _missing = System.Reflection.Missing.Value;
in my code.
You can then iterate through all the cells in the returned range.

The easiest way to get your head around it is to go into excel and
record a macro. 9 times out of 10 it's pretty much a case of using the
same methods, although take care with some of the setters and getters
as mentioned above.

Hope that helps.

Aug 6 '07 #4
Thanks so much. This is exactly what I needed.
--
Robert Hill

"DeveloperX" wrote:
On 6 Aug, 16:46, Robert <rhill...@hotmail.comwrote:
Thanks for the response. My Workbook has numerous sheets and I need to get
the values in column A of the "Security" sheet. I can set a worksheet
variable like this:

Worksheet w = (Worksheet)excelApp.Sheets["Security"];

Now that I have 'w' I can't seem to get the right syntax to read any values
in column A.
--
Robert Hill

"DeveloperX" wrote:
On 6 Aug, 14:44, Robert <rhill...@hotmail.comwrote:
I am opening an Excel Workbook from C# (VS2005) and I need to get a value
from a Spreadsheet before closing the C# application. Does anyone know if
this can be done? Here is the code to open the Workbook:
//Open the Excel Spreadsheet
Microsoft.Office.Interop.Excel.Application excelApp = new
Microsoft.Office.Interop.Excel.ApplicationClass();
excelApp.Visible = true;
string workbookPath = @"\\server\path$\AssignmentTest.xls";
Microsoft.Office.Interop.Excel.Workbook excelWorkbook =
excelApp.Workbooks.Open(workbookPath,
0, false, 5, "password", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindow s, "",
true, false, 0, true, false, false);
--
Robert Hill
Worksheet w = (Worksheet)excelApp.ActiveSheet;
Range r = (Range)w.Cells[Row, Column];
string s = (string) r.Text
where Row and Column are int's
Worksheet w = (Worksheet)excelApp.ActiveSheet;
Range r = (Range)w.Cells.get_Range(aRange, _missing);
string s = (string) r.Text
where aRange could be "A1" for example.
you've also got
r.Value2 which returns the range's value
r.Formula for the formula obviously
and less obvious r.set_Value to set the value.- Hide quoted text -
- Show quoted text -

Well column A is column one, so the easiest way would be to do:

for(row = 1; row< 10; row++) // loop through first 10 rows excel is
mainly 1 based.
{
Range r = (Range)w.Cells[row, 1];
string s = (string) r.Text;
}

Alternatively you can

do the Range r = (Range)w.Cells.get_Range("A:A", _missing);

method where _missing is
private static object _missing = System.Reflection.Missing.Value;
in my code.
You can then iterate through all the cells in the returned range.

The easiest way to get your head around it is to go into excel and
record a macro. 9 times out of 10 it's pretty much a case of using the
same methods, although take care with some of the setters and getters
as mentioned above.

Hope that helps.

Aug 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Phil Powell | last post by:
What would one best recommend to parse an existing Excel spreadsheet (was done in Excel 97 or 2000 not sure to be honest)? I am looking for the most practical way of parsing an existing...
13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
7
by: Hugh McLaughlin | last post by:
Hello Everyone and thanks for your help in advance. I am working on an application that requires the parsing of an Excel spreadsheet that will be loaded into a SQL Server table. An example of...
1
by: Howard Weiss | last post by:
I would like to set a particular cell in an Excel Spreadsheet to a specific value Visual Basic Equivalent would be Cells(row, column) = x where row and column locate the cell and x is the...
6
by: syvman | last post by:
Hi everyone... I am pulling my hair out trying to do this, and was wondering if someone could give me some assistance... I have an Excel spreadsheet containing several worksheets. I'd like to be...
3
by: Scott M. Lyon | last post by:
I'm trying to figure out a way to export data (actually the result of a Stored Procedure call from SQL Server) into a specified Excel spreadsheet format. Currently, I have the data read into a...
1
by: annu124 | last post by:
Hi all, I am trying to write a vb script to export data from txt files in a particular folder in each row in excel spreadsheet. the data in a.txt is 1 2 3 4
0
by: ssrirao | last post by:
There is an Excel Spreadsheet containing data, residing in an internet site. It’s very easy to Import data from a local Excel Spreadsheet into SQL Server Database Table using DTS. But in my case...
1
by: Sport Girl | last post by:
Hi everybody , i have the task of developing in Perl a script that retrieves data from 3 tables ( bugs, profiles, products) from a MySQL database called bugs and display them in an excel sheet...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.