8

我有这个当前代码,它将“AAPL”输入到 Excel 表中,并返回相应的值。

我想这样做,以便在cout << "Ticker: ";我可以输入股票代码(例如 AAPL)并使用它之后,variant_t ticker = "xxx".我尝试通过使用这样做,string但我得到一个错误,说不能从'std::string to const _variant_t &'无论如何要这样做?提前致谢。

 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;
4

2 回答 2

6

电话ticker.SetString(str.c_str())应该可以完成这项工作。

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

于 2012-12-26T00:16:31.877 回答
2

这是工作代码:

    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();
于 2012-12-26T01:20:55.777 回答