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:
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.