472,954 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Reading Excel dates into C# come out as "weird" ints - help?

Hi,

I'm using an adaptation of excel-reading code that's all over the
internet - I don't much like or understand it, but it has worked for me
in the past.... beggars can't be choosers... :
Excel.Application excelObj = new Excel.Application();
Excel.Workbook theWorkbook = excelObj.Workbooks.Open(path+filename,
0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false,
false, 0, true, true, true);
Excel.Sheets sheets = theWorkbook.Worksheets;
Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);

//This is an incredibly ghetto solution.
//Need to learn more about the Excel api
//and make this code reasonable.
int row = 2;
while (row<100)
{
string xlValue = worksheet.get_Range("A" +
row.ToString(), "A" + row.ToString()).Cells.Value2.ToString();
MessageBox.Show("Here you go: " + xlValue);
row++;
}

The file being read has its first column a bunch of dates (when you
look at it in excel). Here are the first few values of the column in
Excel, along with the first couple contents of the MessageBox above:

Excel MessageBox
11/30/2002 - 37590
12/31/2002 - 37621
1/31/2003 - 37652
2/28/2003 - 37680
3/31/2003 - 37711
4/30/2003 - 37741
5/31/2003 - 37772
6/30/2003 - 37802
7/31/2003 - 37833

I say these ints are "weird" because when I try to cast xlValue above
as an int, I get a "cast not valid" error. WTF? Same deal when I try to
cast xlValue as a DateTime.

All I want to do is search the date column for a particular date and
then get some values out of other columns once a date-match is found.

What can I do here?

Thanks for any advice,

cdj

Nov 10 '06 #1
2 2834
the xl date is the number of days since the 01/01/1900 00:00:00 (on a mac its
1904) so the best way to get the date is to use the Text property of the cell
which will give you the string displayed in it (you can then DateTime.Parse
this) or
new DateTime(1900,1,1).AddDays(xlValue);

I think the casting issue is becuase its a double or a decimal or something
as it can have .000's. Look at is quick watch and it should tell you the
underlying type. If all else fails,
double xlValued = 0.0;
Double.TryParse(xlValue, NumberStyles.Any, out xlValued);

HTH

Ciaran O'Donnell
"sherifffruitfly" wrote:
Hi,

I'm using an adaptation of excel-reading code that's all over the
internet - I don't much like or understand it, but it has worked for me
in the past.... beggars can't be choosers... :
Excel.Application excelObj = new Excel.Application();
Excel.Workbook theWorkbook = excelObj.Workbooks.Open(path+filename,
0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false,
false, 0, true, true, true);
Excel.Sheets sheets = theWorkbook.Worksheets;
Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);

//This is an incredibly ghetto solution.
//Need to learn more about the Excel api
//and make this code reasonable.
int row = 2;
while (row<100)
{
string xlValue = worksheet.get_Range("A" +
row.ToString(), "A" + row.ToString()).Cells.Value2.ToString();
MessageBox.Show("Here you go: " + xlValue);
row++;
}

The file being read has its first column a bunch of dates (when you
look at it in excel). Here are the first few values of the column in
Excel, along with the first couple contents of the MessageBox above:

Excel MessageBox
11/30/2002 - 37590
12/31/2002 - 37621
1/31/2003 - 37652
2/28/2003 - 37680
3/31/2003 - 37711
4/30/2003 - 37741
5/31/2003 - 37772
6/30/2003 - 37802
7/31/2003 - 37833

I say these ints are "weird" because when I try to cast xlValue above
as an int, I get a "cast not valid" error. WTF? Same deal when I try to
cast xlValue as a DateTime.

All I want to do is search the date column for a particular date and
then get some values out of other columns once a date-match is found.

What can I do here?

Thanks for any advice,

cdj

Nov 10 '06 #2

Ciaran O''Donnell wrote:
the xl date is the number of days since the 01/01/1900 00:00:00 (on a mac its
1904) so the best way to get the date is to use the Text property of the cell
which will give you the string displayed in it (you can then DateTime.Parse
this) or
new DateTime(1900,1,1).AddDays(xlValue);

I think the casting issue is becuase its a double or a decimal or something
as it can have .000's. Look at is quick watch and it should tell you the
underlying type. If all else fails,
double xlValued = 0.0;
Double.TryParse(xlValue, NumberStyles.Any, out xlValued);

HTH
Thanks! It helped immensely! It's bizarre that you still have to cast
the cell's Text property as as string, but whatever.

Is it just me, or is dealing with an Excel sheet in c# incredibly
arcane?

Thanks again for your help, and the background info!

cdj

Nov 10 '06 #3

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

Similar topics

7
by: Ryan Park | last post by:
Hi, //SITUATION I got a panel control that hold a certain position on a form. Every controls or UIs are on this panel. At certain situation, I called dispose() method of this panel control...
4
by: Mike D | last post by:
I know everyone says their problem is weird so ... I have a weird problem. I am trying to save an asp file as an excel spreadsheat. The table displays fine in html but when I add...
0
by: Svet | last post by:
CreateObject("Excel.Application") on NT 4.0 hangs and I have to kill the Access 97 app that it is running on. The same code works fine as a stand alone code. This occurs on users' machines thatt...
86
by: Randy Yates | last post by:
In Harbison and Steele's text (fourth edition, p.111) it is stated, The C language does not specify the range of integers that the integral types will represent, except ot say that type int may...
5
by: Jesee | last post by:
I am reading Jeffrey Richter's book "Applied Microsoft .NET Framework programming",i came across "Exception handing". Page 405 says "If the stack overflow occurs within the CLR itself,your...
8
by: John Brock | last post by:
I am creating an Excel workbook using VB.NET, and have run into a problem. Excel at times insists on reformatting data that I enter into cells, e.g., converting "01234" to "1234", and this screws...
2
by: Diffident | last post by:
Hello All, I just finished reading an interesting article by Scott about App Domains: http://odetocode.com/Articles/305.aspx Scott, I have a question about the section "Shadow Copies and...
1
by: webgirl | last post by:
Hi everyone, I have a weird problem with some Word/Excel automation code that I run from Access (not sure if I should therefore post this in the Access forum..? Thought I'd try here first) ...
1
by: nguyentrongkha | last post by:
I have two asp.net applications that host on server 2003. I create a hyper link on one application that links to default page of the other application.Whenever users click on that link, I get a...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.