我正在使用EEPlus在MVC-5 C#应用程序下构建一个Excel文件。一切都按计划进行,直到我在一行上设置一个高度(因此图像可以适合)。
我加载de图像并在第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文件。
答案 0 :(得分:7)
归结为图片徽标的EditAs
设置。默认情况下,它会设置为OneCell
,但将其设置为TwoCell
我相信会解决您的问题。它上面的文档(查看EPP 4.0.1)相当神秘,但它基本上说这个设置会告诉图形保持其原始的行/列位置和大小。这些名字似乎有点反直觉。
我不得不猜测你的代码是什么样的(让我知道如果我弄错了)我能够重现你遇到的问题,然后用EditAs
设置解决:
[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();
}
}
修复它的另一件事是在行调整大小后添加徽标,但不确定这是否适用于您要执行的操作。