2012-06-12 18 views
1

即時通訊嘗試繪製一系列使用c的excel圖。問題是,當我嘗試在循環中製作圖時,我必須在Excel中更改工作表的名稱。但是,這些名稱在_bstr_t格式:C:int to _bstr_t

pSheet->Name =name; 

我想讓名字看起來像( 「表號%d」,i),其中i爲計數器。我嘗試使用sprintf和其他方法,但沒有運氣。

任何幫助,將不勝感激!

+1

我認爲你必須使用[BSTR功能(http://msdn.microsoft.com/en-us/library/windows/desktop/ms221069%28v=vs.85%29.aspx)使用BSTRs ... – sarnold

回答

0

首先創建一個字符數組的名稱,然後將其分配到的名稱。

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

類_bstr_t是圍繞BSTR數據類型,其被定義爲WCHAR *一個C++包裝類。例如參見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 正確的,這個代碼不回答你的問題,那麼請張貼的頭文件的代碼段您正在使用,顯示name屬性的定義。此外,pSheet->Name = name聲明不太可能設置Excel工作表的名稱,因爲它通常涉及調用函數而不是簡單地設置屬性。理解這一點也需要你更多的背景。