如何添加、提取和移除工作表中的圖片 | 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工作表中新增圖片?

To insert an image into a spreadsheet, use the InsertImage method, which supports various image types including JPG/JPEG, BMP, PNG, GIF, and 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");
$vbLabelText   $csharpLabel

InsertImage 方法可在工作表中靈活地定位和調整影像大小。 與 Excel 中手動插入影像不同,程式化插入可確保在多個檔案中保持一致的影像位置,因此非常適合產生需要標準化格式的報表或文件。 當在不使用 Interop 的情況下使用 C# 處理 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");
$vbLabelText   $csharpLabel
File explorer showing extracted PNG images and files in Documents/Replicate/bin/Debug/net6.0 directory
Spreadsheet showing three extracted images positioned in cells C2-E2, C5-E6, and C8-E10 with coordinate grid

如何從Excel工作表中刪除圖片?

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

移除過程很簡單,但需要了解影像辨識系統。 由於IronXL分配的 ID 是奇數序列(1、3、5、7…),因此在管理多個影像時,請追蹤這些 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");
$vbLabelText   $csharpLabel

對於涉及多個工作表和圖像的更複雜場景,請探索管理工作表,以了解圖像操作如何與工作表層級操作互動。 此外,在處理受保護的 Excel 檔案時,請參閱我們的Excel 文件保護指南,以了解影像操作如何與受密碼保護的工作簿一起使用。

常見問題解答

如何使用 C# 程式化地在 Excel 試算表中加入圖片?

IronXl.Excel 提供了 InsertImage 方法,可將圖片新增至 Excel 工作表。只需指定圖片檔案路徑,以及希望圖片出現的儲存格範圍座標(左上角和右下角)。IronXL 支援多種圖片格式,包括 JPG/JPEG、BMP、PNG、GIF 和 TIFF。

在 Excel 中插入圖片時,支援哪些圖片格式?

IronXl.Excel 支援多種影像格式,可插入 Excel 工作表中,包括 JPG/JPEG、BMP、PNG、GIF 和 TIFF。這種靈活性可讓您處理各種常用於商業文件和資料可視化的圖像類型。

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

您可以使用 IronXl.Excel 的 Images 集合屬性從 Excel 工作表中擷取圖片。存取 worksheet.Images[index],即可擷取特定圖片及其屬性和元資料,讓您可以在其他文件或應用程式中重新使用這些圖片。

我可以程式化地移除 Excel 檔案中的圖片嗎?

是的,IronXL 提供 RemoveImage 方法來刪除工作表中的圖片。只需提供圖片 ID 即可移除特定圖片,讓您完全控制圖片管理,而無需 Excel Interop。

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

使用 IronXL 的 InsertImage 方法時,您會指定四個座標:起始列、起始行、結束列和結束行。圖片會自動拉伸或壓縮,以符合定義的儲存格範圍。例如,InsertImage("image.gif", 5, 1, 6, 2) 會建立 1x1 大小的圖片。

我需要安裝 Microsoft Excel 才能在試算表中處理圖像嗎?

不,IronXl.Excel 可獨立運作,無需 Microsoft Excel 或 Excel Interop 依賴。您只需在您的 .NET 應用程式中使用 IronXL library,就能以程式化的方式從 Excel 檔案中新增、抽取和移除圖片。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

A PHP Error was encountered

Severity: Notice

Message: Undefined index: IronXl.Excel

Filename: helpers/counter_helper.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/helpers/counter_helper.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/views/main/sections/ready_to_started_202509.php
Line: 12
Function: getTotalDonwloadNumber

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 489
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Notice

Message: Undefined index: IronXl.Excel

Filename: helpers/counter_helper.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/helpers/counter_helper.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/views/main/sections/ready_to_started_202509.php
Line: 19
Function: getTotalDonwloadNumber

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 489
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 1,890,100 | 版本: 2026.3 剛剛發布

A PHP Error was encountered

Severity: Notice

Message: Undefined index: IronXl.Excel

Filename: helpers/counter_helper.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/helpers/counter_helper.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/views/main/sections/still_scrolling_202512.php
Line: 17
Function: getTotalDonwloadNumber

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 71
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Notice

Message: Undefined index: IronXl.Excel

Filename: helpers/counter_helper.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/helpers/counter_helper.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/views/main/sections/still_scrolling_202512.php
Line: 24
Function: getTotalDonwloadNumber

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 71
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronXl.Excel
執行範例 觀看您的資料變成試算表。