C:int到_bstr\t

C:int到_bstr\t,c,windows,excel,bstr,C,Windows,Excel,Bstr,我正试图用c语言在excel中绘制一系列曲线图。问题是,当我尝试在循环中创建绘图时,我必须更改excel中工作表的名称。但这些名称的格式是: pSheet->Name =name; 我想使名称看起来像第%d页,I,其中I是计数器。我尝试使用sprintf和may其他方法,但没有成功 任何帮助都将不胜感激 首先使用名称创建一个字符数组,然后将其分配给名称 char arr[25]; sprintf(arr, "Sheet number %d", i); pSheet->Nam

我正试图用c语言在excel中绘制一系列曲线图。问题是,当我尝试在循环中创建绘图时,我必须更改excel中工作表的名称。但这些名称的格式是:

    pSheet->Name =name;
我想使名称看起来像第%d页,I,其中I是计数器。我尝试使用sprintf和may其他方法,但没有成功


任何帮助都将不胜感激

首先使用名称创建一个字符数组,然后将其分配给名称

char arr[25];
sprintf(arr, "Sheet number %d", i);
pSheet->Name = arr;

类BSTRUTT是一个围绕BSTR数据类型的C++包装类,它定义为WCHAR *。例如,见。如果您正在使用C,那么您对_bstr_t的引用可能不正确,您应该请求转换为bstr

以下代码行可以为您完成此操作:

DWORD len;
BSTR  bstrPtr;
char  sheetName[25];

/* Construct the name of your sheet as a regular string */
sprintf(sheetName, "Sheet number %d", i);
/* Count the number of bytes in the WChar version of your name
   by doing a dummy conversion */
len = MultiByteToWideChar(CP_ACP, 0, sheetName, -1, 0, 0);
/* Allocate the BSTR with this size */
bstrPtr = SysAllocStringLen(0, len);
/* Do the actual conversion of the sheetName into BSTR */
MultiByteToWideChar(CP_ACP, 0, sheetName, -1, bstrPtr, len);

/* Do your stuff... */

/* Deallocate the BSTR */
SysFreeString(bstrPtr);
如果您对_bstr_t的引用是正确的,并且此代码没有回答您的问题,那么请发布您正在使用的头文件的片段,显示name属性的定义。此外,语句pSheet->Name=Name不太可能设置Excel工作表的名称,因为这通常涉及调用函数,而不是简单地设置属性。理解这一点也需要您提供更多的上下文。

我认为您必须使用BSTR。。。