【问题标题】:What is the difference between CellValues.InlineString and CellValues.String in OpenXML?OpenXML 中的 CellValues.InlineString 和 CellValues.String 有什么区别?
【发布时间】:2011-06-24 13:52:40
【问题描述】:

我正在尝试编写一些代码来生成 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); 
}

这个

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);
}

【问题讨论】:

    标签: c# openxml openxml-sdk


    【解决方案1】:

    根据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 时,您的文本必须被文本元素和&lt;is&gt; 标签包围:

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

    【讨论】:

    • 请问您引用的文档的链接在哪里?
    • 我用链接更新了答案,以下载开放 xml 规范的 pdf。您可以在那里搜索或参考此微软链接以获取示例msdn.microsoft.com/en-us/library/…
    猜你喜欢
    • 2012-05-17
    • 2010-10-02
    • 2011-12-12
    • 2010-09-16
    • 2012-03-14
    • 2012-02-06
    • 2011-02-25
    • 2011-11-22
    • 2015-03-26
    相关资源
    最近更新 更多