如何使用 C# 在Excel試算表中新增、擷取和刪除影像
IronXL使 C# 開發人員能夠以程式設計方式將影像插入建立Excel試算表時所需的工作表,提取現有影像及其屬性,並使用簡單的 API 方法刪除不需要的影像,而無需 Excel Interop 依賴項。 在.NET中建立需要公司標誌、產品圖像或資料視覺化圖形等視覺元素的 Excel 檔案時,此功能至關重要。
新增圖片可以用相關的圖形或插圖豐富資料。 移除圖片可以簡化內容編輯和組織。 提取圖像可以將其重新用於其他文件或應用程序,並更新現有圖像。 這些功能可以完全控制Excel工作簿中的影像處理。
快速入門:一次插入、提取和刪除圖像
使用 IronXL 直覺的 API,只需幾行程式碼即可在工作表中新增、取得和刪除影像。 本範例展示如何插入影像、透過 Images 集合存取影像以及刪除影像——所有這些都無需接觸 Interop。
最簡工作流程(5個步驟)
- 下載 C# 庫,用於在電子表格中插入、提取和刪除圖像。
- 匯入現有 Excel 檔案或建立新文件
- 使用`InsertImage`方法將影像插入工作表中。
- 訪問**Images**屬性以提取圖像及其信息
- 將圖像 ID 提供給`RemoveImage`方法即可刪除圖像。
如何在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。
: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")
InsertImage 方法可在工作表中靈活地定位和調整影像大小。 與 Excel 中手動插入影像不同,程式化插入可確保在多個檔案中保持一致的影像位置,因此非常適合產生需要標準化格式的報表或文件。 當在不使用 Interop 的情況下使用 C# 處理 Excel 時,這種方法尤其有益,因為它消除了對 Microsoft Office 安裝的依賴。
在 Excel 中插入的圖片是什麼樣子的?
如何從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")
提取的圖像
圖片尺寸
如何從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");
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")
對於涉及多個工作表和圖像的更複雜場景,請探索管理工作表,以了解圖像操作如何與工作表層級操作互動。 此外,在處理受保護的 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 檔案中新增、抽取和移除圖片。

