简体   繁体   中英

Add Text to Excel Using EPplus

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:

 foreach (string subdir in filesindirectory)
        {
            string[] splitter = subdir.Split('\\');
            string folderName = splitter[splitter.Length - 1];
            ExcelWorksheet ws = package.Workbook.Worksheets.Add("Worksheet-" + folderName); //create new worksheet
            ImageCount = 0;

            if (Directory.GetFiles(subdir).Length == 0)
                {
                    ws.Cells["J9:L10"].Merge = true;
                    ws.Cells["J9:L10"].Style.VerticalAlignment = ExcelVerticalAlignment.Top;
                    ws.Cells["J9:L10"].Value = "No chart to display";
                    ws.Cells["J9:L10"].Style.Font.Size = 16;
                }

            foreach (string img in Directory.GetFiles(subdir))
            {
                    ImageCount++;
                    System.Drawing.Image Image1 = System.Drawing.Image.FromFile(img);
                    var AddImage = ws.Drawings.AddPicture("Image" + ImageCount.ToString(), Image1 );
                    Image1 .Dispose();
                    // Row, RowoffsetPixel, Column, ColumnOffSetPixel
                    if (ImageCount > 1)
                    {
                        AddImage .SetPosition(ImageFinalPosition, 0, 2, 0);
                        AddImage .SetSize(770, 450);
                        ImageFinalPosition += (ImagePosition + 1); // Add 1 to have 1 row of empty row
                    }
                    else
                    {
                        AddImage.SetPosition(ImageCount, 0, 2, 0);
                        AddImage.SetSize(770, 450);
                        ImageFinalPosition = (ImageCount + ImagePosition) + 1; // Add 1 to have 1 row of empty row
                    }

The problem I faced now is when I add the text to specific merged cells, if I view the spreadsheet with different screen size, smaller screen size will display text at centre of spreadsheet while larger screen size will display the text on the left of the spreadsheet.

This is a sample of what I see:

Smaller screensize(laptop): 在此输入图像描述 Larger screensize(desktop): 在此输入图像描述 This is the output I want to achieve(all screensize): 在此输入图像描述 Please help me on this, thanks!

You must understand how foreach loop works..
If your count is 0 your code in foreach doesn't run at all
You should put your checking before the foreach loop to write inside the worksheet

Put your code before the foreach loop as per below

if (Directory.GetFiles(subdir).Length == 0)   //if no image in that sub directory
{
    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";
}

foreach (string img in Directory.GetFiles(subdir))
{
    // Your else code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM