【问题标题】:How can I update a cell into worksheet without create new worksheet如何在不创建新工作表的情况下将单元格更新到工作表中
【发布时间】: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 或从空白到“文本”)
  • 请提供完整代码

标签: c# excel openxml


【解决方案1】:

下面的代码允许您更新单元格。

public class UpdateExcelCell
    {    
        public void UpdateCell(string docName, string text,uint rowIndex, string columnName)
        {
            // Open the document for editing.
            using (SpreadsheetDocument spreadSheet =SpreadsheetDocument.Open(docName, true))
            {
                WorksheetPart worksheetPart = GetWorksheetPartByName(spreadSheet, "Sheet1");

                if (worksheetPart != null)
                {
                    Cell cell = GetCell(worksheetPart.Worksheet, columnName, rowIndex);

                    cell.CellValue = new CellValue(text);
                    cell.DataType = new EnumValue<CellValues>(CellValues.Number);

                    // Save the worksheet.
                    worksheetPart.Worksheet.Save();
                }
            }

        }

        private static WorksheetPart GetWorksheetPartByName(SpreadsheetDocument document, string sheetName)
        {
            IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName);

            if (sheets.Count() == 0)
            {
                // The specified worksheet does not exist.
                return null;
            }

            string relationshipId = sheets.First().Id.Value;
            WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(relationshipId);
            return worksheetPart;

        }

        // Given a worksheet, a column name, and a row index, 
        // gets the cell at the specified column and 
        private static Cell GetCell(Worksheet worksheet, string columnName, uint rowIndex)
        {
            Row row = GetRow(worksheet, rowIndex);

            if (row == null)
                return null;

            return row.Elements<Cell>().Where(c => string.Compare(c.CellReference.Value, columnName + rowIndex, true) == 0).First();
        }


        // Given a worksheet and a row index, return the row.
        private static Row GetRow(Worksheet worksheet, uint rowIndex)
        {
            return worksheet.GetFirstChild<SheetData>().Elements<Row>().Where(r => r.RowIndex == rowIndex).First();
        }
    }

你这样称呼它:

UpdateExcelCell xl = new UpdateExcelCell();
xl.UpdateCell(@"C:\Book1.xlsx","cell has been changed",1,"A");

【讨论】:

  • 我已经在网上找到了该解决方案,但它不起作用。它给了我 .Name 错误('OpenXmlElement' 不包含'Name' 的定义,并且没有找到采用'OpenXmlElement' 类型的第一个参数的可访问扩展方法'Name'。可能缺少使用指令或对'assembly' 的引用.) 和 GetFirstChild(无法从用法推断方法“OpenXmlElement.GetFirstChild ()”的类型参数。尝试显式指定类型参数)和 EnumValue(使用泛型类型“EnumValue ”需要类型 1 的参数) .我该如何解决?
  • 您必须在上面指定您已经尝试过的内容以及收到的错误。人们不会为你做这项工作。
  • 上面的代码有效。更新后打开文件时,您只需查看错误。
  • 好的,很好。编辑后它可以工作。非常感谢。但是我并没有要求解决它,而是要纠正出现在我面前的错误哈哈
  • 您的代码不完整。没有人会调查缺少部分的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-31
  • 2023-03-19
相关资源
最近更新 更多