簡體   English   中英

使用EPplus將文本添加到Excel

[英]Add Text to Excel Using EPplus

我有一個主目錄,其中包含許多子目錄。 在每個子目錄中都會有圖像。 我已設法將每個子目錄中的圖像導出到Excel工作簿中的每個Excel電子表格中。 例如,如果我有10個子目錄,那么將有1個excel工作簿和10個excel電子表格,在每個電子表格中將有每個子目錄的圖像。

我現在想要完成的是,如果任何子目錄中沒有圖像,導出到Excel電子表格的子目錄將為空。 我想在文本中添加“找不到圖像”這個空白的Excel電子表格。

這是我嘗試過的:

 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
                    }

我現在面臨的問題是當我將文本添加到特定的合並單元格時,如果我查看具有不同屏幕尺寸的電子表格,較小的屏幕尺寸將在電子表格的中心顯示文本,而較大的屏幕尺寸將顯示電子表格左側的文本。

這是我看到的樣本:

較小的屏幕尺寸(筆記本電腦): 在此輸入圖像描述 更大的屏幕尺寸(桌面): 在此輸入圖像描述 這是我想要實現的輸出(所有屏幕大小): 在此輸入圖像描述 請幫幫我,謝謝!

你必須了解foreach循環如何工作..
如果您的count0 ,則foreach中的代碼根本不會運行
您應該在foreach循環之前進行檢查以在工作表內寫入

將代碼放在foreach循環之前,如下所示

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM