Quản lý Microsoft® Siêu dữ liệu tài liệu Excel qua C++

Xem, chèn, cập nhật, xóa hoặc trích xuất các thuộc tính tài liệu Excel tùy chỉnh và tích hợp trong ứng dụng C++.

 

Metadata trong Excel - Cách xem, chèn và xóa siêu dữ liệu file excel. C++ Thư viện Excel tạo điều kiện một cách dễ dàng bằng cách hỗ trợ các thuộc tính tích hợp/do hệ thống xác định như tên tác giả, tiêu đề, số liệu thống kê tài liệu, v.v. đôi khi muốn kiểm tra khi tệp cuối cùng được sửa đổi hoặc lưu cùng với các thuộc tính tùy chỉnh/do người dùng xác định ở dạng cặp tên/giá trị. Để tự động hóa quy trình, thư viện hỗ trợ tạo và duy trì các tệp Excel siêu dữ liệu lớn. Sách bài tập class Mở một sổ làm việc theo đường dẫn, theo luồng và theo FileFormatType đặc biệt. Vì vậy hãy tải tệp bằng phương pháp thích hợp để xử lý tiếp. Một số khả năng được liệt kê bên dưới và các nhà phát triển có thể dễ dàng nâng cao mã của họ theo yêu cầu của ứng dụng.

Đọc và cập nhật thuộc tính dựng sẵn

Để tự động hóa các thuộc tính tích hợp, API cung cấp GetBuiltInDocumentProperties() phương thức trả về bộ sưu tập DocumentProperties đại diện cho tất cả các thuộc tính tài liệu tích hợp của bảng tính. Sau khi truy cập tất cả các thuộc tính dựng sẵn, hãy truy cập các thuộc tính có liên quan bằng phương thức liên quan như GetTitle(), GetSubject(), v.v. Để cập nhật các thuộc tính, API cung cấp phương thức như SetTitle, SetSubject, SetAuthor, SetComments, v.v. Xem bộ sưu tập thuộc tính tài liệu dựng sẵn cho chức năng cần thiết.

C++ Mã để đọc thuộc tính do hệ thống xác định
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++ Mã để cập nhật thuộc tính dựng sẵn
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();
 

Xem và thêm thuộc tính được xác định tùy chỉnh

Để xử lý các thuộc tính tùy chỉnh, API cung cấp Sổ làm việc::GetCustomDocumentProperties trả về tất cả bộ sưu tập thuộc tính tài liệu tùy chỉnh của bảng tính. Trước tiên, truy cập các thuộc tính tùy chỉnh thông qua phương pháp này, nhà phát triển có thể sử dụng các phương pháp có liên quan để thêm các thuộc tính như AddIDocumentProperty, AddLinkToContentProperty và sử dụng tương tự UpdateLinkedPropertyValue, UpdateLinkedRange để cập nhật giá trị thuộc tính tài liệu tùy chỉnh liên kết đến nội dung và phạm vi được liên kết tương ứng. Các nhà phát triển có thể sử dụng phương pháp liên quan từ bộ sưu tập các thuộc tính tài liệu tùy chỉnh .

C++ Mã để xem thuộc tính tùy chỉnh
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++ Mã để thêm siêu dữ liệu vào tệp 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();