Creating Multiple work sheet from multiple thread throwing error root level is invalid

T.Zacks 3,996 Reputation points
2021-04-28T17:58:16.497+00:00

I am using EPPlus to generate excel file with multiple sheets.I am trying to add multiple worksheet to a single excel file from different thread and getting ambiguous error message root level is invalid

Here is my code. please some one have look and tell me what i am doing wrong in my code ?

ExcelPackage excelPackage = null;  
private void button1_Click(object sender, EventArgs e)  
{  
    string exclpath = @"d:\Test1.xlsx";  
  
    FileInfo fi = new FileInfo(@exclpath);  
    excelPackage = new ExcelPackage(fi);  
  
    Action[] actions;  
  
    actions = new Action[]  
    {  
        () => Test1(),  
        () => Test2()  
    };  
  
    Parallel.Invoke(actions);  
    excelPackage.Save();  
}  
  
private void Test1()  
{  
    OfficeOpenXml.ExcelWorksheet ws = excelPackage.Workbook.Worksheets.Add("Delta");  
    ws.Cells[1, 1].Value = "Test Delta";  
}  
  
private void Test2()  
{  
    OfficeOpenXml.ExcelWorksheet ws = excelPackage.Workbook.Worksheets.Add("Horizontal");  
    ws.Cells[1, 1].Value = "Test Horizontal";  
}  

Please guide me how can i add multiple sheets with data from different thread into single excel file. is it not possible ?

if possible please guide me. Thanks

Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-04-29T06:11:45.147+00:00

    According to their documentation, you need to add LicenseContext to the code.

     ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.Commercial;  
     ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;  
    

    When I test this code, if I call Test1 or Test2 alone, there will be no problems with the code, but when I call both at the same time, the generated excel file will look like this:

    92465-1.png

    This seems to be a problem caused by this package. I suggest you go to the github repository they provide to ask questions about this package.

    In addition, do you consider other packages, such as OpenXML, I used this package to write a similar code:

           static SpreadsheetDocument spreadsheetDocument;  
            static Sheets sheets;  
            static WorksheetPart worksheetPart;  
            public static void M1()  
            {  
                string filePath = @"d:\test\excel\output.xlsx";  
                spreadsheetDocument = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook);  
      
                WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();  
                workbookpart.Workbook = new Workbook();  
                sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());  
                worksheetPart = workbookpart.AddNewPart<WorksheetPart>();  
                worksheetPart.Worksheet = new Worksheet(new SheetData());  
      
                Action[] actions;  
      
                actions = new Action[]  
                {  
                     () => WorkM1(),  
                     () => WorkM2()  
                };  
      
                Parallel.Invoke(actions);  
      
                workbookpart.Workbook.Save();  
                spreadsheetDocument.Close();  
            }  
            public static void WorkM1()   
            {  
                Sheet sheet = new Sheet()  
                {  
                    Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),  
                    SheetId = 2,  
                    Name = new StringValue("Sheet1")  
                };  
                sheets.Append(sheet);  
            }  
            public static void WorkM2()  
            {  
                Sheet sheet = new Sheet()  
                {  
                    Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),  
                    SheetId = 3,  
                    Name = new StringValue("Sheet2")  
                };  
                sheets.Append(sheet);  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.