
Microsoft Excel 提供了在 Excel 文件中添加註釋的選項。添加註釋可能有多種原因,例如解釋公式或添加可能對讀者有用的上下文信息。註釋也可用於建議對工作表進行編輯。此外,您還可以設置評論的字體大小、高度、寬度等。在本文中,您將了解如何使用 C++ 以編程方式向 Excel 工作表添加註釋。
用於在 Excel 工作表中添加註釋的 C++ API
Aspose.Cells for C++ 是一個本地 C++ 庫,允許您使用 Excel 文件。使用 API,您可以輕鬆創建、讀取和修改 Excel 文件。此外,您還可以在 Excel 工作表中添加註釋。要開始使用,請通過 NuGet 安裝 API 或直接從 下載 部分下載。
PM> Install-Package Aspose.Cells.Cpp
使用 C++ 在 Excel 工作表中添加註釋
以下是向 Excel 工作表中的單元格添加註釋的步驟。
- 使用 IWorkbook 類加載 Excel 文件。
- 檢索要在其中添加評論的工作表。
- 使用 IWorksheet->GetIComments()->Add(intrusiveptrAspose::Cells::Systems::String cellName) 方法。
- 使用 IWorksheet->GetIComments()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) 方法檢索在上一步中添加的註釋。
- 使用 IComment->SetNote(intrusiveptrAspose::Cells::Systems::String值) 方法。
- 使用 IWorkbook->Save(intrusiveptrAspose::Cells::Systems::String文件名) 方法。
以下示例代碼顯示瞭如何使用 C++ 向 Excel 工作表中的單元格添加註釋。
// 源目錄路徑。
StringPtr srcDir = new String("SourceDirectory\\Excel\\");
// 輸出目錄路徑。
StringPtr outDir = new String("OutputDirectory\\");
// 加載源 Excel 文件
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.xlsx")));
// 檢索第一個工作表
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
// 向單元格 F5 添加註釋
int commentIndex = worksheet->GetIComments()->Add(new String("F5"));
// 檢索添加到單元格 F5 的評論
intrusive_ptr<IComment> comment = worksheet->GetIComments()->GetObjectByIndex(commentIndex);
// 設置評論備註
comment->SetNote(new String("Hello Aspose!"));
// 保存 Excel 文件
workbook->Save(outDir->StringAppend(new String("AddComment_out.xlsx")));

圖像顯示添加到單元格 F5 的評論
使用 C++ 將格式應用於 Excel 工作表中的註釋
以下是使用 C++ 將格式應用於 Excel 工作表中的註釋的步驟。
- 使用 IWorkbook 類加載 Excel 文件。
- 檢索要在其中添加評論的工作表。
- 使用 IWorksheet->GetIComments()->Add(intrusiveptrAspose::Cells::Systems::String cellName) 方法。
- 使用 IWorksheet->GetIComments()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) 方法檢索在上一步中添加的評論。
- 使用 IComment->SetNote(intrusiveptrAspose::Cells::Systems::String值) 方法。
- 根據您的需要設置評論的格式。
- 使用 IWorkbook->Save(intrusiveptrAspose::Cells::Systems::String文件名) 方法。
以下示例代碼顯示如何使用 C++ 將格式應用於 Excel 工作表中的註釋。
// 源目錄路徑。
StringPtr srcDir = new String("SourceDirectory\\Excel\\");
// 輸出目錄路徑。
StringPtr outDir = new String("OutputDirectory\\");
// 加載源 Excel 文件
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.xlsx")));
// 檢索第一個工作表
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
// 向單元格 F5 添加註釋
int commentIndex = worksheet->GetIComments()->Add(new String("F5"));
// 檢索添加到單元格 F5 的評論
intrusive_ptr<IComment> comment = worksheet->GetIComments()->GetObjectByIndex(commentIndex);
// 設置評論備註
comment->SetNote(new String("Hello Aspose!"));
// 設置字體大小
comment->GetIFont()->SetSize(14);
// 設置字體粗細
comment->GetIFont()->SetBold(true);
// 以 CM 為單位設置高度
comment->SetHeightCM(10);
// 以 CM 為單位設置寬度
comment->SetWidthCM(2);
// 保存 Excel 文件
workbook->Save(outDir->StringAppend(new String("ApplyFormattingToComment_out.xlsx")));

圖像顯示添加到單元格 F5 的自定義格式的評論
獲得免費許可證
為了在沒有評估限制的情況下試用 API,您可以申請免費的臨時許可證。
結論
在本文中,您學習瞭如何使用 C++ 在 Excel 工作表中添加註釋。此外,您還了解瞭如何使用簡單易用的 Aspose.Cells for C++ API 將格式應用於註釋。該 API 提供了一系列用於處理 Excel 文件的附加功能,您可以通過訪問 官方文檔 詳細了解這些功能。如有任何疑問,請隨時通過我們的免費支持論壇與我們聯繫。