OpenXML:如何修改excel的单元格属性?



给定以下XML标记:

<x:c r="C1" s="9"/>   

使用 OpenXMLReader,我想访问单元格C1并修改 xml 属性以添加新的数据类型t="inlineStr"然后允许我在单元格中插入文本,如下所示:

<x:c r="C1" s="9" t="inlineStr"/> 
  <x:is>
       <x:t>Report Title</x:t>
  </x:is>  

这是我访问单元格的代码:

if (oXMLReader.ElementType == typeof(Cell))
{
   if (oXMLReader.Attributes.First(a => a.LocalName == "r").Value == "C1")
   {
     //to do: modify the cell attributes to include t="s"
     oXMLWriter.WriteElement(new CellValue("Report Title"));
   }
}

如何修改单元格属性以包含t=s

这是

创建带有文本的内联Cell的方式

public Cell CreateInlineTextCell(string columnName, int rowIndex, string input)
{
    string cellReference = columnName + rowIndex;
    var cell = new Cell
    {
        DataType = CellValues.InlineString,
        CellReference = cellReference
    };
    var inlineString = new InlineString();
    var text = new Text { Text = input };
    inlineString.AppendChild(text);
    cell.AppendChild(inlineString);
    return cell;
}

最新更新