我有一段代码(如下)可以在excel中获取特定单元格的文本,但我不知道如何修改此文本以更改单元格文本。
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");
但这也不起作用。
你知道我错过了什么吗?