Click here to Skip to main content
15,747,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all,
I have a main directory which contains many sub directories. In each sub directory there will be images. I have managed to export images from each subdirectory into each excel spreadsheet in a excel workbook. For example, if I have 10 sub directories, there will be 1 excel workbook with 10 excel spreadsheets, in each spreadsheet there will be each sub directory's images.

What I want to accomplish now is if there is no image in any sub directory, the sub directory exported to an excel spreadsheet will be blank. I want to add "No image found" to that blank excel spreadsheet as text.

This is what I have tried but no text appear on the blank excel spreadsheet:
string[] filesindirectory = Directory.GetDirectories(Server.MapPath("~/StoreImage"));  //main directory path

int ImageCount = 0;

  foreach (string subdir in filesindirectory)  //foreach sub directory in main directory
                string[] splitter = subdir.Split('\\');
                string folderName = splitter[splitter.Length - 1];
                ExcelWorksheet ws = package.Workbook.Worksheets.Add("Worksheet-" + folderName); //create new worksheet
                ImageCount = 0;
                foreach (string img in Directory.GetFiles(subdir))  //foreach image in sub directory
                    if (Directory.GetFiles(subdir).Length == 0)   //if no image in that sub directory
                      ws.InsertRow(5, 2);

                        // Inserting values in the 5th row
                        ws.Cells["A5"].Value = "12010";
                        ws.Cells["B5"].Value = "Drill";
                        ws.Cells["C5"].Value = 20;
                        ws.Cells["D5"].Value = 8;

                        // Inserting values in the 6th row
                        ws.Cells["A6"].Value = "12011";
                        ws.Cells["B6"].Value = "Crowbar";
                        ws.Cells["C6"].Value = 7;
                        ws.Cells["D6"].Value = 23.48;                         
                        System.Drawing.Image Image1 = System.Drawing.Image.FromFile(img);
                        var ADDImage1 = ws.Drawings.AddPicture("Chart" + ImageCount.ToString(), Image1);
                        // Row, RowoffsetPixel, Column, ColumnOffSetPixel
                        if (ImageCount > 1)
                            ADDImage1.SetPosition(ImageFinalPosition, 0, 2, 0);
                            ADDImage1.SetSize(770, 450);
                            ImageFinalPosition += (ImagePosition + 1); // Add 1 to have 1 row of empty row
                            ADDImage1.SetPosition(ImageCount, 0, 2, 0);
                            ADDImage1.SetSize(770, 450);
                            ImageFinalPosition = (ImageCount + ImagePosition) + 1; // Add 1 to have 1 row of empty row

This is the output I want to achieve:[^]

Please help me on this, thanks!
Updated 29-Oct-15 20:08pm

1 solution

Share this answer
Member 11999641 30-Oct-15 2:10am    
hi VR Karthikeyan, I have tried the method in the link but still got no content appear in the blank excel spreadsheet. Please look at my updated post thanks.

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