C# 使用 EPPlus 将图像添加到 Excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11588704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:28:28  来源:igfitidea点击:

Adding images into Excel using EPPlus

c#epplus

提问by matthewr

I am trying to add the same image multiple times into an excel file using EPPlus. I am using the following code to do so:

我正在尝试使用 EPPlus 多次将相同的图像添加到 excel 文件中。我正在使用以下代码来执行此操作:

Image logo = Image.FromFile(path);
ExcelPackage package = new ExcelPackage(info);
var ws = package.Workbook.Worksheets.Add("Test Page");
for(int a = 0; a < 5; a++)
{
    ws.Row(a*5).Height = 39.00D;
    var picture = ws.Drawings.AddPicture(a.ToString(), logo);
    picture.SetPosition(a*5, 0, 2, 0);
}

Everything works perfectly and all the images are correctly added but they are stretched downwards. Here is what one of the pictures should look like:

一切正常,所有图像都正确添加,但它们向下拉伸。这是其中一张图片的样子:

enter image description here

在此处输入图片说明

But it looks like this in excel:

但是在excel中是这样的:

enter image description here

在此处输入图片说明

I have to resize each row of the start of each picture but I dont think that would be affecting it. Would there be a way to add the pictures/do what I am trying to do or would I have to copy-paste the images in manually? (I am using the picture as an example)

我必须调整每张图片开头的每一行的大小,但我认为这不会影响它。有没有办法添加图片/做我想做的事情,还是我必须手动复制粘贴图像?(我以图片为例)

Thanks.

谢谢。

采纳答案by Hyman7

I'm not sure if this is the best solution but definetly a workaround for your problem.

我不确定这是否是最好的解决方案,但绝对是您问题的解决方法。

Here's what I did:

这是我所做的:

ExcelPackage package = new ExcelPackage();
var ws = package.Workbook.Worksheets.Add("Test Page");

for (int a = 0; a < 5; a++)
{
    ws.Row(a * 5).Height = 39.00D;
}

for (int a = 0; a < 5; a++)
{
    var picture = ws.Drawings.AddPicture(a.ToString(), logo);
    picture.SetPosition(a * 5, 0, 2, 0);
}

Here is how it looks.

这是它的外观。

enter image description here

在此处输入图片说明

For some reason when we have the row height set, its interfering with the picture height.

出于某种原因,当我们设置了行高时,它会干扰图片高度。

回答by Tushar Chhabhaiya

when you are passing say example 39 as pixel it will take it as point insted of pixel internally, so you think that you are going to set row's height to 39 pixel but actually it is setting row's height to 39 point. so according to following formula your row height will became 52 pixel.

当您将示例 39 作为像素传递时,它将在内部将其作为像素的点插入,因此您认为您要将行的高度设置为 39 像素,但实际上它将行的高度设置为 39 点。所以根据以下公式,您的行高将变为 52 像素。

If you want to set row's height to 39px, mean you have to pass 29.25 Point(according to formula) insted of 39.

如果要将行的高度设置为 39px,则意味着您必须传递 39 的 29.25 点(根据公式)。

points = pixels * 72 / 96

Try this one.

试试这个。

回答by S3ddi9

try this

尝试这个

Image logo = Image.FromFile(path);
ExcelPackage package = new ExcelPackage(info);
var ws = package.Workbook.Worksheets.Add("Test Page");
for(int a = 0; a < 5; a++)
{
    ws.Row(a*5).Height = 39.00D;
    var picture = ws.Drawings.AddPicture(a.ToString(), logo);
    // xlMove disables the auto resizing
    picture.Placement = xlMove; //XLPlacement : xlMoveAndSize,xlMove,xlFreeFloating
    picture.SetPosition(a*5, 0, 2, 0);
}

or

或者

Image logo = Image.FromFile(path);
ExcelPackage package = new ExcelPackage(info);
var ws = package.Workbook.Worksheets.Add("Test Page");
for(int a = 0; a < 5; a++)
{
    ws.Row(a*5).Height = 39.00D;
    var picture = ws.Drawings.AddPicture(a.ToString(), logo);
    picture.From.Column = 0;
    picture.From.Row = a;
    picture.SetSize(120, 150);
}

回答by user1125572

Add the following right before you save the document:

在保存文档之前添加以下内容:

foreach (ExcelPicture drawing in ws.Drawings)
   drawing.SetSize(100);

回答by Bharat Pandey

use the below code to adjust the image in an excel cell:

使用以下代码调整 Excel 单元格中的图像:

        Image logo = Image.FromFile(path);
        ExcelPackage package = new ExcelPackage(info);
        var ws = package.Workbook.Worksheets.Add("Test Page");
        for(int a = 0; a < 5; a++)
        {
           ws.Row(a*5).Height = 39.00D;
           var picture = ws.Drawings.AddPicture(a.ToString(), logo);
           picture.From.Column = 0;
           picture.From.Row = a;
           picture.To.Column=0;//end cell value
           picture.To.Row=a;//end cell value
           picture.SetSize(120, 150);
        }

回答by Jhonny Nina

This is one solution that you can apply in C#.

这是您可以在 C# 中应用的一种解决方案。

private void AddImage(ExcelWorksheet oSheet, int rowIndex, int colIndex, string imagePath)
{
    Bitmap image = new Bitmap(imagePath);
    ExcelPicture excelImage = null;
    if (image != null)
    {
        excelImage = oSheet.Drawings.AddPicture("Debopam Pal", image);
        excelImage.From.Column = colIndex;
        excelImage.From.Row = rowIndex;
        excelImage.SetSize(100, 100);
        // 2x2 px space for better alignment
        excelImage.From.ColumnOff = Pixel2MTU(2);
        excelImage.From.RowOff = Pixel2MTU(2);
    }
}

public int Pixel2MTU(int pixels)
{
    int mtus = pixels * 9525;
    return mtus;
}