利用epplus在EXCEL中插入图片如何让其不随

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用epplus在EXCEL中插入图片如何让其不随相关的知识,希望对你有一定的参考价值。

参考技术A 使用C#在Excel中添加公式,需要用到Excel库,例如Spire.XLS, NPOI, EPPLUS。如果用spire, 代码如下:

//实例化WorkbookWorkbook wb = new Workbook(); //获取第一个工作表Worksheet sheet = wb.Worksheets[0]; //写入数字到A1-A4sheet.Range["A1"].NumberValue = 1;sheet.Range["A2"].NumberValue = 2;sheet.Range["A3"].NumberValue = 3;sheet.Range["A4"].NumberValue = 4; //定义求和公式string formula = "=SUM(A1:A4)"; //在单元格A5中应用公式sheet.Range["A5"].Formula = formula; //保存文档wb.SaveToFile("求和.xlsx", FileFormat.Version2013);

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

插入图片主方法代码:

 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中插入图片如何让其不随的主要内容,如果未能解决你的问题,请参考以下文章

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

.Net下C#针对Excel开发控件汇总(ClosedXML,EPPlus,NPOI)

在 EPPLUS RichText 中插入文本

每次我的邮箱登录时都会出现一个对话框怎么设置让其不出现?

如何让背景图片固定不随滚动条滚动

如何在 epplus 中使用 c# 为 excel 中的整个列着色?