OpenXML中的CellValues.InlineString和CellValues.String有什么区别?

时间:2011-06-24 13:52:41

标签: c# openxml openxml-sdk

我正在尝试编写一些代码来生成Excel电子表格,我不确定 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);
}

1 个答案:

答案 0 :(得分:14)

根据18.18.11 ST_CellType的文件:

  

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

在单元格中插入公式时,您只想使用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时,您的文字必须由文字元素和<is>标记包围:

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