使用 EPplus 将文本添加到 Excel
Add Text to Excel Using EPplus
我有一个包含许多子目录的主目录。在每个子目录中都会有图像。我已经设法将每个子目录中的图像导出到 excel 工作簿中的每个 excel 电子表格中。例如,如果我有 10 个子目录,将有 1 个 excel 工作簿和 10 个 excel 电子表格,在每个电子表格中将有每个子目录的图像。
我现在要完成的是,如果任何子目录中没有图像,导出到excel电子表格的子目录将是空白的。我想 将 "No image found" 作为文本添加到那个空白 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
循环的工作原理..
如果你的 count
是 0
你在 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
}
我有一个包含许多子目录的主目录。在每个子目录中都会有图像。我已经设法将每个子目录中的图像导出到 excel 工作簿中的每个 excel 电子表格中。例如,如果我有 10 个子目录,将有 1 个 excel 工作簿和 10 个 excel 电子表格,在每个电子表格中将有每个子目录的图像。
我现在要完成的是,如果任何子目录中没有图像,导出到excel电子表格的子目录将是空白的。我想 将 "No image found" 作为文本添加到那个空白 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
循环的工作原理..
如果你的 count
是 0
你在 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
}