在C ++中使Excel自动化的程序的问题 [英] Issue with program to automate excel in C++

查看:79
本文介绍了在C ++中使Excel自动化的程序的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的C ++专家/专家,

我正在编写一个使excel自动化的程序.当我在excel中使用整数(写/读)时,我的程序运行良好.当我使用字符串时,我的程序也运行良好.但是当我使用字符串数组时出现了问题.问题如下
错误C2664:" SysAllocString":无法将参数1从"char *"转换为"const OLECHAR *"".请帮忙.

程序:

Dear C++ Gurus/Experts,

I am writing a program to automate excel.My program is working well when I use integer (Write/Read) in excel.My program also works well when I use string.But the issue appears when I use array of strings.I am getting the issue as below
"error C2664: ''SysAllocString'' : cannot convert parameter 1 from ''char *'' to ''const OLECHAR *'' ". Please Help.

Program:

//working good with strings

 for(int i=1; i<=15; i++) {
      for(int j=1; j<=15; j++) {
         // Create entry value for (i,j)
		  BSTR b;
		  VARIANT parm1;
		  b = SysAllocString(L"Haroon"); // this line working good
		  parm1.vt = VT_BSTR;
		  parm1.bstrVal = b;
         // Add to safearray...
         long indices[] = {i,j};
		 SafeArrayPutElement(arr.parray, indices, &parm1);
      }
   }

//Having problems with Array of strings

//Array of strings

char *hrs[]={"noor","riz","vignesh"};

 for(int i=1; i<=15; i++) {
      for(int j=1; j<=15; j++) {
         // Create entry value for (i,j)
		  BSTR b;
		  VARIANT parm1;
		  b = SysAllocString(hrs[0]); // issue in this line
		  parm1.vt = VT_BSTR;
		  parm1.bstrVal = b;
         // Add to safearray...
         long indices[] = {i,j};
		 SafeArrayPutElement(arr.parray, indices, &parm1);
      }
   }

推荐答案

SysAllocString()参数的类型为OLECHAR,它是一个宽字符(WCHAR).在第一个版本中,您传递的是一个恒定的宽字符串,而第二个版本中则使用了一个char字符串数组.要解决此问题,请使用:
The SysAllocString() parameter is of type OLECHAR which is a wide char (WCHAR). In your first version, you are passing a constant wide string while your second version uses an array of char strings. To solve the problem use:
WCHAR *hrs[]={L"noor",L"riz",L"vignesh"};


或更好的const字符串数组(LPCWSTRconst WCHAR*)


or better as array of const strings (LPCWSTR is const WCHAR*)

LPCWSTR hrs[]={L"noor",L"riz",L"vignesh"};


这篇关于在C ++中使Excel自动化的程序的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆