LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading date from Excel with LabWindows CVI

Hello,
I am trying to read a date cell from Excel 2003 with CVI 7.0.0.
 
Using 'CA_VariantHasDate' give error.
Using 'CA_VariantGetDate' give error.
 
The only thing that seems to work is 'CA_VariantGetDouble' but the double value returned does not make any sense to me.
 
All help will be most appreciated.
 
Best regards
Kurt Jakobsen
 
0 Kudos
Message 1 of 9
(4,541 Views)

Hi Kurt,

Could you send the value that you read from Excel as double?
It may look meaningless but the date and time values may be kept as a single number giving, for example, the number of seconds since some reference time in the past.
CVI has some time format conversion functions some of which are in the ANSI library. That value you got from Excel might be meaningful for one of them.
Just "surf" a little in the function libraries.

If you post the value I can help more.

S. Eren BALCI
IMESTEK
0 Kudos
Message 2 of 9
(4,535 Views)

Hello Eblaci

Thanks for your reply, I've tried a bit further without result, so I've attached my project. Make your project path C:\Source\PdYieldToDb and it should work. The excel file is Test.xls The call to the cell is from the main function.

Thanks again for your help!

best regards

Kurt

0 Kudos
Message 3 of 9
(4,526 Views)

Hi Kurt,

I just need the double value, I mean a single number. Not the whole project.
I cannot run your project because I do not have CVI installed in this machine.
You can simply debug your code and copy the number you got from Excel into the post.

S. Eren BALCI
IMESTEK
0 Kudos
Message 4 of 9
(4,523 Views)

Hello Ebalci,

the double value returned is 39184. The value in the date cell is 12.04.2007.

best regards

Kurt

0 Kudos
Message 5 of 9
(4,522 Views)

I think that is the number of days since 1st Jan 1900.

JR

0 Kudos
Message 6 of 9
(4,515 Views)

Hi Kurt,

A little search in CVI, Excel help and Google gives the following results:

  • What you get from Excel (39184) is actually a date in Excel serial format.
  • It gives the number of days since 1 January 1900. 39184 is meaningful since a rough calculation 39184/365 = 107.353... Which means 2007 is about 107 years after 1900.
  • CVI can convert the number of "seconds" since 1 Jan 1900 to a day-month-year (DMY) format.
  • The following link has the function to convert Excel serial format to DMY format: http://www.codeproject.com/datetime/exceldmy.asp

The link has the source code in it. But it would give compiler errors in CVI when copied directly. I tried to "clean" the code in a text editor (since I do not have CVI right now) and I hope it will compile. I paste the code below.
You paste it in a file and just call it with necessary parameters.

Please let me know if something goes wrong so I can edit the post.

Note: You may also check that link to verify the relation between Excel and Gregorian calendar formats http://www.calendarhome.com/converter/

void ExcelSerialDateToDMY(int nSerialDate, int *nDay, int *nMonth, int *nYear)
{
    int l, n, i, j;
   
    // Excel/Lotus 123 have a bug with 29-02-1900. 1900 is not a
    // leap year, but Excel/Lotus 123 think it is...
    if (nSerialDate == 60)
    {
        (*nDay)    = 29;
        (*nMonth)    = 2;
        (*nYear)    = 1900;

        return;
    }
    else if (nSerialDate < 60)
    {
        // Because of the 29-02-1900 bug, any serial date
        // under 60 is one off... Compensate.
        nSerialDate++;
    }

    // Modified Julian to DMY calculation with an addition of 2415019
    l = nSerialDate + 68569 + 2415019;
    n = (int)(( 4 * l ) / 146097);
    l = l - (int)(( 146097 * n + 3 ) / 4);
    i = (int)(( 4000 * ( l + 1 ) ) / 1461001);
    l = l - (int)(( 1461 * i ) / 4) + 31;
    j = (int)(( 80 * l ) / 2447);
    (*nDay) = l - (int)(( 2447 * j ) / 80);
    l = (int)(j / 11);
    (*nMonth) = j + 2 - ( 12 * l );
    (*nYear) = 100 * ( n - 49 ) + i + l;
}

Message Edited by ebalci on 04-17-2007 04:28 PM

S. Eren BALCI
IMESTEK
0 Kudos
Message 7 of 9
(4,512 Views)

All right Ebalci!

that did the trick! It gave back the correct date.

Thank you very much!

best regards

Kurt

0 Kudos
Message 8 of 9
(4,507 Views)

You are welcome, Kurt.

I am also glad that it worked Smiley Wink

S. Eren BALCI
IMESTEK
0 Kudos
Message 9 of 9
(4,497 Views)