IRONSOFTWAREHOME

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

Curtis Chau
Curtis Chau
Updated: 2026年4月22日

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

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

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

  1. 1Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. 2複製並運行這段程式碼片段。

    workSheet.InsertImage("logo.png", 1, 1, 3, 3);
    workSheet.RemoveImage(1);
    var firstImage = workSheet.Images[0];
    C#
  3. 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 等奇數序列。

此奇數序列對於日後進行圖片擷取或移除操作時,引用特定圖片至關重要。

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");

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 檔案中的視覺資產進行分類與管理的系統時,將展現無可取代的價值。 透過程式化方式擷取圖片的功能,亦能支援自動化品質控管流程,在這些流程中,圖片需依據特定業務規則進行驗證或處理。

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");
檔案總管顯示 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 建立關聯,以便更輕鬆地進行管理。

using IronXL;

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

// Remove image
workSheet.RemoveImage(3);

workBook.SaveAs("removeImage.xlsx");

若涉及包含多個工作表與圖片的更複雜情境,請參閱"管理工作表"以了解圖片操作如何與工作表層級的操作相互作用。 此外,在處理受保護的 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文件中的圖像。

Does IronXL require Excel Interop to manage images in Excel files?

No, IronXL does not require Excel Interop. It fully supports adding, extracting, and removing images from Excel files programmatically, eliminating the need for Microsoft Office dependencies.

How can I retain the position of images when extracting them from Excel files with IronXL?

When extracting images with IronXL, you can retrieve their position data, including row and column indices, to preserve layout and positioning information.

Why would I use IronXL for image manipulation in Excel workbooks?

IronXL simplifies image manipulation in Excel, allowing for consistent placement, extraction, and removal of images programmatically without reliance on manual processes or Excel Interop.

Can IronXL handle images in password-protected Excel files?

Yes, IronXL can work with password-protected Excel files. You may need to manage worksheet-level operations to ensure compatibility with image manipulation.

Curtis Chau
技術作家

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

...
閱讀更多

準備好開始了嗎?

Nuget Downloads 2,237,574版本:2026.9剛剛發布

立即獲取免費

立即獲取 30天試用金鑰

bullet_checked無需信用卡或註冊帳號
bullet_test在生產
環境中進行測試,且不顯示浮水印
bullet_calendar30 天全
功能產品
bullet_support試用期間提供 24/5 技術
支援
立即獲取您的免費30天試用金鑰
無需信用卡或帳戶建立
PDF的C# NuGet程式庫
NuGet安裝

版本: 2026.9

PM > Install-Package IronXL.Excel
nuget.org/packages/IronXL.Excel/
  1. 在方案資源管理器中,右鍵單擊參考,管理NuGet包
  2. 選擇瀏覽並搜尋“IronXL”
  3. 選擇包並安裝
C# PDF DLL
下載DLL

版本: 2026.9

  1. 下載並解壓IronXL到您的方案目錄中的比如~/Libs位置
  2. 在Visual Studio方案資源管理器中,右鍵單擊參考。選擇瀏覽,“IronXL.dll”

授權從$999

有問題嗎?聯絡我們的開發團隊。

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預約您的免費即時演示
Booking Badge

全球數百萬工程師的信賴

Iron Software的客戶標誌
獲得無義務諮詢
填寫以下表格或電郵sales@ironsoftware.com
您的資料將始終保密。
全球數百萬工程師的信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立