码迷,mamicode.com
首页 > 其他好文 > 详细

EPPlus实现Excel工作簿中插入图片

时间:2020-06-20 17:08:51      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:win   nts   fse   play   ddp   高度   class   bytes   round   

插入图片主方法代码:

 1 /// <summary>
 2 /// 插入图片
 3 /// </summary>
 4 /// <param name="worksheet"></param>
 5 /// <param name="imageBytes"></param>
 6 /// <param name="rowNum"></param>
 7 /// <param name="columnNum"></param>
 8 /// <param name="autofit"></param>
 9 public static void InsertImage(ExcelWorksheet worksheet, byte[] imageBytes, int rowNum, int columnNum, bool autofit)
10 {
11     using (var image = Image.FromStream(new MemoryStream(imageBytes)))
12     {
13         var picture = worksheet.Drawings.AddPicture($"image_{DateTime.Now.Ticks}", image);
14         var cell = worksheet.Cells[rowNum, columnNum];
15         int cellColumnWidthInPix = GetWidthInPixels(cell);
16         int cellRowHeightInPix = GetHeightInPixels(cell);
17         int adjustImageWidthInPix = cellColumnWidthInPix;
18         int adjustImageHeightInPix = cellRowHeightInPix;
19         if (autofit)
20         {
21             //图片尺寸适应单元格
22             var adjustImageSize = GetAdjustImageSize(image, cellColumnWidthInPix, cellRowHeightInPix);
23             adjustImageWidthInPix = adjustImageSize.Item1;
24             adjustImageHeightInPix = adjustImageSize.Item2;
25         }
26         //设置为居中显示
27         int columnOffsetPixels = (int)((cellColumnWidthInPix - adjustImageWidthInPix) / 2.0);
28         int rowOffsetPixels = (int)((cellRowHeightInPix - adjustImageHeightInPix) / 2.0);
29         picture.SetSize(adjustImageWidthInPix, adjustImageHeightInPix);
30         picture.SetPosition(rowNum - 1, rowOffsetPixels, columnNum - 1, columnOffsetPixels);
31     }
32 }

 

GetAdjustImageSize方法:

技术图片
 1 /// <summary>
 2 /// 获取自适应调整后的图片尺寸
 3 /// </summary>
 4 /// <param name="image"></param>
 5 /// <param name="cellColumnWidthInPix"></param>
 6 /// <param name="cellRowHeightInPix"></param>
 7 /// <returns>item1:调整后的图片宽度; item2:调整后的图片高度</returns>
 8 private static Tuple<int, int> GetAdjustImageSize(Image image, int cellColumnWidthInPix, int cellRowHeightInPix)
 9 {
10     int imageWidthInPix = image.Width;
11     int imageHeightInPix = image.Height;
12     //调整图片尺寸,适应单元格
13     int adjustImageWidthInPix;
14     int adjustImageHeightInPix;
15     if (imageHeightInPix * cellColumnWidthInPix > imageWidthInPix * cellRowHeightInPix)
16     {
17         //图片高度固定,宽度自适应
18         adjustImageHeightInPix = cellRowHeightInPix;
19         double ratio = (1.0) * adjustImageHeightInPix / imageHeightInPix;
20         adjustImageWidthInPix = (int)(imageWidthInPix * ratio);
21     }
22     else
23     {
24         //图片宽度固定,高度自适应
25         adjustImageWidthInPix = cellColumnWidthInPix;
26         double ratio = (1.0) * adjustImageWidthInPix / imageWidthInPix;
27         adjustImageHeightInPix = (int)(imageHeightInPix * ratio);
28     }
29     return new Tuple<int, int>(adjustImageWidthInPix, adjustImageHeightInPix);
30 }
View Code

 

GetWidthInPixels方法:

技术图片
 1 /// <summary>
 2 /// 获取单元格的宽度(像素)
 3 /// </summary>
 4 /// <param name="cell"></param>
 5 /// <returns></returns>
 6 private static int GetWidthInPixels(ExcelRange cell)
 7 {
 8     double columnWidth = cell.Worksheet.Column(cell.Start.Column).Width;
 9     Font font = new Font(cell.Style.Font.Name, cell.Style.Font.Size, FontStyle.Regular);
10     double pxBaseline = Math.Round(MeasureString("1234567890", font) / 10);
11     return (int)(columnWidth * pxBaseline);
12 }
View Code

 

GetHeightInPixels方法:

技术图片
 1 /// <summary>
 2 /// 获取单元格的高度(像素)
 3 /// </summary>
 4 /// <param name="cell"></param>
 5 /// <returns></returns>
 6 private static int GetHeightInPixels(ExcelRange cell)
 7 {
 8     double rowHeight = cell.Worksheet.Row(cell.Start.Row).Height;
 9     using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
10     {
11         float dpiY = graphics.DpiY;
12         return (int)(rowHeight * (1.0 / DEFAULT_DPI) * dpiY);
13     }
14 }
View Code

 

MeasureString方法:

技术图片
 1 /// <summary>
 2 /// MeasureString
 3 /// </summary>
 4 /// <param name="s"></param>
 5 /// <param name="font"></param>
 6 /// <returns></returns>
 7 private static float MeasureString(string s, Font font)
 8 {
 9     using (var g = Graphics.FromHwnd(IntPtr.Zero))
10     {
11         g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
12         return g.MeasureString(s, font, int.MaxValue, StringFormat.GenericTypographic).Width;
13     }
14 }
View Code

 

EPPlus实现Excel工作簿中插入图片

标签:win   nts   fse   play   ddp   高度   class   bytes   round   

原文地址:https://www.cnblogs.com/miaosha5s/p/13168868.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!