Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 132449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:15:51+00:00 2026-05-11T06:15:51+00:00

I want to update a cell in a spreadsheet that is used by a

  • 0

I want to update a cell in a spreadsheet that is used by a chart, using the Open XML SDK 2.0 (CTP). All the code samples I have found insert new cells. I am struggling with retrieving the right worksheet.

public static void InsertText(string docName, string text, uint rowIndex,    string columnName) {   // Open the document for editing.   using (SpreadsheetDocument spreadSheet =      SpreadsheetDocument.Open(docName, true))   {     Workbook workBook = spreadSheet.WorkbookPart.Workbook;      WorksheetPart worksheetPart = workBook.WorkbookPart.       WorksheetParts.First();      SheetData sheetData = worksheetPart.Worksheet.       GetFirstChild<SheetData>();      // If the worksheet does not contain a row with the specified     // row index, insert one.     Row row;      if (sheetData.Elements<Row>().Where(       r => r.RowIndex == rowIndex).Count() != 0)       // At this point I am expecting a match for a row that exists       // in sheet1 but I am not getting one 

When I navigate the tree in Visual Studio, I am seeing three sheets, but none of them has any children. What am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:15:52+00:00Added an answer on May 11, 2026 at 6:15 am

    Here is the working code. This is a prototype. For a larger number of changes, one might open the document only once. Also, there are some hard-coded things like sheet name and cell type that would have to be parameterized before this can be called production-ready. http://openxmldeveloper.org/forums/4005/ShowThread.aspx was very helpful.

    using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Spreadsheet; using System.Xml; using System.IO; using System.Diagnostics;  namespace OpenXMLWindowsApp {     public class OpenXMLWindowsApp     {         public void UpdateSheet()         {             UpdateCell('Chart.xlsx', '20', 2, 'B');             UpdateCell('Chart.xlsx', '80', 3, 'B');             UpdateCell('Chart.xlsx', '80', 2, 'C');             UpdateCell('Chart.xlsx', '20', 3, 'C');              ProcessStartInfo startInfo = new ProcessStartInfo('Chart.xlsx');             startInfo.WindowStyle = ProcessWindowStyle.Normal;             Process.Start(startInfo);         }          public static 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();         }      } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I want: Update all new commits from server with my local repository in
I am using MS Access Database. Now I have to update a particular cell
Hello, I want to build excel spreadsheet with servlet programming. I used tab-separated data
I want to paste an image from clipboard to an excel cell using vba.
Alright heres the deal. I have a sports app that i want to update
I want to update a cell with the date-time when a value is entered
I have developed a windows forms c# application, i just want update items in
I want to update the partial view every time the ActionLink is clicked. I'm
I want to update a table field with a new value provided like if
I want to update a list of storage devices as the user inserts USB

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.