使用EPPlus将图像添加到Excel Range

时间:2018-08-24 07:23:49

标签: excel office-interop epplus epplus-4

我正在使用Microsoft Interope库创建Excel工作表。目前,我正在迁移到EPPlus,以摆脱服务器上的excel应用程序使用。我正在使用以下方法将图像添加到Excel Range中,但是无法在EPPlus中实现相同的功能。

private void insertImage(Microsoft.Office.Interop.Excel.Range currCellRange, String imagePath)
        {
            currCellRange.MergeArea.Value2 = "";
            Microsoft.Office.Interop.Excel.Range rng = currCellRange.MergeArea;

            Image image = new Bitmap(imagePath);
            int originalWidth = image.Width;
            int originalHeight = image.Height;

            float fTop = float.Parse(rng.Top.ToString());
            float fLeft = float.Parse(rng.Left.ToString());
            float fWidth = float.Parse(rng.Width.ToString());
            float fHeight = float.Parse(rng.Height.ToString());

            // To preserve the aspect ratio
            float ratioX = (float)fWidth / (float)originalWidth;
            float ratioY = (float)fHeight / (float)originalHeight;
            float ratio = Math.Min(ratioX, ratioY);

            // New width and height based on aspect ratio
            float newWidth = (int)(originalWidth * ratio);
            float newHeight = (int)(originalHeight * ratio);

            // REduce width and height by 5 to show the border
            newWidth = newWidth - 5;
            newHeight = newHeight - 5;

            // New Top and Left
            float newLeft = (float)fLeft + (((float)fWidth - newWidth) / 2);
            float newTop = (float)fTop + (((float)fHeight - newHeight) / 2);

            currCellRange.Worksheet.Shapes.AddPicture(imagePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, newLeft, newTop, newWidth, newHeight);
            image.Dispose();
        }

我无法正确获得图像的选定范围的顶部,左侧,宽度和高度。如何在EPPlus中实现上述功能。

0 个答案:

没有答案