图片从资源作为页脚在excel

本文关键字:excel 资源 | 更新日期: 2023-09-27 18:08:25

如何在创建的excel文件中使用来自资源的图像作为页脚?

这肯定行不通:

xlWorkSheet.PageSetup.CenterFooterPicture = Properties.Resources.stopka;

自:不能隐式转换类型"System.Drawing"。位图' to Microsoft.Office.Interop.Excel.Graphic'

Ok this works:

  xlWorkSheet.PageSetup.CenterFooterPicture.Filename = Application.StartupPath + "''stopka.png";
  xlWorkSheet.PageSetup.CenterFooterPicture.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;
  xlWorkSheet.PageSetup.CenterFooterPicture.Width = 590;
  xlWorkSheet.PageSetup.CenterFooter = "&G";

但这不是我需要的。我想从项目资源而不是从应用程序文件夹获取图像。

图片从资源作为页脚在excel

这行得通:

System.Reflection.Assembly CurrAssembly = System.Reflection.Assembly.LoadFrom(System.Windows.Forms.Application.ExecutablePath);
  System.IO.Stream stream = CurrAssembly.GetManifestResourceStream("Oferty_BMGRP.Resources.stopka.png");
  string temp = Path.GetTempFileName();
  System.Drawing.Image.FromStream(stream).Save(temp);

  xlWorkSheet.PageSetup.CenterFooterPicture.Filename = temp; //Application.StartupPath + "''Resources''stopka.png";
  xlWorkSheet.PageSetup.CenterFooterPicture.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;
  xlWorkSheet.PageSetup.CenterFooterPicture.Width = 590;
  xlWorkSheet.PageSetup.CenterFooter = "&G";

图片"stopka.png"必须设置为嵌入资源

由于microsoft没有为CenterFooterPicture属性提供set选项

以下是MSDN

中提供的文档

CenterFooterPicture -返回一个图形对象,表示页脚中间部分的图片。用于设置图片的相关属性。

链接到参考资料