在 EPPlus 上设置行高时的奇怪行为

Weird behavior when setting a row's height on EPPlus

我正在 MVC-5 C# 应用程序下使用 EEPlus 构建一个 Excel 文件。一切都按计划进行,直到我在一行上设置高度(这样图像才能适合)。

我加载图像并在第 20 列设置高度,如下所示:

Image cfPhoto = null;
Bitmap cfBm = null;
ExcelPicture pictureCf = null;
var photoInitialColumn = 0;
i++;
completedFormPhotos.ForEach(delegate(CompletedFormPhoto cfP)
{
    cfPhoto = Image.FromFile(HostingEnvironment.MapPath("~/Content/Images/FormPhotos/" + cfP.Id + ".jpg"));
    cfBm = new Bitmap(cfPhoto, new Size(215, 170));
    pictureCf = worksheet.Drawings.AddPicture(cfP.Id.ToString(), cfBm);
    pictureCf.SetPosition(i+1, 0, photoInitialColumn, 10);
    worksheet.Cells[_alpha[photoInitialColumn] + (i + 3) + ':' + _alpha[photoInitialColumn + 1] + (i + 3)].Merge = true;
    worksheet.Cells[_alpha[photoInitialColumn] + (i + 3) + ':' + _alpha[photoInitialColumn + 1] + (i + 3)].Value = cfP.comment;
    worksheet.Cells[_alpha[photoInitialColumn] + (i + 3) + ':' + _alpha[photoInitialColumn + 1] + (i + 3)].Style.WrapText = true;
    photoInitialColumn += 2;
    //HERE I SET THE HEIGHT. At this point, i == 18 
    worksheet.Row(i+2).Height = 180;
});

但是,我在 Excel 文件(A1 单元格)的顶部有一个公司徽标,它也被调整大小(在高度上)。定义如下:

Image _keyLogo = Image.FromFile(HostingEnvironment.MapPath("~/Content/Images/key_logo.png"));
var pictureLogo = worksheet.Drawings.AddPicture("Logo Key Quimica", _keyLogo);
pictureLogo.SetPosition(0, 0, 0, 0);

结果:

任何帮助将不胜感激。

Here 是有问题的 excel 文件。

归结为图片标志的EditAs设置。默认情况下,它将设置为 OneCell 但将其设置为 TwoCell 我相信会解决您的问题。它的文档(查看 EPP 4.0.1)相当含糊,但它基本上说此设置将告诉绘图保持其原始 row/column 位置和大小。不过,这些名称似乎有点违反直觉。

我不得不猜测您的代码是什么样的(如果我弄错了请告诉我)并且我能够重现您遇到的问题然后使用 EditAs 设置解决:

[TestMethod]
public void Image_Stretch_Test()
{
    //
    var existingFile = new FileInfo(@"c:\temp\temp.xlsx");
    if (existingFile.Exists)
        existingFile.Delete();

    using (var package = new ExcelPackage(existingFile))
    {
        var workbook = package.Workbook;
        var worksheet = workbook.Worksheets.Add("newsheet");

        var _keyLogo = Image.FromFile("C:/Users/Ernie/Desktop/key_logo.png");
        var pictureLogo = worksheet.Drawings.AddPicture("Logo Key Quimica", _keyLogo);
        pictureLogo.SetPosition(0, 0, 0, 0);
        pictureLogo.EditAs = eEditAs.TwoCell;  //REMOVE THIS TO SHOW THE STRETCH PROBLEM

        var cfPhoto = Image.FromFile("C:/Users/Ernie/Desktop/Main_Pic.png");
        var cfBm = new Bitmap(cfPhoto, new Size(215, 170));

        var pictureCf = worksheet.Drawings.AddPicture("Main_Pic", cfBm);
        pictureCf.SetPosition(10, 0, 0, 0);

        worksheet.Row(11).Height = 280;

        package.Save();
    }
}

修复它的另一件事是在调整行大小后添加徽标,但不确定这对您尝试执行的操作是否可行。