C:int到_bstr_t
即时通讯试图绘制一系列的Excel中使用C的情节。 问题是当我试图把循环制作的情节,我必须改变工作表的名称在Excel中。 但是这些名字是用_bstr_t格式的:
pSheet->Name =name;
我想让名字看起来像(“张数%d”,我),我是反击。 我尝试使用sprintf和其他方法,但没有运气。
任何帮助,将不胜感激!
首先用名称创build一个字符数组,然后将其分配给Name。
char arr[25]; sprintf(arr, "Sheet number %d", i); pSheet->Name = arr;
类_bstr_t是围绕着BSTR数据types的C ++包装类,它被定义为WCHAR *。 看例如BSTR和_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的引用是正确的,并且此代码不能回答您的问题,请发布您正在使用的头文件的片段,以显示name
属性的定义。 而且,语句pSheet->Name = name
不可能设置Excel工作表的名称,因为这通常涉及到调用一个函数,而不是简单地设置一个属性。 理解这一点也需要更多的背景。