简体   繁体   中英

OpenXML: How to modify an excel's cell attribute?

Given the following XML tag:

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

Using OpenXMLReader, I would like to access cell C1 and modify the xml attribute to add a new datatype t="inlineStr" which would then allow me to insert a text to the cell as follows:

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

This is my code which accessrd the cell:

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

How do I modify the cell attributes to include t=s ?

This is how you create an inline Cell with text

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM