如何從工作表中新增、提取和刪除圖像 | IronXL

如何使用 C# 在 Excel 中新增、擷取及移除圖片

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

IronXL 讓 C# 開發人員能夠透過程式化方式將圖片插入 Excel 試算表、擷取現有圖片及其屬性,並使用簡單的 API 方法移除不需要的圖片,且無需依賴 Excel Interop。 當在 .NET 環境中建立需要公司標誌、產品圖片或資料視覺化圖表等視覺元素的 Excel 檔案時,此功能至關重要。

新增圖片可透過相關圖表或插圖豐富內容。 移除圖片可簡化內容編輯與組織工作。 提取圖片可讓您將其重新用於其他文件或應用程式,並更新現有的圖片。 這些功能可讓您完全掌控 Excel 工作簿內的影像處理。

快速入門:一次完成圖片插入、擷取與移除 使用 IronXL 直觀的 API,只需幾行程式碼即可在試算表中新增、取得及刪除圖片。 此範例展示如何插入圖片、透過 Images 集合存取該圖片,以及移除圖片——所有操作皆無需使用 Interop。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronXL.Excel

    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


如何將圖片新增至 Excel 試算表?

若要將圖片插入試算表,請使用 InsertImage 方法,該方法支援多種圖片格式,包括 JPG/JPEG、BMP、PNG、GIF 及 TIFF。 此功能在您需要使用 C# 建立 Excel 圖表,並為其新增額外視覺元素時特別實用。 請指定圖片的左上角與右下角座標,以確定其尺寸,尺寸將透過減去列與行的數值來計算。

此方法的簽名需要五個參數:圖片檔案路徑,以及代表起始欄位、起始列、結束欄位和結束列的四個整數。 圖片會自動拉伸或壓縮,以適應定義的儲存格範圍。 例如:

  • 針對 1x1 尺寸的圖片:
    • worksheet.InsertImage("image.gif", 5, 1, 6, 2);
  • 針對 2x2 尺寸的圖片:
    • worksheet.InsertImage("image.gif", 5, 1, 7, 3);

在 Excel 中處理圖片時,請了解 IronXL 內部如何管理這些圖片。 每張插入的圖片都會獲得一個遵循特定模式的唯一 ID。

請注意生成的圖片 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

InsertImage 方法可讓您在工作表中靈活調整圖片的位置與大小。 與在 Excel 中手動插入圖片不同,程式化插入能確保在多個檔案中保持一致的排版位置,因此非常適合用於生成需要標準化格式的報告或文件。 此方法在 C# 中不使用 Interop 處理 Excel 時尤為有益,因為它消除了對 Microsoft Office 安裝的依賴。

插入的圖片在 Excel 中看起來是什麼樣子?

電子試算表中,C4 和 C7 儲存格已插入兩個色彩鮮豔的標誌,顯示圖片插入成功

如何從 Excel 檔案中擷取圖片?

To extract images from the selected worksheet, access the Images property, which provides a list of all images contained within the sheet. 當您需要在不使用 Interop情況下載入 Excel 檔案,並處理其內嵌的視覺內容時,此功能至關重要。 透過此清單,您可以執行各種操作,例如匯出、調整大小、取得位置,以及取得每張圖片的位元組資料。 圖片 ID 採用奇數編號模式,依 1、3、5、7 等序列遞增。

此擷取流程可全面存取影像屬性與資料,讓開發人員能夠:

  • 將圖片匯出為各種格式(PNG、JPEG、BMP 等)
  • 擷取圖片定位資訊以維持版面配置
  • 存取原始位元組資料以進行自訂處理或儲存
  • 無需外部影像處理函式庫,即可透過程式碼調整影像大小

此功能在不同文件格式間遷移內容,或建置需對 Excel 檔案中的視覺資產進行分類與管理的系統時,將展現無可取代的價值。 透過程式化方式擷取圖片的功能,亦能支援自動化品質控管流程,在這些流程中,圖片需依據特定業務規則進行驗證或處理。

: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
檔案總管顯示 Documents/Replicate/bin/Debug/net6.0 目錄中擷取的 PNG 影像與檔案
試算表,顯示三張擷取的影像分別置於儲存格 C2-E2、C5-E6 與 C8-E10,並附座標格線

如何從 Excel 試算表中移除圖片?

Following the extract images example, you can easily remove any inserted image using its corresponding index number. 將圖片的 ID 號傳遞給 RemoveImage 方法,即可將其從工作表中移除。 此操作在您需要使用 C# 編輯 Excel 檔案以清除不需要的視覺元素,或為不同受眾準備文件時特別有用。

移除流程雖然簡單,但需要理解圖片 ID 系統。 由於 IronXL 採用奇數序列(1、3、5、7...)分配 ID,在管理多張圖片時請追蹤這些 ID。 建議在您的應用程式中實作映射系統,將具意義的名稱與圖片 ID 建立關聯,以便更輕鬆地進行管理。

: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 檔案時,請參閱我們的《Excel 檔案保護指南,以了解圖片操作在密碼保護的工作簿中如何運作。

常見問題

我如何在C#中程式化地將圖像新增到Excel試算表中?

IronXL提供了InsertImage方法以將圖像新增到Excel工作表中。只需指定圖像文件的路徑和圖像要顯示的單元格範圍座標(左上角和右下角)。IronXL支持多種圖像格式,包括JPG/JPEG、BMP、PNG、GIF和TIFF。

在將圖像插入Excel時支持哪些圖像格式?

IronXL支持多個圖像格式插入到Excel工作表中,包括JPG/JPEG、BMP、PNG、GIF和TIFF。這種靈活性允許您處理業務文件和資料可視化中常用的各種圖像型別。

如何從Excel工作表中提取現有的圖像?

您可以使用IronXL的Images集合屬性從Excel工作表中提取圖像。存取worksheet.Images[index]即可獲取特定圖像及其屬性和中介資料,以便在其他文件或應用程式中重新利用它們。

我可以程式化地從Excel文件中刪除圖像嗎?

是的,IronXL提供了RemoveImage方法以從工作表中刪除圖像。只需提供圖像ID即可刪除特定圖像,讓您能夠完全控制圖像管理,無需Excel Interop。

如何指定插入圖像的大小和位置?

使用IronXL的InsertImage方法時,您需要指定四個座標:起始列、起始行、結束列和結束行。圖像將自動擴展或壓縮以適應定義的單元格範圍。例如,InsertImage("image.gif", 5, 1, 6, 2) 建立了一個1x1的圖像大小。

需要安裝Microsoft Excel才能操作試算表中的圖像嗎?

不需要,IronXL獨立運行,不需要Microsoft Excel或Excel Interop的依賴。您只需在.NET應用程式中使用IronXL程式庫即可程式化地新增、提取和刪除Excel文件中的圖像。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備好開始了嗎?
Nuget 下載 2,134,203 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

仍在滾動嗎?

想要快速證明嗎? PM > Install-Package IronXL.Excel
運行一個範例 觀看您的資料成為試算表。