如何在C#中編輯Excel工作簿的中繼資料
在C#中使用IronXL的keywords和其他文件屬性,無需Microsoft Interop,實現自動化的電子表單組織和搜尋。
Excel電子表單的中繼資料包括modification date及其他相關細節的資訊。 中繼資料提供了背景,有助於組織和分類電子表單。 它簡化了檔案搜尋和管理,特別是當您處理多個電子表單檔案時。 無論您是在建立新的電子表單,還是載入現有的工作簿,IronXL使中繼資料管理變得無縫。
快速入門: 用一個簡單步驟編輯工作簿的中繼資料
使用IronXL的Metadata介面設定、修改和儲存像Keywords這樣的屬性。 不需要Interop,您可以只需幾行乾淨直觀的C#程式碼立即開始使用。
最小化工作流程(5步)
- 下載C#程式庫以編輯工作簿的中繼資料
- 載入現有的電子表單或建立一個全新的
- 使用`Metadata`屬性存取和修改中繼資料資訊
- 查看和修改電子表單資料
- 匯出帶有經過編輯的中繼資料屬性的電子表單
如何編輯工作簿的中繼資料屬性?
要編輯工作表文件的作者名稱,請使用所需的資料字串設置Author屬性。 例如,workBook.Metadata.Author = "Your Name"。 可以獲取和檢索WorkBook屬性中可用的中繼資料資訊。 這種方法能無縫地適用於各種電子表單文件型別,包括CSV格式。
哪個屬性可以編程修改?
:path=/static-assets/excel/content-code-examples/how-to/edit-workbook-metadata.cs
using IronXL;
using System;
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Set author
workBook.Metadata.Author = "Your Name";
// Set comments
workBook.Metadata.Comments = "Monthly report";
// Set title
workBook.Metadata.Title = "July";
// Set keywords
workBook.Metadata.Keywords = "Report";
// Read the creation date of the excel file
DateTime? creationDate = workBook.Metadata.Created;
// Read the last printed date of the excel file
DateTime? printDate = workBook.Metadata.LastPrinted;
workBook.SaveAs("editedMetadata.xlsx");
Imports IronXL
Imports System
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
' Set author
workBook.Metadata.Author = "Your Name"
' Set comments
workBook.Metadata.Comments = "Monthly report"
' Set title
workBook.Metadata.Title = "July"
' Set keywords
workBook.Metadata.Keywords = "Report"
' Read the creation date of the excel file
Dim creationDate? As DateTime = workBook.Metadata.Created
' Read the last printed date of the excel file
Dim printDate? As DateTime = workBook.Metadata.LastPrinted
workBook.SaveAs("editedMetadata.xlsx")
對於更複雜的場景,您可以將中繼資料編輯與其他Excel操作結合起來。 這裡有一個全面的例子,展示了多個Excel文件的批量處理:
using IronXL;
using System;
using System.IO;
public class BatchMetadataProcessor
{
public static void ProcessFinancialReports(string folderPath)
{
// Get all Excel files in the directory
string[] excelFiles = Directory.GetFiles(folderPath, "*.xlsx");
foreach (string filePath in excelFiles)
{
// Load the workbook
WorkBook workBook = WorkBook.Load(filePath);
// Update metadata based on file content
string fileName = Path.GetFileNameWithoutExtension(filePath);
// Set consistent metadata across all reports
workBook.Metadata.Author = "Finance Department";
workBook.Metadata.Company = "Your Company Name";
workBook.Metadata.Category = "Financial Reports";
// Set dynamic metadata based on filename
if (fileName.Contains("Q1"))
{
workBook.Metadata.Title = "Q1 Financial Report";
workBook.Metadata.Keywords = "Q1, Finance, Quarterly";
}
else if (fileName.Contains("Q2"))
{
workBook.Metadata.Title = "Q2 Financial Report";
workBook.Metadata.Keywords = "Q2, Finance, Quarterly";
}
// Add timestamp to comments
workBook.Metadata.Comments = $"Processed on {DateTime.Now:yyyy-MM-dd HH:mm}";
// Set the subject based on worksheet content
WorkSheet sheet = workBook.DefaultWorkSheet;
workBook.Metadata.Subject = $"Report containing {sheet.RowCount} data rows";
// Save with updated metadata
string outputPath = Path.Combine(folderPath, "processed", fileName + "_updated.xlsx");
workBook.SaveAs(outputPath);
}
}
}
using IronXL;
using System;
using System.IO;
public class BatchMetadataProcessor
{
public static void ProcessFinancialReports(string folderPath)
{
// Get all Excel files in the directory
string[] excelFiles = Directory.GetFiles(folderPath, "*.xlsx");
foreach (string filePath in excelFiles)
{
// Load the workbook
WorkBook workBook = WorkBook.Load(filePath);
// Update metadata based on file content
string fileName = Path.GetFileNameWithoutExtension(filePath);
// Set consistent metadata across all reports
workBook.Metadata.Author = "Finance Department";
workBook.Metadata.Company = "Your Company Name";
workBook.Metadata.Category = "Financial Reports";
// Set dynamic metadata based on filename
if (fileName.Contains("Q1"))
{
workBook.Metadata.Title = "Q1 Financial Report";
workBook.Metadata.Keywords = "Q1, Finance, Quarterly";
}
else if (fileName.Contains("Q2"))
{
workBook.Metadata.Title = "Q2 Financial Report";
workBook.Metadata.Keywords = "Q2, Finance, Quarterly";
}
// Add timestamp to comments
workBook.Metadata.Comments = $"Processed on {DateTime.Now:yyyy-MM-dd HH:mm}";
// Set the subject based on worksheet content
WorkSheet sheet = workBook.DefaultWorkSheet;
workBook.Metadata.Subject = $"Report containing {sheet.RowCount} data rows";
// Save with updated metadata
string outputPath = Path.Combine(folderPath, "processed", fileName + "_updated.xlsx");
workBook.SaveAs(outputPath);
}
}
}
Imports IronXL
Imports System
Imports System.IO
Public Class BatchMetadataProcessor
Public Shared Sub ProcessFinancialReports(folderPath As String)
' Get all Excel files in the directory
Dim excelFiles As String() = Directory.GetFiles(folderPath, "*.xlsx")
For Each filePath As String In excelFiles
' Load the workbook
Dim workBook As WorkBook = WorkBook.Load(filePath)
' Update metadata based on file content
Dim fileName As String = Path.GetFileNameWithoutExtension(filePath)
' Set consistent metadata across all reports
workBook.Metadata.Author = "Finance Department"
workBook.Metadata.Company = "Your Company Name"
workBook.Metadata.Category = "Financial Reports"
' Set dynamic metadata based on filename
If fileName.Contains("Q1") Then
workBook.Metadata.Title = "Q1 Financial Report"
workBook.Metadata.Keywords = "Q1, Finance, Quarterly"
ElseIf fileName.Contains("Q2") Then
workBook.Metadata.Title = "Q2 Financial Report"
workBook.Metadata.Keywords = "Q2, Finance, Quarterly"
End If
' Add timestamp to comments
workBook.Metadata.Comments = $"Processed on {DateTime.Now:yyyy-MM-dd HH:mm}"
' Set the subject based on worksheet content
Dim sheet As WorkSheet = workBook.DefaultWorkSheet
workBook.Metadata.Subject = $"Report containing {sheet.RowCount} data rows"
' Save with updated metadata
Dim outputPath As String = Path.Combine(folderPath, "processed", fileName & "_updated.xlsx")
workBook.SaveAs(outputPath)
Next
End Sub
End Class
當我儲存時現有的中繼資料會如何處理?
當您使用IronXL儲存或匯出Excel文件時,任何您未明確修改的中繼資料屬性都會保留其原始值。 只有您修改過的屬性會在儲存的文件中更新。這種選擇性的更新方法確保處理過程中不會意外地丟失現有的有價值的中繼資料。 下圖顯示了使用IronXL編輯後Excel的文件屬性面板中中繼資料的展示方式:
如果您需要在設定新值之前清除現有的中繼資料,只需為要重設的屬性分配空字串或null值即可。 這在準備外部分發的文件時特別有用,因為您可能需要移除內部公司的資訊。
IronXL中有哪些中繼資料欄位可用?
並不是所有的中繼資料屬性都可以編輯。 有些屬性只能檢索。 了解支援不同操作的屬性對於有效的中繼資料管理至關重要。 在處理受密碼保護的工作簿時,中繼資料仍然可以在成功解密後被存取和修改。
哪些屬性支援讀寫操作?
| 屬性 | 描述 | 操作 | 常見使用案例 |
|---|---|---|---|
Author |
文件建立者名稱 | 設定、修改、檢索 | 追踪文件的所有權、合規性 |
Comments |
文件的附加備註 | 設定、修改、檢索 | 版本備註、處理指導 |
LastPrinted |
最後一次列印操作的日期/時間 | 設定、修改、檢索 | 列印歷史追踪、審計跟蹤 |
Keywords |
可搜尋的關鍵字 | 設定、修改、檢索 | 文件分類、搜尋優化 |
Category |
文件類別分類 | 設定、修改、檢索 | 文件組織、部門排序 |
Created |
文件建立日期 | 設定、修改、檢索 | 文件年齡追踪、歸檔決策 |
ModifiedDate |
最後修改日期 | 設定、修改、檢索 | 變更追踪、版本控制 |
Subject |
文件主題描述 | 設定、修改、檢索 | 內容概述、快速識別 |
Title |
文件標題 | 設定、修改、檢索 | 文件識別、報告 |
哪些屬性是唯讀的?
| 屬性 | 描述 | 典型值 |
|---|---|---|
ApplicationName |
建立文件的應用程式名稱 | "Microsoft Excel","IronXL" |
CustomProperties |
使用者定義的自定義屬性 | 隨著文件不同而有所不同 |
Company |
文件關聯的公司名稱 | 來自系統的組織名稱 |
Manager |
文件屬性中的管理者名稱 | 從原始文件中檢索 |
Template |
建立文件使用的範本 | 範本檔名或"Normal" |
有關進階的中繼資料操作和完整的API文件,請參閱IronXL API Reference。 如果您在處理中繼資料時遇到任何問題,請查看我們的故障排除指南或探索生產部署的授權選項。
常見問題
如何在C#中程式化編輯Excel中繼資料?
IronXL在WorkBook類中提供了一個簡單的Metadata屬性,允許您程式化地編輯Excel中繼資料。您可以輕鬆地設置如標題、作者、主題和關鍵字等屬性,無需Microsoft Interop。只需載入您的工作簿並存取workBook.Metadata即可修改任何中繼資料屬性。
我可以在Excel文件中修改哪些中繼資料屬性?
使用IronXL,您可以修改各種中繼資料屬性,包括作者、標題、主題、關鍵字、類別、評論、狀態、管理員和公司。該程式庫還提供建立和修改日期的唯讀存取,允許全面的中繼資料管理。
我需要安裝Microsoft Office來編輯Excel中繼資料嗎?
不需要,IronXL無需安裝Microsoft Office或Interop。它是一個獨立的C#程式庫,能夠獨立讀取、寫入和修改Excel文件及其中繼資料,特別適合於沒有Office安裝的伺服器環境或系統。
我可以批量處理多個Excel文件的中繼資料嗎?
可以,IronXL支持批量處理Excel文件。您可以遍歷目錄中的多個電子表格,使用WorkBook.Load()載入每一個,修改其中繼資料屬性並儲存。這對於組織大量電子表格文件特別有用。
哪些Excel文件格式支持中繼資料編輯?
IronXL的中繼資料編輯功能與多種電子表格文件格式完全相容,包括XLSX、XLS和CSV文件。該程式庫在內部處理格式特定的細節,允許您使用相同的Metadata屬性介面,無論文件型別如何。
編輯完中繼資料後,如何儲存變更?
使用IronXL修改中繼資料屬性後,簡單地調用Save()方法來更新現有文件,或調用SaveAs()來建立包含更新過的中繼資料的新文件。程式庫會自動持久化所有中繼資料變更以及任何電子表格資料修改。

