2011-06-24 16 views
15

我想寫一些代碼來生成一個Excel電子表格,我不知道CellValues.InlineStringCellValues.String之間的區別插入文本的單元格。OpenXML中的CellValues.InlineString和CellValues.String有什麼區別?

要不要我用這個:

private void UpdateCellTextValue(Cell cell,string cellValue) 
{ 
    InlineString inlineString = new InlineString(); 
    Text cellValueText = new Text { Text = cellValue }; 
    inlineString.AppendChild(cellValueText); 

    cell.DataType = CellValues.InlineString; 
    cell.AppendChild(inlineString); 
} 

private void UpdateCellTextValue(Cell cell, string cellValue) 
{ 
    cell.CellValue = new CellValue(cellValue); 
    cell.DataType = new EnumValue<CellValues>(CellValues.String); 
} 

或只是這個(InsertSharedStringItem返回新插入的共享字符串項的ID)

private void SetCellSharedTextValue(Cell cell,string cellValue) 
{   
    int stringId = InsertSharedStringItem(cellValue); 
    cell.CellValue = new CellValue(stringId.ToString()); 
    cell.DataType = new EnumValue<CellValues>(CellValues.SharedString); 
} 

回答

13

按照18.18.11 ST_CellType的文檔:

str(字符串)包含公式 字符串的單元格。

當您在單元格中插入公式時,您只想使用CellValues.String。下面是XML應該如何看:

<x:c r="C6" s="1" vm="15" t="str"> 
    <x:f>CUBEVALUE("xlextdat9 Adventure Works",C$5,$A6)</x:f> 
    <x:v>2838512.355</x:v> 
</x:c> 

CellValues.InlineString,才應使用,如果你不希望存儲在SharedStringTable的字符串。然後,您標記爲inlineString的任何內容都將被視爲富文本。但是注意,當你使用CellValues.InlineString您的文字必須由一個文本元素surounded和<is>標籤:

<x:c r="B2" t="inlineStr"> 
    <is><t>test string</t></is> 
</c> 
+0

請哪裏是你所引用的文件的鏈接? –

+1

我用鏈接更新了答案,以便下載打開xml規範的pdf。您可以在那裏搜索或參考此示例http://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.cell%28v=office.14%29.aspx – amurra