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 8855845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:13:39+00:00 2026-06-14T14:13:39+00:00

If anything, this serves as my notes for parsing/importing Open XML Excel file data.

  • 0

If anything, this serves as my notes for parsing/importing Open XML Excel file data.

I created code below using Open XML SDK 2.0 that extracts Excel 2007 data from one Excel file successfully but fails returning empty strings on a different but similar Excel file. I don’t know if I’m using the right methods in the right hierarchy correctly or if this changes depending on the Excel file version (2007, 2010, 2003 with compatibility, etc.).

Is there a consistent method for extracting data from Excel files using Open XML SDK 2/2.5?

I made some LinqPad examples below commented with my findings/example questions.

    // Obtain a reference to the spreadsheet file 
var doc = SpreadsheetDocument.Open(@"C:\MyExcelFile.xlsx", false);

    // Only one WorkbookPart per spreadsheet
    // Parts have a common Root property and differing methods to set them
    // like WorkbookPart.Workbook sets the root for WorkbookPart
var workbookpart = doc.WorkbookPart;

    // Find sheet names and ids
var sheets = doc.WorkbookPart.Workbook.Sheets;
var sheetname = sheets.Descendants<Sheet>().FirstOrDefault().Name.Value;
var sheetId = sheets.Descendants<Sheet>().FirstOrDefault().SheetId.Value;
var id = sheets.Descendants<Sheet>().FirstOrDefault().Id.Value;

    // "Parts" hold collections even if that part only has one sub part
    // How many Worksheet parts should we expect?  One per Sheet?

var sheet1id = doc.WorkbookPart.Workbook.Descendants<Sheet>()
               .Where(p => p.Name.Value == "Sheet1")
               .Select(q => q.Id.Value).FirstOrDefault();

var worksheetpart = (WorksheetPart) workbookpart.GetPartById( sheet1id);
//  Removed below because I don't know how to read WorksheetPart id
//var worksheetpart = workbookpart.WorksheetParts.FirstOrDefault();

    // Worksheet is the root of WorkSheetPart
    // Worksheet.Descendants<Column>() has usable min, max, width, customWidth
    // Worksheet.Descendants<Row>() (or any other <type>) is empty
    // Why is Worksheet.SheetDimension empty? How do you determine sheet size?
    // Other than doc.WorkbookPart.GetPartById(sheetId) I have no idea
    // how to determine this worksheet's id, name, or sheetid
    // maybe their index as an Array matches the Sheets Id?
var worksheet = worksheetpart.Worksheet;

    // Expect multiple SheetData?  Why?
    // .Descendants< ... >() retrieves objects cast to their proper type
var sheetdata = worksheet.Descendants<SheetData>().FirstOrDefault();

    // Is this where we should access Rows or under Worksheet?
var row = sheetdata.Descendants<Row>().FirstOrDefault();

var cell = row.Descendants<Cell>().FirstOrDefault();

    // Print out the Cell's values (Need to reference shared values elsewhere)
cell.CellReference.Value.Dump();
cell.DataType.Value.Dump();
cell.CellValue.Text.Dump();

    // Close the spreadsheet
doc.Close();

(add LinqPad Query References of DocumentFormat.OpenXml, WindowsBase.dll and Namespace imports of DocumentFormat.OpenXml, DocumentFormat.OpenXml.Spreadsheet, and DocumentFormat.OpenXml.Packaging to make this work.)

  • 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-14T14:13:40+00:00Added an answer on June 14, 2026 at 2:13 pm

    As I don’t know, what you want to do with data, I’ll post example that reads values from all Cells in all sheets. Code taken from article, written by Mathias Brandewinder here

    var filePath = @"C:/Tests/protectedFile.xlsx";
             using (var document = SpreadsheetDocument.Open(filePath, false))
             {
                var workbookPart = document.WorkbookPart;
                var workbook = workbookPart.Workbook;
    
                var sheets = workbook.Descendants<Sheet>();
                foreach (var sheet in sheets)
                {
                   var worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id);
                   var sharedStringPart = workbookPart.SharedStringTablePart;
                   var values = sharedStringPart.SharedStringTable.Elements<SharedStringItem>().ToArray();
    
                   var cells = worksheetPart.Worksheet.Descendants<Cell>();
                   foreach (var cell in cells)
                   {
                      Console.WriteLine(cell.CellReference);
                      // The cells contains a string input that is not a formula
                      if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
                      {
                         var index = int.Parse(cell.CellValue.Text);
                         var value = values[index].InnerText;
                         Console.WriteLine(value);
                      }
                      else
                      {
                         Console.WriteLine(cell.CellValue.Text);
                      }
    
                      if (cell.CellFormula != null)
                      {
                         Console.WriteLine(cell.CellFormula.Text);                    
                      }
                   }
                }
             }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE As Mathias notes below, this exact problem has been reported and resolved here:
Specifically because of server restrictions for this project I cannot use anything like Google
Is there anything like this outside of the application delegate? I would just like
is there anything like this in JDK or Apache Commons (or in other jar)?
(Before anyone says anything Yes this was homework but i have already turned it
I didnt find anything according this issue. Can jaas be used to secure my
I can't find anything about this on the internet, so I'm looking for help
I couldn't find anything on this, but probably its just because I don't know
I couldn't find anything about this in MySQL documentation. SELECT accesion_id, definition FROM accesion_table
Well this is a really weird issue, I really didn't find anything on this

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.