Adding images into Excel using EPPlus

百般思念 提交于 2019-11-26 20:54:12
Jack7

I'm not sure if this is the best solution but definetly a workaround for your problem.

Here's what I did:

ExcelPackage package = new ExcelPackage(); var ws = package.Workbook.Worksheets.Add("Test Page");  for (int a = 0; a < 5; a++) {     ws.Row(a * 5).Height = 39.00D; }  for (int a = 0; a < 5; a++) {     var picture = ws.Drawings.AddPicture(a.ToString(), logo);     picture.SetPosition(a * 5, 0, 2, 0); } 

Here is how it looks.

For some reason when we have the row height set, its interfering with the picture height.

Jhonny Nina

This is one solution that you can apply in C#.

private void AddImage(ExcelWorksheet oSheet, int rowIndex, int colIndex, string imagePath) {     Bitmap image = new Bitmap(imagePath);     ExcelPicture excelImage = null;     if (image != null)     {         excelImage = oSheet.Drawings.AddPicture("Debopam Pal", image);         excelImage.From.Column = colIndex;         excelImage.From.Row = rowIndex;         excelImage.SetSize(100, 100);         // 2x2 px space for better alignment         excelImage.From.ColumnOff = Pixel2MTU(2);         excelImage.From.RowOff = Pixel2MTU(2);     } }  public int Pixel2MTU(int pixels) {     int mtus = pixels * 9525;     return mtus; } 

try this

Image logo = Image.FromFile(path); ExcelPackage package = new ExcelPackage(info); var ws = package.Workbook.Worksheets.Add("Test Page"); for(int a = 0; a < 5; a++) {     ws.Row(a*5).Height = 39.00D;     var picture = ws.Drawings.AddPicture(a.ToString(), logo);     // xlMove disables the auto resizing     picture.Placement = xlMove; //XLPlacement : xlMoveAndSize,xlMove,xlFreeFloating     picture.SetPosition(a*5, 0, 2, 0); } 

or

Image logo = Image.FromFile(path); ExcelPackage package = new ExcelPackage(info); var ws = package.Workbook.Worksheets.Add("Test Page"); for(int a = 0; a < 5; a++) {     ws.Row(a*5).Height = 39.00D;     var picture = ws.Drawings.AddPicture(a.ToString(), logo);     picture.From.Column = 0;     picture.From.Row = a;     picture.SetSize(120, 150); } 

Add the following right before you save the document:

foreach (ExcelPicture drawing in ws.Drawings)    drawing.SetSize(100); 
Bharat Pandey

use the below code to adjust the image in an excel cell:

        Image logo = Image.FromFile(path);         ExcelPackage package = new ExcelPackage(info);         var ws = package.Workbook.Worksheets.Add("Test Page");         for(int a = 0; a < 5; a++)         {            ws.Row(a*5).Height = 39.00D;            var picture = ws.Drawings.AddPicture(a.ToString(), logo);            picture.From.Column = 0;            picture.From.Row = a;            picture.To.Column=0;//end cell value            picture.To.Row=a;//end cell value            picture.SetSize(120, 150);         } 

when you are passing say example 39 as pixel it will take it as point insted of pixel internally, so you think that you are going to set row's height to 39 pixel but actually it is setting row's height to 39 point. so according to following formula your row height will became 52 pixel.

If you want to set row's height to 39px, mean you have to pass 29.25 Point(according to formula) insted of 39.

points = pixels * 72 / 96 

Try this one.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!