Answered by:
Add Text to Excel Using EPPlus

Question
-
User-471962099 posted
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 { using (ExcelRange rng1 = ws.Cells["A1:A3"]) { rng1.Value = "content not found"; ws.Cells["A1:A3"].Merge = true; ws.Cells["A1:A3"].Style.VerticalAlignment = ExcelVerticalAlignment.Top; ws.Cells["A1:A3"].Style.Border.Top.Style = ExcelBorderStyle.Thin; ws.Cells["A1:A3"].Style.Border.Left.Style = ExcelBorderStyle.Thin; ws.Cells["A1:A3"].Style.Border.Right.Style = ExcelBorderStyle.Thin; ws.Cells["A1:A3"].Style.Border.Bottom.Style = ExcelBorderStyle.Thin; ws.Cells["A1:A3"].Style.Fill.PatternType = ExcelFillStyle.Solid; ws.Cells["A1:A3"].Style.Fill.BackgroundColor.SetColor(System.Drawing.ColorTranslator.FromHtml("#f0f3f5")); ws.Cells["A1:A3"].Value = "content not found"; } } else { ImageCount++; System.Drawing.Image Image1 = System.Drawing.Image.FromFile(img); var ADDImage1 = ws.Drawings.AddPicture("Chart" + ImageCount.ToString(), Image1); Image1.Dispose(); // 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 } else { 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!
Friday, October 30, 2015 1:29 AM
Answers
-
User-219423983 posted
Hi Felicia Tan,
In you code, I think you’d better place the “Directory.GetFiles(subdir).Length == 0” outside the foreach loop as below.
if(Directory.GetFiles(subdir).Length == 0) { //Here, I think the "using code" could be removed //and just retain ws.Cells["A1:A3"] settings using (ExcelRange rng1 = ws.Cells["A1:A3"]) { } } else { foreach (string img in Directory.GetFiles(subdir)) //foreach image in sub directory { ImageCount++; System.Drawing.Image Image1 = System.Drawing.Image.FromFile(img); //...... } }
I hope it’s useful to you.
Best Regard,
Weibo Zhang
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, November 4, 2015 6:56 AM