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

mht*_*ttk 16 c# openxml openxml-sdk

我正在尝试编写一些代码来生成Excel电子表格,我不确定CellValues.InlineStringCellValues.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); 
}
Run Code Online (Sandbox Code Playgroud)

这个

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

或者只是这个(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);
}
Run Code Online (Sandbox Code Playgroud)

amu*_*rra 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>
Run Code Online (Sandbox Code Playgroud)

CellValues.InlineString只有在你不想将字符串存储在SharedStringTable.时才应该使用.然后,您标记为的任何内容都inlineString将被视为富文本.虽然请注意,当您使用时CellValues.InlineString,您的文本必须由文本元素和<is>标记包围:

<x:c r="B2" t="inlineStr">
   <is><t>test string</t></is>
</c>
Run Code Online (Sandbox Code Playgroud)