如何在不创建新工作表的情况下将单元格更新到工作表中

How can I update a cell into worksheet without create new worksheet

我想更新工作表中的一个单元格,但我在网上找到的唯一代码是如何创建一个新单元格。

如何更新工作表中的单元格(请参阅 ????)?

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

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

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