【发布时间】:2021-12-20 15:51:15
【问题描述】:
我想更新我的工作表中的一个单元格,但我在网上找到的唯一代码是如何创建一个新的。
如何更新工作表中的单元格(参见????)?
public static void InsertText(string docName, string text)
{
// Open the document for editing.
using SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true);
// Get the SharedStringTablePart. If it does not exist, create a new one.
SharedStringTablePart shareStringPart;
if (spreadSheet.WorkbookPart.GetPartsOfType<SharedStringTablePart>().Count() > 0)
{
shareStringPart = spreadSheet.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First();
}
else
{
shareStringPart = spreadSheet.WorkbookPart.AddNewPart<SharedStringTablePart>();
}
// Insert the text into the SharedStringTablePart.
int index = InsertSharedStringItem(text, shareStringPart);
WorksheetPart worksheetPart = ????
Cell cell = InsertCellInWorksheet("A", 1, worksheetPart);
// Set the value of cell A1.
cell.CellValue = new CellValue(index.ToString());
cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);
worksheetPart.Worksheet.Save();
}
【问题讨论】:
-
执行此操作的标准方法是创建一个新的,删除旧的并重命名新的以匹配旧的。
-
非常感谢,我该怎么做?你能用代码示例解释一下吗?
-
您要更新单元格的值吗?
-
是的,我想更改值(例如,从 0 到 135 或从 4 到 2 或从空白到“文本”)
-
请提供完整代码