本文介绍了在.net中使用SharedStringTable和XML SDK设置文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面有一段代码可以在excel中获取特定单元格的文本,但是我不知道如何修改此文本来更改单元格文本.

I have a piece of code (below) that can get the text of an specific cell in excel, but I don't know how to modify this text to change the cell text.

public static void UpdateTextCell(string docName, string text, uint rowIndex, 
    string columnName, string sheetName)
{
   // Open the document for editing.
   using (SpreadsheetDocument spreadSheet = SpreadsheetDocument
       .Open(docName, true))
   {
        WorksheetPart worksheetPart = GetWorksheetPartByName(spreadSheet, 
            sheetName);

        if (worksheetPart != null)
        {

          SharedStringTablePart sharedstringtablepart=spreadSheet.WorkbookPart
              .GetPartsOfType<SharedStringTablePart>().First();
          SharedStringTable sharedStringTable = sharedstringtablepart
              .SharedStringTable;
          Cell cell = GetCell(worksheetPart.Worksheet, columnName, rowIndex);
          if (cell.DataType.Value == CellValues.SharedString)
          {
             var value = cell.InnerText;
             value = sharedStringTable.ElementAt(int.Parse(value)).InnerText;
             sharedStringTable.ElementAt(int.Parse(value)).InnerXml
                 .Replace("value", "Modified");
          }
          // Save the worksheet.
          worksheetPart.Worksheet.Save();
        }
   }
}

由于sharedStringTable.ElementAt(int.Parse(value)).InnerText是只读的,所以我尝试使用sharedStringTable.ElementAt(int.Parse(value)).InnerXml.Replace("value", "Modified");修改文本字符串,但这也不起作用.

Since sharedStringTable.ElementAt(int.Parse(value)).InnerText is read only, I tried to modify the text string using sharedStringTable.ElementAt(int.Parse(value)).InnerXml.Replace("value", "Modified"); but this doesn't work either.

你知道我想念什么吗?

推荐答案

尝试添加

sharedStringTable.Save();

之后

sharedStringTable.ElementAt(int.Parse(value)).InnerXml
                 .Replace("value", "Modified");

这篇关于在.net中使用SharedStringTable和XML SDK设置文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 09:29