_bstr_t 类是一个围绕 BSTR 数据类型的 C++ 包装类,它被定义为 WCHAR *。参见例如What's the difference between BSTR and _bstr_t?。如果您正在使用 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 的引用是正确的,并且此代码没有回答您的问题,那么请发布您正在使用的头文件的 sn-p,显示 name 的定义属性。此外,pSheet->Name = name 语句不太可能设置 Excel 工作表的名称,因为这通常涉及调用函数而不是简单地设置属性。理解这一点也需要您提供更多背景信息。