Click here to Skip to main content
15,742,180 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi all,

I want to create new excel spreadsheet dynamically based on the no. of images there are inside my Image folder. If there are 10 images, I will have to create 10 excel spreadsheet in the same excel workbook and so on. I went researched online but only able to find how to add in 1 specific excel spreadsheet.

This is my codes:
C#
FileInfo newFile = new FileInfo("export.xlsx");
 ExcelPackage package = new ExcelPackage();

 string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Test"));
            int count = 0;
            foreach (string img in filesindirectory)
            {
                count++;
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet");

I want to loop through the Image folder and create new excel spreadsheet for each image inside the folder.

Question: How to create new excel Spreadsheet dynamically using epplus?

Appreciate if someone can guide me on this, thanks.
Posted

1 solution

I think this is what you want, you can append the count to Sheet name like below:
C#
int count = 0;
foreach (string img in filesindirectory) {
    count++;
    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet " + count);
    //work on your sheet
}                


If you want to hold reference to the worksheets for other use you can use an array:
C#
ExcelWorksheet[] worksheets = new ExcelWorksheet[no. of images]
int count = 0;
foreach (string img in filesindirectory) {
    worksheet[count] = package.Workbook.Worksheets.Add("Sheet " + (count + 1));
    //work on your sheet
    count++;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900