简体   繁体   English

如何在不提示替换(空)目标单元格(EPPlus)的内容的情况下将图像添加到工作表中?

[英]How can I add an image to my sheet without being prompted to replace contents of the (empty) destination cells (EPPlus)?

When I open the Excel file I generate using EPPlus (C#), I get " Do you want to replace the contents of the destination cells in [... .xlsx]PivotTable? " 当我打开使用EPPlus(C#)生成的Excel文件时,我得到“ 你想要替换[... .xlsx]数据透视表中目标单元格的内容吗?

As you can see in the screen shot below, there is nothing in the cell where I want the image to display. 正如您在下面的屏幕截图中看到的那样,单元格中没有任何内容可以显示图像。 Even when I push the image beyond the outer edge of the PivotTable, I get this message (I had been placing beneath column C/3). 即使我将图像推到数据透视表的外边缘之外,我也收到了这条消息(我一直放在C / 3列下面)。 When I select yes to the dialog quoted above, the image is finally placed on the sheet (if I select "no" it is not added), and the sheet looks like this: 当我在上面引用的对话框中选择“是”时,图像最终放在工作表上(如果我选择“否”,则不添加),工作表如下所示:

在此输入图像描述

This is the code I'm using to add the image: 这是我用来添加图片的代码:

int imageCol = _grandTotalsColumnPivotTable + 1; // -1; <= I really want it to be this
AddImage(pivotTableWorksheet, 1, imageCol);

. . .

private void AddImage(ExcelWorksheet oSheet, int rowIndex, int colIndex)
{
    var excelImage = oSheet.Drawings.AddPicture("PRO*ACT Logo", _logo);
    excelImage.From.Column = colIndex - 1;
    excelImage.From.Row = rowIndex - 1;
    excelImage.SetSize(130, 100);
    excelImage.From.ColumnOff = Pixel2MTU(2);
    excelImage.From.RowOff = Pixel2MTU(2);
}

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

How can I either get the message to not display (programmatically, this can't be something the user needs to do) and "just do it" or let it know that there is nothing in that location, anyway? 我怎么能得到消息不显示(以编程方式,这不是用户需要做的事情)和“只是做”或让它知道该位置没有任何东西,无论如何?

UPDATE UPDATE

This is apparently not the problem; 这显然不是问题; I tried to delete this post, but can't, since it has an answer. 我试图删除这篇文章,但不能,因为它有答案。 Even when I comment out the writing of the image, I get it. 即使我评论出图像的写作,我也明白了。 And trying to prevent messages like so: 并试图阻止这样的消息:

_xlApp.DisplayAlerts = false;

...does not work, either - I still get it. ......也行不通 - 我还是得到了。

Apparently the problem is elsewhere, but I don't know where. 显然问题出在其他地方,但我不知道在哪里。

UPDATE 2 更新2

It was a case of hidden data underneath the PivotTable; 这是数据透视表下隐藏数据的一个案例; once I got rid of that, the problem went away. 一旦我摆脱了这个问题,问题就消失了。 IOW, the problem was with my code, not with EPPlus. IOW,问题在于我的代码,而不是EPPlus。

I use this code to add an image to Excel. 我使用此代码将图像添加到Excel。 It does not asks me to replace content. 它没有要求我替换内容。 But I do not use PivotTables. 但我不使用数据透视表。 Maybe that would make a difference. 也许这会有所作为。

using (System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("test.png")))
{
     var excelImage = ws.Drawings.AddPicture("My Logo", image);
     excelImage.SetPosition(1, 0, 5, 0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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