如何从工作表中添加、提取和删除图像 | IronXL

如何使用 C# 在 Excel 中添加、提取和删除图像

This article was translated from English: Does it need improvement?
Translated
View the article in English

用户可以通过添加图片,利用相关的图形或插图来丰富数据。 移除或删除图片可以简化内容编辑和整理。 此外,检索图像的功能允许在其他文档或应用程序中重新利用它们,以及更新现有图像。 这些功能共同增强了用户对图像的控制能力,改善了整体用户体验,并实现了在 Excel 工作簿中无缝处理图像。

快速入门:一次性插入、提取和删除图像

使用 IronXL 直观的 API,只需几行代码即可在工作表中添加、获取和删除图像。 此示例展示了插入图像、通过 Images 集合访问图像以及删除图像是多么容易——所有这些都无需接触 Interop。

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 IronXL

    PM > Install-Package IronXL.Excel

  2. 复制并运行这段代码。

    workSheet.InsertImage("logo.png", 1, 1, 3, 3);
    workSheet.RemoveImage(1);
    var firstImage = workSheet.Images[0];
  3. 部署到您的生产环境中进行测试

    立即开始在您的项目中使用 IronXL,免费试用!
    arrow pointer


添加图片示例

To insert an image into a spreadsheet, utilize the InsertImage method, which supports various image types, such as JPG/JPEG, BMP, PNG, GIF, and TIFF. 要确定图像的尺寸,必须指定图像的左上角和右下角,尺寸计算方法是用列值和行值相减。 例如,您可以尝试以下方法:

  • 对于 1x1 的图像尺寸:
    • worksheet.InsertImage("image.gif", 5, 1, 6, 2);
  • 对于 2x2 的图像尺寸:
    • worksheet.InsertImage("image.gif", 5, 1, 7, 3);

生成的图像 ID 遵循 1、3、5、7 等模式。

:path=/static-assets/excel/content-code-examples/how-to/add-remove-extract-worksheet-images-insert.cs
using IronXL;

WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Insert images
workSheet.InsertImage("ironpdf.jpg", 2, 2, 4, 4);
workSheet.InsertImage("ironpdfIcon.png", 2, 6, 4, 8);

workBook.SaveAs("insertImages.xlsx");
Imports IronXL

Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Insert images
workSheet.InsertImage("ironpdf.jpg", 2, 2, 4, 4)
workSheet.InsertImage("ironpdfIcon.png", 2, 6, 4, 8)

workBook.SaveAs("insertImages.xlsx")
$vbLabelText   $csharpLabel

输出电子表格

插入图片

提取图像示例

To extract images from the selected worksheet, simply access the Images property, which provides a list of all the images contained within the sheet. 通过此列表,您可以执行各种操作,例如导出、调整大小、检索位置以及获取每张图像的字节数据。 值得注意的是,图像 ID 遵循奇数模式,按 1、3、5、7 等顺序递增。

:path=/static-assets/excel/content-code-examples/how-to/add-remove-extract-worksheet-images-extract.cs
using IronSoftware.Drawing;
using IronXL;
using IronXL.Drawing;
using System;
using System.Collections.Generic;

WorkBook workBook = WorkBook.Load("insertImages.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Retreive images
List<IronXL.Drawing.Images.IImage> images = workSheet.Images;

// Select each image
foreach (IronXL.Drawing.Images.IImage image in images)
{
    // Save the image
    AnyBitmap anyBitmap = image.ToAnyBitmap();
    anyBitmap.SaveAs($"{image.Id}.png");

    // Resize the image
    image.Resize(1,3);

    // Retrieve image position
    Position position = image.Position;
    Console.WriteLine("top row index: " + position.TopRowIndex);
    Console.WriteLine("bottom row index: " + position.BottomRowIndex);

    // Retrieve byte data
    byte[] imageByte = image.Data;
}

workBook.SaveAs("resizeImage.xlsx");
Imports IronSoftware.Drawing
Imports IronXL
Imports IronXL.Drawing
Imports System
Imports System.Collections.Generic

Private workBook As WorkBook = WorkBook.Load("insertImages.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Retreive images
Private images As List(Of IronXL.Drawing.Images.IImage) = workSheet.Images

' Select each image
For Each image As IronXL.Drawing.Images.IImage In images
	' Save the image
	Dim anyBitmap As AnyBitmap = image.ToAnyBitmap()
	anyBitmap.SaveAs($"{image.Id}.png")

	' Resize the image
	image.Resize(1,3)

	' Retrieve image position
	Dim position As Position = image.Position
	Console.WriteLine("top row index: " & position.TopRowIndex)
	Console.WriteLine("bottom row index: " & position.BottomRowIndex)

	' Retrieve byte data
	Dim imageByte() As Byte = image.Data
Next image

workBook.SaveAs("resizeImage.xlsx")
$vbLabelText   $csharpLabel
提取的图像
图片尺寸

移除图像示例

Following the extract images example, you can easily remove any inserted image using its corresponding index number. Simply pass the image's ID number to the RemoveImage method to remove it from the worksheet.

:path=/static-assets/excel/content-code-examples/how-to/add-remove-extract-worksheet-images-remove.cs
using IronXL;

WorkBook workBook = WorkBook.Load("insertImages.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Remove image
workSheet.RemoveImage(3);

workBook.SaveAs("removeImage.xlsx");
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("insertImages.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Remove image
workSheet.RemoveImage(3)

workBook.SaveAs("removeImage.xlsx")
$vbLabelText   $csharpLabel

常见问题解答

如何将图像添加到 Excel 工作表中?

要使用 IronXL 将图像插入 Excel 工作表,请使用 InsertImage 方法。此方法允许您通过定义左上角和右下角来指定图像尺寸。支持的格式包括 JPG、BMP、PNG、GIF 和 TIFF。

在 Excel 中插入图像时支持哪些图像格式?

IronXL 支持多种图像格式以插入到 Excel 工作表中,包括 JPG/JPEG、BMP、PNG、GIF 和 TIFF。

如何从 Excel 工作表中提取图像?

要使用 IronXL 从 Excel 工作表中提取图像,请访问工作表的 Images 属性。这提供了所有图像的列表,使您能够导出、调整大小并检索其位置和字节数据。

我可以从 Excel 工作表中删除图像吗?

可以,使用 IronXL,您可以通过使用 RemoveImage 方法从 Excel 工作表中删除图像。只需将图像的 ID 传递给此方法即可将其删除。

使用 C# 开始操作 Excel 中的图像需要什么?

要开始使用 IronXL 操作 Excel 中的图像,从 NuGet 下载 IronXL C# 库并加载现有的 Excel 文件或创建一个新的。

在 IronXL 中如何生成图像 ID?

在 IronXL 中,图像 ID 按奇数模式生成,例如 1, 3, 5, 7 等。

能否导出从 Excel 工作表中提取的图像?

可以,一旦使用 IronXL 的 Images 属性提取图像后,可以使用 Export 方法导出为如 PNG 之类的格式。

如何在将图像插入 Excel 工作表时指定图像的大小?

使用 IronXL 将图像插入 Excel 工作表时,通过 InsertImage 方法提供图像的左上角和右下角以指定大小。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 1,738,553 | Version: 2025.11 刚刚发布