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

  • Home
  • SEARCH
  • 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 9157245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:58:03+00:00 2026-06-17T12:58:03+00:00

I am trying to create an Excel document using OpenXML (SAX method). When my

  • 0

I am trying to create an Excel document using OpenXML (SAX method). When my method is called I want to check to see if a tab has already been created for a given key. If it is I would like to just append a row to the bottom of that tab. If the tab hasn’t been created for a given key I create a new tab like;

      part = wbPart.AddNewPart<WorksheetPart>();

                        string worksheetName = row.Key[i].ToString();

                        Sheet sheet = new Sheet() { Id = document.WorkbookPart.GetIdOfPart(part), SheetId = sheetNumber, Name = worksheetName };
                        sheets.Append(sheet);

                        writer = OpenXmlWriter.Create(part);
                        writer.WriteStartElement(new Worksheet());
                        writer.WriteStartElement(new SheetData());

                        currentrow = 1;

                        string header = Header + "\t" + wrapper.GetHeaderString(3, 2, -1); //need to fix
                        WriteDataToExcel(header, currentrow, 0, writer);
                        currentrow++;


                        writer.WriteEndElement();
                        writer.WriteEndElement();
                        writer.Close();

If the a tab as already been created I recall sheet using the following code;

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;

    }

When the correct Worksheet part is returned I try and add the new row by pointing my OpenXmlWriter to the correct part then adding the row;

   part = GetWorksheetPartByName(document, row.Key[i].ToString());

                        writer = OpenXmlWriter.Create(part);
                        writer.WriteStartElement(part.Worksheet);
                        writer.WriteStartElement(part.Worksheet.GetFirstChild<SheetData>());
                        SheetData sheetData = part.Worksheet.GetFirstChild<SheetData>();
                        Row lastRow = sheetData.Elements<Row>().LastOrDefault();

The code runs however I always end up with just one row (the initial one I added when first creating the tab). No subsequent rows show up in the spreadsheet.

I will be adding a lot of rows (50,000+) and would prefer not to have to create a new file and copy the information over each time.

  • 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. Editorial Team
    Editorial Team
    2026-06-17T12:58:05+00:00Added an answer on June 17, 2026 at 12:58 pm

    From my experience, using the SAX method to write (ie, with OpenXmlWriter) works best for new things (parts, worksheets, whatnot). When you use OpenXmlWriter.Create(), that’s like overwriting the original existing data for the part (WorksheetPart in this case). Even though in effect, it’s not. It’s complicated.

    As far as my experiments went, if there’s existing data, you can’t edit data using OpenXmlWriter. Not even if you use the Save() function or close the OpenXmlWriter correctly. For some reason, the SDK will ignore your efforts. Hence the original one row that you added.

    If you’re writing 50,000 rows, it’s best to do so all at one go. Then the SAX method will be useful. Besides, if you’re writing one row (at a time?), the speed benefits of using SAX versus the DOM method is negligible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a multi-sheet excel document, and thus far I'd been doing
I am trying to create dynamic excel sheet, with the help of php using
I am trying to create a macro for an Excel file which has a
I am trying to create a new instance of Excel using VBA using: Set
We are trying to create an excel document which supports macros with some data
I have automation to create an Excel document from C#. I am trying to
I am trying to create an Excel sheet using the Spreadsheet_Excel_Writer that comes with
I am trying to open an Excel document using EPPlus reference/package. I can't get
I am trying to create an excel file using the below code in an
I am trying to create addin for excel that has a dropdown and a

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.