473,418 Members | 1,749 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.

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 2859
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.