OpenXML C# Excel

When using open XML to create an excel file, do you need to create rows before you can add cell data to the row?

If I simply want to add data to A7, D7, E7 do I need to create row 7 first?

Any examples on how to achieve this would be helpful.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kelvin McDanielSr. DeveloperCommented:
Yes, you need to create the row before you begin to add cells. Otherwise, it doesn't know where things are supposed to be located.

The following example is from a decent tutorial series found at that mirrors what I recall when I last used the library.
public void CreateExcelDoc(string fileName)
    using (SpreadsheetDocument document = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook))
        WorkbookPart workbookPart = document.AddWorkbookPart();
        workbookPart.Workbook = new Workbook();

        WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
        worksheetPart.Worksheet = new Worksheet();

        Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());

        Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Employees" };



        List<Employee> employees = Employees.EmployeesList;

        SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());

        // Constructing header
        Row row = new Row();

            ConstructCell("Id", CellValues.String),
            ConstructCell("Name", CellValues.String),
            ConstructCell("Birth Date", CellValues.String),
            ConstructCell("Salary", CellValues.String));

        // Insert the header row to the Sheet Data

        // Inserting each employee
        foreach (var employee in employees)
            row = new Row();

                ConstructCell(employee.Id.ToString(), CellValues.Number),
                ConstructCell(employee.Name, CellValues.String),
                ConstructCell(employee.DOB.ToString("yyyy/MM/dd"), CellValues.String),
                ConstructCell(employee.Salary.ToString(), CellValues.Number));



private Cell ConstructCell(string value, CellValues dataType)
    return new Cell()
        CellValue = new CellValue(value),
        DataType = new EnumValue<CellValues>(dataType)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JedidiaAuthor Commented:
Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.