Excel .NET组件Spire.XLS教程:在C#/VB.NET中替换 Excel 中的图片

有时在完成 Excel 工作簿后,您可能需要用更好的图片替换一些现有图片,以使工作簿更具吸引力和说服力。在本教程中,您将学习如何使用Spire.XLS for .NET替换Excel 中的图片。

*这么优秀的国产工具怎能错过呢!在线下单专享“一口价”,立即购买!想要获取更多福利的朋友可以咨询在线客服哦~

在 Excel 中替换图片

以下是使用 Spire.XLS for .NET 将一张图片替换为另一张图片的详细步骤。

  • 创建一个工作簿对象。
  • 使用Workbook.LoadFromFile()方法加载示例文件。
  • 使用Workbook.Worksheets[0]获取第一个工作表。
  • 使用Worksheet.Pictures[0]属性从第一个工作表中获取第一张图片
  • 使用Image.FromFile()方法从指定文件加载图像,并将其设置为ExcelPicture.Picture属性的新值。为此,原始图片将被替换为新图片。
  • 使用Workbook.SaveToFile()方法保存文档。

C#

using Spire.Xls;using Spire.Xls.Collections;using System.Drawing;namespace ReplacePictureinExcel{    class Program    {        static void Main(string[] args)        {            //Create a Workbook instance            Workbook workbook = new Workbook();            //Load the Excel file            workbook.LoadFromFile (“Input.xls”);            //Get the first sheet            Worksheet sheet = workbook.Worksheets[0];            //Get Excel picture collection            PicturesCollection pictureCollection = sheet.Pictures;            //Get the first picture from the collection            ExcelPicture excelPicture = pictureCollection[0];                // Creates an Image from the specified file.            excelPicture.Picture = Image.FromFile (image);            //Save the document            workbook.SaveToFile("ReplaceImage.xlsx", ExcelVersion.Version2013);        }    }}

VB.NET

Imports Spire.XlsImports Spire.Xls.CollectionsImports System.DrawingNamespace ReplacePictureinExcelClass ProgramPrivate Shared Sub Main(args As String())'Create a Workbook instanceDim workbook As New Workbook()'Load the Excel fileworkbook.LoadFromFile(Input.xls)'Get the first sheetDim sheet As Worksheet = workbook.Worksheets(0)'Get Excel picture collectionDim pictureCollection As PicturesCollection = sheet.Pictures'Get the first picture from the collectionDim excelPicture As ExcelPicture = pictureCollection(0)' Creates an Image from the specified file.excelPicture.Picture = Image.FromFile(image)'Save the documentworkbook.SaveToFile("ReplaceImage.xlsx", ExcelVersion.Version2013)End SubEnd ClassEnd Namespace

原始文件:

Excel .NET组件Spire.XLS教程:在C#/VB.NET中替换 Excel 中的图片

生成的文件:

Excel .NET组件Spire.XLS教程:在C#/VB.NET中替换 Excel 中的图片

是E-iceblue官方友好合作伙伴,如果您对spire.xls,可以联系在线客服了解具体授权价格和使用机制

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年10月7日
下一篇 2021年10月7日

相关推荐

发表回复

登录后才能评论