1

I managed to create a workbook programmatically with the only input being an arbitrarily chosen excel file path. I encounter a System.NullReferenceException  when trying to create the Sheet object. I suspect that this is because my workbook doesn't have any sheets. How can I debug this?

using System;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.IO;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using System.Reflection;

using System.Linq;



namespace ConsoleApp35
{
    class Program
    {
        static void Main(string[] args)
        {

            {

                SpreadsheetDocument spreadsheetDocument =
                SpreadsheetDocument.Create(@"C:\________________.xlsx", SpreadsheetDocumentType.Workbook);
                spreadsheetDocument.AddWorkbookPart();

                {
                    // Add a WorksheetPart.  
                    WorksheetPart newWorksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart<WorksheetPart>();
                    newWorksheetPart.Worksheet = new Worksheet(new SheetData());

                    // Create Sheets object.  
                    Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>();
                    string relationshipId = spreadsheetDocument.WorkbookPart.GetIdOfPart(newWorksheetPart);

                    // Create a unique ID for the new worksheet.  
                    uint sheetId = 1;
                    if (sheets.Elements<Sheet>().Count() > 0)
                    {
                        sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
                    }

                    // Give the new worksheet a name.  
                    string sheetName = "mySheet" + sheetId;

                    // Append the new worksheet and associate it with the workbook.  
                    Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
                    sheets.Append(sheet);
                }
                Console.WriteLine("All done.");
                Console.ReadKey();
            }
        }
    }
}
1

1 Answer 1

1

Try this approach instead. There are just a few things I added/changed and I tested this.

Include references to:

  1. Microsoftbase
  2. DocumentFormat.OpenXML

static void Main(string[] args)
{
    string document = @"C:\Temp\MySpreadsheet.xlsx";

    // Open the document for editing.  
    using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(document, SpreadsheetDocumentType.Workbook,true))
    {
        // Add a WorkbookPart
        WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart();
        workbookPart.Workbook = new Workbook();

        // Add a WorksheetPart
        WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
        var sheetData = new SheetData();

        worksheetPart.Worksheet = new Worksheet(sheetData);
        workbookPart.Workbook.Append(new BookViews(new WorkbookView()));

        spreadsheetDocument.WorkbookPart.Workbook.Sheets = new Sheets();
        Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>();
        string relationshipId = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart);

        uint sheetId = 1;
        if (sheets.Elements<Sheet>().Count() > 0)
        {
            sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
        }

        Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = "TestSheet" };
        sheets.Append(sheet);

        WorkbookView workbookView = workbookPart.Workbook.Descendants<WorkbookView>().FirstOrDefault(); 

        workbookPart.Workbook.Save();
        spreadsheetDocument.Close();

        Console.WriteLine("All Done");
        Console.ReadKey();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Portland Runner. I ended up using ClosedXML since it was faster to export to excel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.