设置一个排的上EPPlus高度时,怪异的行为 [英] Weird behavior when setting a row's height on EPPlus

查看:1724
本文介绍了设置一个排的上EPPlus高度时,怪异的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立的Excel与EEPlus文件下的MVC-5 C#应用程序。一切按计划进行,直到我上设置一排(所以图像可以适合)的高度。

I am building an Excel file with EEPlus under MVC-5 C# application. Everything goes as planned until I set a height on a row (so an image can fit).

我负荷,图像和设置高度上列20,像这样:

I load de images and set the height on column 20, like so:

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单元格)的顶部有一个公司的标志。这是这样定义的:

But, I have a company logo at the top of the Excel file (A1 cell) which gets resized as well (on height). That is defined like this:

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);

致使在此:

任何帮助将是非常美联社preciated。

Any help would be really appreciated.

这里是有问题的Excel文件。

Here is the excel file in question.

推荐答案

它归结到图象标识的 EditAs 设置。默认情况下,将设置为 OneCell ,但将它设置为 TwoCell 我相信会解决你的问题。它(看EPP 4.0.1)文档是非常模糊的,但它基本上是说这个设置会告诉图纸保持其原有的行/列位置和大小。该名称似乎有点直觉虽然。

It comes down to the EditAs setting of the picture logo. By default it will be set to OneCell but setting it to TwoCell I believe will solve your problem. The documentation on it (looking at EPP 4.0.1) is rather cryptic but it basically says this setting will tell the drawing to maintain its original row/column position and size. The names seem a bit counter intuitive though.

我猜你的code样子(让我知道如果我听错了),我是能够重现您遇到的问题,然后用 EditAs 设置:

I had to guess what your code looks like (let me know if I got it wrong) and I was able to reproduce the problem you were having and then solve with the EditAs setting:

[TestMethod]
public void Image_Stretch_Test()
{
    //http://stackoverflow.com/questions/27873762/weird-behavior-when-setting-a-rows-height-on-epplus
    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();
    }
}

这修复它是加贴标识后的行调整,但不知道这是可行的你正在尝试做其他的事情。

The other thing that fix it was add the logo AFTER the row resize but not sure if that is practical to what you are trying to do.

这篇关于设置一个排的上EPPlus高度时,怪异的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆