2012-12-25 10 views
8

Ho questo codice corrente, che digita "AAPL" in un foglio Excel, e restituisce il valore corrispondente.convertire una stringa in una variante in C++

Vorrei fare in modo che, dopo cout << "Ticker: "; posso digitare un simbolo ticker (come AAPL) e utilizzare questo come il variant_t ticker = "xxx". Ho provato a fare in modo da utilizzare string ma ottengo un errore che dice che non si può convertire da 'std::string to const _variant_t &' C'è un modo per fare questo? Grazie in anticipo.

XL->Workbooks->Open(L"F:\\xxx\\Desktop\\fxxxx.xlsx"); 
Excel::RangePtr pRange = pSheet->Cells; 

cout << "Ticker: "; 
variant_t ticker = "AAPL"; 

pRange->Item[2][1] = ticker; 
double value = pRange->Item[2][2]; 
cout << "\n Value = " << value << endl; 

risposta

6

Call ticker.SetString(str.c_str()) dovrebbe fare il lavoro.

See: http://msdn.microsoft.com/en-us/library/x295h94e%28v=vs.100%29.aspx

+0

Grazie mille per il vostro aiuto, ho provato ad aggiungere questo in varie linee, ma ottengo un errore, dove esattamente dove lo metto ? – user1594369

+0

Da qualche parte tra 'variant_t ticker;' e 'pRange-> Item .. = ticker;'. Ovviamente 'str' dovrebbe essere qualsiasi cosa venga chiamato il tuo input std :: string. E probabilmente lo vuoi fare DOPO aver letto nella stringa, o non avrà l'effetto giusto. ;) –

+0

Puoi aggiungere la tua risposta come una "risposta" (rende il codice molto più leggibile) - e se pensi di averlo fatto per aiutarti ad arrivarci, "accetta" la mia risposta. –

2

Ecco il codice di lavoro:

XL->Workbooks->Open(L"F:\\xx\\Desktop\\xxz.xlsx");        //Open databse 
    Excel::_WorksheetPtr pSheet = XL->ActiveSheet;          //Point to Active Sheet 
    Excel::RangePtr pRange = pSheet->Cells;            //Point to Active Cells 
    cout << " Ticker: ";                //Prompt Ticker 
    string tick;                  //Define string "tick" to store ticker 
    cin >> tick;                  //Store ticker 
    variant_t ticker;                 //Define variant to be used for ticker 
    ticker.SetString(tick.c_str());              //Convert string to variant         
    pRange->Item[2][1] = ticker;              //Write ticker to cell 
    double bi = pRange->Item[2][2];              //Read Beta, store as "bi" 
    cout << "\n Beta = " << bi << endl;             //Return Value 
    XL->Application->Quit();