Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. _variant_t vItem = pRange->Item[ iRow ][ iCol ]; [modified]

_variant_t vItem = pRange->Item[ iRow ][ iCol ]; [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
help
19 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Franck Paquier

    Good morning Are you sure that your cell containing a time value return a string ? may be Excel put in your variant a type which is not compatible with a string you may need to convert it before put in the string. Hope this will help Regards Franck.

    M Offline
    M Offline
    MsmVc
    wrote on last edited by
    #3

    yes i know that my question how to convert.

    L 1 Reply Last reply
    0
    • M MsmVc

      yes i know that my question how to convert.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      What exactly is the problem? You say that you are reading a string in the form "12:10:59". To convert this to a time value you need to split it into its component parts ("12", "10" and "59"), convert each substring to an integer, and then combine with a simple formula to give you the total number of seconds.

      M 1 Reply Last reply
      0
      • L Lost User

        What exactly is the problem? You say that you are reading a string in the form "12:10:59". To convert this to a time value you need to split it into its component parts ("12", "10" and "59"), convert each substring to an integer, and then combine with a simple formula to give you the total number of seconds.

        M Offline
        M Offline
        MsmVc
        wrote on last edited by
        #5

        No my problem is this. If i am reading "12:00:12" from excel then through this code

        _variant_t vItem = pRange->Item[ 1 ][ iColumn ];
        _bstr_t bstrText( vItem );

        i am getting 0.500138888888889 now what i do to get "12:00:12" from excel.

        L 1 Reply Last reply
        0
        • M MsmVc

          No my problem is this. If i am reading "12:00:12" from excel then through this code

          _variant_t vItem = pRange->Item[ 1 ][ iColumn ];
          _bstr_t bstrText( vItem );

          i am getting 0.500138888888889 now what i do to get "12:00:12" from excel.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          MsmVc wrote:

          i am getting 0.500138888888889

          This is the internal Excel date and time format. I cannot remember the exact details but I think the datetime is in the form D.S, where D is the number of days since the 'epoch'*, and S is number of seconds since midnight divided by 86,400 (the total number of seconds in a day). So in your case to get the time we multiply 0.500138888888889 by 86,400 which gives 43,212 or 12 hours, 0 minutes and 12 seconds. *epoch is the base date from which counting starts. I think Excel may start at year zero.

          M 1 Reply Last reply
          0
          • L Lost User

            MsmVc wrote:

            i am getting 0.500138888888889

            This is the internal Excel date and time format. I cannot remember the exact details but I think the datetime is in the form D.S, where D is the number of days since the 'epoch'*, and S is number of seconds since midnight divided by 86,400 (the total number of seconds in a day). So in your case to get the time we multiply 0.500138888888889 by 86,400 which gives 43,212 or 12 hours, 0 minutes and 12 seconds. *epoch is the base date from which counting starts. I think Excel may start at year zero.

            M Offline
            M Offline
            MsmVc
            wrote on last edited by
            #7

            Thanks for nice and smart concept.But my problem is something different. See i am getting values from Excel "0.500138888888889" this data can be Time format or double format.So how can i format this data.I think i need to identify data type of excel before format(Change). Please help me

            M L 2 Replies Last reply
            0
            • M MsmVc

              Thanks for nice and smart concept.But my problem is something different. See i am getting values from Excel "0.500138888888889" this data can be Time format or double format.So how can i format this data.I think i need to identify data type of excel before format(Change). Please help me

              M Offline
              M Offline
              MsmVc
              wrote on last edited by
              #8

              i think need to post some more code.

              try
              {
              .
              CoInitialize(NULL);

              while ( m\_list.DeleteColumn( 0 ) );
              HRESULT hr;
              
              CFileDialog fileDlg(TRUE, \_T("xls"), NULL, OFN\_PATHMUSTEXIST|OFN\_OVERWRITEPROMPT,\_T("Excel Files|\*.xls;\*.xlsx|"));
              if (fileDlg.DoModal () == IDCANCEL) 
              	return;
              
              CString OpenFile = fileDlg.GetPathName ();
              
              // Load the Excel application in the background.
              Excel::\_ApplicationPtr pApplication;
              if (FAILED( pApplication.CreateInstance( \_T("Excel.Application") ) ) )
              {
              	
              	return;
              }
              
              \_variant\_t	varOption( (long) DISP\_E\_PARAMNOTFOUND, VT\_ERROR );
              
              Excel::\_WorkbookPtr pBook = pApplication->Workbooks->Open( (\_bstr\_t)OpenFile, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption );
              if ( pBook == NULL )
              {
              		pBook->Close( VARIANT\_FALSE );
              
              // Need to quit, otherwise Excel remains active and locks the .xls file.
              pApplication->Quit( );
              	Errorf( \_T("Failed to open Excel file!") );
              	return;
              }
              
              
              for(int i=1;i<4;i++)
              {
              	Excel::\_WorksheetPtr pSheet = pBook->Sheets->Item\[ i \];
              	
              	\_bstr\_t na=pSheet->GetName();
              			
              	
              if ( pSheet == NULL )
              {
              	
              	return;
              }
              
              // Load the column headers.
              Excel::RangePtr pRange = pSheet->GetRange( \_bstr\_t( \_T("A1") ), \_bstr\_t( \_T("Z1" ) ) );
              
              
              
              if ( pRange == NULL )
              {
              	Errorf( \_T("Failed to get header cell range( A1:iv )!") );
              	return;
              }
              
              int	iColumns = 0;
              
              for ( int iColumn = 1; iColumn < 26; ++iColumn )
              {
              	\_variant\_t	vItem = pRange->Item\[ 1 \]\[ iColumn \];
              	\_bstr\_t		bstrText( vItem );
              
              	if ( bstrText.length( ) == 0 )
              		break;
              
              	m\_list.InsertColumn(iColumns++,bstrText,LVCFMT\_LEFT,170 );
              }
              
              // Load the rows (up to the first blank one).
              pRange = pSheet->GetRange( \_bstr\_t( \_T("A2") ), \_bstr\_t( \_T("Z16384" ) ) );
              
              
              int iColumn=0;
              for ( int iRow = 1; ; ++iRow )
              {
              	for (  iColumn = 1; iColumn <= iColumns; ++iColumn )
              	{
              		\_variant\_t	vItem = pRange->Item\[ iRow  \]\[ iColumn \];
              

              //Here i am getting values.
              _bstr_t bstrText( vItem );

              		if ( bstrText.length( ) == 0 )
              			break;
              
              		if ( iColumn == 1 )
              			m\_list.InsertItem( iRow - 1, bstrText );
              		else
              			m\_list.SetItemText( iRow - 1, iColumn - 1, bstrText );
              	}
              
              	if ( iColumn == 1 )
              		break;
              }
              
              // Make it all look pretty.
              for ( int iColumn = 1; iColumn <= iColumns; ++iColumn )
              	m\_list.SetColumnWidth( iColumn, LVSCW\_AUTOSIZE\_USEHEADER );
              }
              // Don't
              
              1 Reply Last reply
              0
              • M MsmVc

                Thanks for nice and smart concept.But my problem is something different. See i am getting values from Excel "0.500138888888889" this data can be Time format or double format.So how can i format this data.I think i need to identify data type of excel before format(Change). Please help me

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #9

                MsmVc wrote:

                I think i need to identify data type of excel before format(Change).

                Yes I guess that's right. Unfortunately I cannot help with that as I am not sure how to get the information from Excel as to what type of information is held in a particular cell. I suspect that there is no way; if you write a number in an Excel cell, you can format it any way you like. However, you are the only person that knows what that number represents, all Excel knows is how you want it to be displayed on the screen!

                M 1 Reply Last reply
                0
                • L Lost User

                  MsmVc wrote:

                  I think i need to identify data type of excel before format(Change).

                  Yes I guess that's right. Unfortunately I cannot help with that as I am not sure how to get the information from Excel as to what type of information is held in a particular cell. I suspect that there is no way; if you write a number in an Excel cell, you can format it any way you like. However, you are the only person that knows what that number represents, all Excel knows is how you want it to be displayed on the screen!

                  M Offline
                  M Offline
                  MsmVc
                  wrote on last edited by
                  #10

                  Plz help me..

                  F 1 Reply Last reply
                  0
                  • M MsmVc

                    Plz help me..

                    F Offline
                    F Offline
                    Franck Paquier
                    wrote on last edited by
                    #11

                    Good morning I agree with Richard. You know your excel document. so if you expect to find time cells inside, you should detect that when reading it. I think you could try to retreive from excel the cell format. detect the date time mode and react accordingly. if you cannot find the cell format then if i remeber well you can retreive the value of your cell as a string. in that case i would post analyze it and if something like "07:07:13" is detected i will do the tranformation for a time. bye the way did you try to analyze the type of your read vItem to see what is in the BSTR value for a date time cell ? Regards FRanck.

                    M 1 Reply Last reply
                    0
                    • F Franck Paquier

                      Good morning I agree with Richard. You know your excel document. so if you expect to find time cells inside, you should detect that when reading it. I think you could try to retreive from excel the cell format. detect the date time mode and react accordingly. if you cannot find the cell format then if i remeber well you can retreive the value of your cell as a string. in that case i would post analyze it and if something like "07:07:13" is detected i will do the tranformation for a time. bye the way did you try to analyze the type of your read vItem to see what is in the BSTR value for a date time cell ? Regards FRanck.

                      M Offline
                      M Offline
                      MsmVc
                      wrote on last edited by
                      #12

                      Good morning Thank for reply. i will do the tranformation a time I am waiting for you response. BSTR value for a date time cell ? i try to do that but didn't find through this code.

                      _variant_t vItem = pRange->Item[ 1 ][ iColumn ];

                      	COleDateTime dt;
                      	 switch (vItem.vt )
                      	{
                      
                      	     case VT\_DATE:
                      		{
                      
                      		dt = vItem.date;
                      
                      		break;
                      
                      		}
                      		case VT\_R8:
                      
                      		{
                      

                      AfxMessageBox("dOUBLE");

                      		break;
                      
                      		}
                      
                      		case VT\_BSTR:
                      
                      		{
                      			\_bstr\_t		bstrText( vItem );
                      
                      		//tmpData =(CString)val.bstrVal;
                      		//
                      		//strData+=tmpData;
                      
                      		break;
                      
                      		}
                      
                      		case VT\_EMPTY:
                      
                      		{
                      
                      		TRACE("\\t\\t<empty>");
                      
                      		break;
                      
                      		}
                      	}
                      

                      cursor jump out side the case.

                      F 1 Reply Last reply
                      0
                      • M MsmVc

                        Good morning Thank for reply. i will do the tranformation a time I am waiting for you response. BSTR value for a date time cell ? i try to do that but didn't find through this code.

                        _variant_t vItem = pRange->Item[ 1 ][ iColumn ];

                        	COleDateTime dt;
                        	 switch (vItem.vt )
                        	{
                        
                        	     case VT\_DATE:
                        		{
                        
                        		dt = vItem.date;
                        
                        		break;
                        
                        		}
                        		case VT\_R8:
                        
                        		{
                        

                        AfxMessageBox("dOUBLE");

                        		break;
                        
                        		}
                        
                        		case VT\_BSTR:
                        
                        		{
                        			\_bstr\_t		bstrText( vItem );
                        
                        		//tmpData =(CString)val.bstrVal;
                        		//
                        		//strData+=tmpData;
                        
                        		break;
                        
                        		}
                        
                        		case VT\_EMPTY:
                        
                        		{
                        
                        		TRACE("\\t\\t<empty>");
                        
                        		break;
                        
                        		}
                        	}
                        

                        cursor jump out side the case.

                        F Offline
                        F Offline
                        Franck Paquier
                        wrote on last edited by
                        #13

                        Hi If your switch detect nothing i guess it is because the date time is really stored as a double inside excel. Your call:

                        pRange->Item [1][iColumn]

                        always return the value as a double at least for date and time. To by-pass that i would try to retreive the Text of the cell which is what the cell show. Text if from VBA doc but it should exist the same entry point in automation. In that case i am pretty sure you will recieved the 10:23:00 value. In the web site above there is an explanation about how to translate an excel date time: http://www.cpearson.com/excel/datetime The last soulution: If your date time cell is at a fixed place then just retrieve the value and translate as explain in the above link. Regards Franck

                        M 1 Reply Last reply
                        0
                        • F Franck Paquier

                          Hi If your switch detect nothing i guess it is because the date time is really stored as a double inside excel. Your call:

                          pRange->Item [1][iColumn]

                          always return the value as a double at least for date and time. To by-pass that i would try to retreive the Text of the cell which is what the cell show. Text if from VBA doc but it should exist the same entry point in automation. In that case i am pretty sure you will recieved the 10:23:00 value. In the web site above there is an explanation about how to translate an excel date time: http://www.cpearson.com/excel/datetime The last soulution: If your date time cell is at a fixed place then just retrieve the value and translate as explain in the above link. Regards Franck

                          M Offline
                          M Offline
                          MsmVc
                          wrote on last edited by
                          #14

                          i think it's my fault to not describe proper. Date and time cell is not fixed.If i know this cell have date time then i easy convert it. My problem is how to know data time format.

                          F 1 Reply Last reply
                          0
                          • M MsmVc

                            i think it's my fault to not describe proper. Date and time cell is not fixed.If i know this cell have date time then i easy convert it. My problem is how to know data time format.

                            F Offline
                            F Offline
                            Franck Paquier
                            wrote on last edited by
                            #15

                            Hi I think like you do yourself when you watch an excel spread sheet. Either you retrieve the Text dispalyed by the cell and you recognize that it is a time. or you right click on the cell and look at the format of the cell. and you see something like dd:mm:ss Right now in your code you just query the double value of the cell it is not enough. You have to explore your cell deeper in order to be able to make a decision. To do that you can: Getting the STRING/TEXT displayed by the cell. and or the FORMAT associated with the cell. Regards.

                            M 1 Reply Last reply
                            0
                            • F Franck Paquier

                              Hi I think like you do yourself when you watch an excel spread sheet. Either you retrieve the Text dispalyed by the cell and you recognize that it is a time. or you right click on the cell and look at the format of the cell. and you see something like dd:mm:ss Right now in your code you just query the double value of the cell it is not enough. You have to explore your cell deeper in order to be able to make a decision. To do that you can: Getting the STRING/TEXT displayed by the cell. and or the FORMAT associated with the cell. Regards.

                              M Offline
                              M Offline
                              MsmVc
                              wrote on last edited by
                              #16

                              Respected Sir i know that all things to check right click on cell get format. explore your cell deeper in order to be able to make a decision i am same thing last 3 or 4 post.I don't have idea to got the cell format.Try to understand my problem.Plz help me

                              F 2 Replies Last reply
                              0
                              • M MsmVc

                                Respected Sir i know that all things to check right click on cell get format. explore your cell deeper in order to be able to make a decision i am same thing last 3 or 4 post.I don't have idea to got the cell format.Try to understand my problem.Plz help me

                                F Offline
                                F Offline
                                Franck Paquier
                                wrote on last edited by
                                #17

                                code bellow will return you something like: "12:56:32" for a cell containing time.

                                Excel::RangePtr pRange = pSheet->GetRange( _bstr_t(_T("A1") ) );
                                if ( pRange == NULL ){
                                return;
                                }

                                _bstr_t sText= pRange->GetText(); it exist also something called CellFormat. but i cannot find how to call it. Sorry for the short answer but i do not have good code sample with me right now. regards

                                1 Reply Last reply
                                0
                                • M MsmVc

                                  Respected Sir i know that all things to check right click on cell get format. explore your cell deeper in order to be able to make a decision i am same thing last 3 or 4 post.I don't have idea to got the cell format.Try to understand my problem.Plz help me

                                  F Offline
                                  F Offline
                                  Franck Paquier
                                  wrote on last edited by
                                  #18

                                  i wanted to type

                                  Excel::RangePtr pRange = pSheet->GetRange( _bstr_t(_T("A1") ) );
                                  if ( pRange == NULL ){
                                  return;
                                  }

                                  _bstr_t sText= pRange->GetText();

                                  M 1 Reply Last reply
                                  0
                                  • F Franck Paquier

                                    i wanted to type

                                    Excel::RangePtr pRange = pSheet->GetRange( _bstr_t(_T("A1") ) );
                                    if ( pRange == NULL ){
                                    return;
                                    }

                                    _bstr_t sText= pRange->GetText();

                                    M Offline
                                    M Offline
                                    MsmVc
                                    wrote on last edited by
                                    #19

                                    thanks i am trying to do through code.

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups