通过 C++ 管理 Microsoft® Excel 文档元数据
在 C++ 应用程序中查看、插入、更新、删除或提取自定义和内置 Excel 文档属性。
Excel 中的元数据 - 如何查看、插入和删除 Excel 文件元数据。 C++ Excel 库 通过支持内置/系统定义的属性(例如作者姓名、标题、文档统计信息等),有时需要检查最后文件何时被修改或保存以及自定义/用户定义的属性,以简单的方式提供便利名称/值对。为了自动化该过程,库支持创建和维护大型元数据 Excel 文件。 练习册 class 按路径、流和特殊 FileFormatType 打开工作簿。因此,请使用适当的方法加载文件以进行进一步处理。下面列出的可能性很少,开发人员可以根据应用程序需求轻松增强他们的代码。
读取和更新内置属性
为了自动化内置属性,API 提供了 GetBuiltInDocumentProperties() 返回 DocumentProperties 集合的方法,该集合表示电子表格的所有内置文档属性。访问所有内置属性后,使用相关方法如 GetTitle()、GetSubject() 等访问相关属性。为了更新属性,API 提供了 SetTitle、SetSubject、SetAuthor、SetComments 等方法。查看 内置文档属性集合 用于所需的功能。
C++ 读取系统定义属性的代码
Aspose::Cells::Startup(); | |
//Source directory path | |
U16String dirPath = u"..\\Data\\LoadingSavingForMetadata\\"; | |
//Paths of source and output excel files | |
U16String samplePath = dirPath + u"sample-metadata-properties.xlsx"; | |
//Load the sample excel file | |
Workbook wb(samplePath); | |
//Read built-in title and subject properties | |
U16String strTitle = wb.GetBuiltInDocumentProperties().GetTitle(); | |
U16String strSubject = wb.GetBuiltInDocumentProperties().GetSubject(); | |
std::cout << "Title: " << strTitle.ToUtf8() << std::endl; | |
std::cout << "Subject: " << strSubject.ToUtf8() << std::endl; | |
Aspose::Cells::Cleanup(); |
C++ 更新内置属性的代码
Aspose::Cells::Startup(); | |
//Source directory path | |
U16String dirPath = u"..\\Data\\LoadingSavingForMetadata\\"; | |
//Output directory path | |
U16String outPath = u"..\\Data\\Output\\"; | |
//Paths of source and output excel files | |
U16String samplePath = dirPath + u"sample-metadata-properties.xlsx"; | |
U16String outputPath = outPath + u"output-metadata-properties.xlsx"; | |
//Load the sample excel file | |
Workbook wb(samplePath); | |
//Modify built-in title and subject properties | |
U16String strTitle = u"Aspose.Cells New Title"; | |
U16String strSubject = u"Aspose.Cells New Subject"; | |
wb.GetBuiltInDocumentProperties().SetTitle(strTitle); | |
wb.GetBuiltInDocumentProperties().SetSubject(strSubject); | |
//Save the output excel file | |
wb.Save(outputPath); | |
Aspose::Cells::Cleanup(); |
查看和添加自定义属性
为了处理自定义属性,API 提供 工作簿::获取自定义文档属性 返回电子表格的所有自定义文档属性集合。首先通过此方法访问自定义属性,开发人员可以使用相关方法添加属性,如 AddIDocumentProperty、AddLinkToContentProperty 以及类似地使用 UpdateLinkedPropertyValue、UpdateLinkedRange 来更新分别链接到内容和链接范围的自定义文档属性值。开发者可以使用相关方法 自定义文档属性的集合 .
C++ 查看自定义属性的代码
Aspose::Cells::Startup(); | |
//Source directory path | |
U16String dirPath = u"..\\Data\\LoadingSavingAndConverting\\"; | |
//Paths of source and output excel files | |
U16String samplePath = dirPath + u"sample-metadata-properties.xlsx"; | |
//Load the sample excel file | |
Workbook wb(samplePath); | |
//Read the custom property | |
U16String strCustomPropName = u"MyCustom1"; | |
U16String strCustomPropValue = wb.GetCustomDocumentProperties().Get(strCustomPropName).ToString(); | |
U16String myCustom1 = u"\r\nMyCustom1: "; | |
std::cout << myCustom1.ToUtf8() << strCustomPropValue.ToUtf8() << std::endl; | |
Aspose::Cells::Cleanup(); |
C++ 在 Excel 文件中添加元数据的代码
Aspose::Cells::Startup(); | |
//Source directory path | |
U16String dirPath =u"..\\Data\\LoadingSavingAndConverting\\"; | |
//Output directory path | |
U16String outPath = u"..\\Data\\Output\\"; | |
//Paths of source and output excel files | |
U16String samplePath = dirPath + u"sample-metadata-properties.xlsx"; | |
U16String outputPath = outPath + u"output-metadata-properties.xlsx"; | |
//method 1 | |
//Load the sample excel file | |
Workbook wb(samplePath); | |
//Add a new custom property | |
U16String strCustomPropName = u"MyCustom5"; | |
U16String strCustomPropValue = u"This is my custom five."; | |
wb.GetCustomDocumentProperties().Add(strCustomPropName, strCustomPropValue); | |
//Save the output excel file | |
wb.Save(outputPath); | |
////method 2 | |
////Load the sample excel file | |
//Metadata::MetadataOptions options(Metadata::MetadataType::Document_Properties); | |
//Metadata::WorkbookMetadata meta(samplePath, options); | |
////Add a new custom property | |
//meta.GetCustomDocumentProperties().Add(strCustomPropName, strCustomPropValue); | |
////Save the output excel file | |
//meta.Save(outputPath); | |
Aspose::Cells::Cleanup(); |