Solved

Visual Studio 2010 C++ Converting To VARIANT To Pass To Excel

Posted on 2011-03-23
5
1,083 Views
Last Modified: 2012-05-11
I have a vector that I need to convert the values to VARIANT so I can pass to Excel.  I've looked on the internet and found various things, none have worked (probably because I really don't understnad how to use them correctly).  The latest I've tried was using SysAllocString().  Here is what I am doing...  I open an input file, read data into a structure and then place into a vector for manipulation so I don't mess up the original data in the structure.  I then manipulate the data in the vector a little bit and then spit it out in a .CSV file.  That part works.  Instead of a .CSV file I would like to move the contents of the vector to cells in an Excel Spreadsheet.  I can open Excel and populate cells however I keep getting an error message saying "error C2660: 'Excel::Range::GetItem' : function does not take 0 arguments" when I try to put the contents of the vector into a cell:

pSheet->Cells->Item[x][1] =CompleteCompatVector[i][5];

Open in new window


This is where I tried using SysAllocString:

VARIANT VStr;
VStr = SysAllocString(CompleteCompatVector[i][5]);
pSheet->Cells->Item[x][1] = VStr;

Open in new window


That is when I get the following error:
'SysAllocString' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'const OLECHAR *'

I cannot find much documentation on this so that is why I am posting here.  Is there a better way to handle converting to a VARIANT, am I using an incorrect function and/or is my code just all honked up?
0
Comment
Question by:rgrguric
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 

Author Comment

by:rgrguric
ID: 35198320
Here is the structure that I use to hold the original input data:
struct InputFormat
	{
	                     string CurrentSet;
		string SetDescription;
		string MultipleSingle;
		string Required;
		string NextSet;
		string Model;
		string Facility;
		string OutlineType;
		string FeatureCode;
		string FeatureDescription;
		string FeatureQuantity;
		string FeatureAI;
		string FeatureSL;
		string ReferenceNumber;
		string Compat;
		string CompatQuantity;
		string LineType;
	};

Open in new window

0
 
LVL 34

Expert Comment

by:sarabande
ID: 35199080
the const OLECHAR* is a const WCHAR* what is a const wchar_t* (or LPCWSTR). that means it is a pointer to const wide char array.

the std::string is a basic_string<char>  what is a class object which you can't simply assign to a OLECHAR*. but std::string has a function c_str which returns a const char *.

so we need to turn a const char * to a wchar_t * what can be made by mbstowcs function:

 
const char * pcs = CompleteCompatVector[i][5].c_str(); 
  size_t len = CompleteCompatVector[i][5].size();
  wchar_t *  pwcs = new wchar_t[len +1];
  mbstowcs(pwcs, pcs, len);
  VARIANT VStr;
  VStr = SysAllocString(pwcs);
  pSheet->Cells->Item[x][1] = VStr; 
  delete [] pwcs;

Open in new window


i know there are also macros A2W or similar that do the conversion but don't know whether they were available in your environment.

Sara

 
0
 

Author Comment

by:rgrguric
ID: 35199184
Thanks for the explanation!

I tried the above and am getting an error on line:

VStr = SysAllocString(pwcs);

Open in new window


Error:
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'BSTR' (or there is no acceptable conversion)
1>          c:\program files\microsoft sdks\windows\v7.0a\include\oaidl.h(492): could be 'tagVARIANT &tagVARIANT::operator =(const tagVARIANT &)'
1>          while trying to match the argument list '(VARIANT, BSTR)'
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 35199215
You should be better off using the COM support classes, in this case '_variant_t' (http://msdn.microsoft.com/en-us/library/x295h94e%28VS.80%29.aspx) - they will spare you from most of the work. E.g.
#include <comdef.h>

// ...

_variant_t v(CompleteCompatVector[i][5].c_str());

pSheet->Cells->Item[x][1] = v.Detach(); // no 'SysFreeString()' etc. needed.

Open in new window

0
 
LVL 34

Expert Comment

by:sarabande
ID: 35199292
now the SysAllocString is happy with the pwcs but the VARIANT doesn't accept a right-hand BSTR what is the return of SysAllocString.

you better go with the solution jkr made and use the _variant_t what is a wrapper class around the VARIANT type.

Sara
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question