在此示例中,我们将重点介绍如何在 C++ 中从 Excel 文件中删除公式。通过使用 C++,您可以以编程方式从 Excel 工作表的任何单元格中重置公式。通过使用少量 API 调用,您可以在 C++ 中为 Microsoft Windows 和 Linux 平台管理 Excel 工作表单元格公式。
在 C++ 中从 Excel 文件中删除公式的步骤
- 使用 NuGet 包管理器添加 Aspose.Cells.Cpp 包
- 添加对 Aspose::Cells 命名空间的引用
- 初始化 Workbook Class 对象以加载 Excel 文件以删除公式
- 访问工作表内的单元格 C1
- 重置选定单元格的公式值
- 在 C++ 中删除公式后保存输出的 excel 文件
在下面的示例中,您将学习如何访问工作簿和 通过 C++ 从 Excel 文件中删除公式*,只需使用一些 API 调用。使用 C++,您可以通过编程方式操作 Excel 单元格,例如通过索引或工作表名称访问任何工作表,然后删除单元格中的任何公式,而不会影响工作簿的其他内容。
在 C++ 中从 Excel 工作表单元格中删除公式的代码
#pragma once | |
#include "Aspose.Cells.h" | |
class RemoveFormula | |
{ | |
public: | |
static void RemoveFormulaFromCell() | |
{ | |
// Set the license for Aspose.Cells API | |
intrusive_ptr<License> CellRemoveFormulaLicense = new License(); | |
CellRemoveFormulaLicense->SetLicense(new String("Aspose.Total.lic")); | |
// Initialize the Workbook class object to load Excel file for resetting formula | |
intrusive_ptr<IWorkbook> WbWithFormula = Factory::CreateIWorkbook(new String("ExcelFileWithFormula.xlsx")); | |
// Access the cell C1 from a first worksheet | |
intrusive_ptr <ICell> cellWithFormula = WbWithFormula->GetIWorksheets()->GetObjectByIndex(0)-> | |
GetICells()->GetObjectByIndex(new String("C1")); | |
intrusive_ptr<Object> TempData = cellWithFormula->GetValue(); | |
// Reset formula value for the selected cell | |
cellWithFormula->SetFormula(new String("")); | |
cellWithFormula->SetValue(TempData); | |
// Save the output Excel file without formula | |
WbWithFormula->Save(new String ("ExcelWithoutFormula.xlsx")); | |
} | |
}; |
您可以从 Excel 文件中删除公式,而无需更改文件的其他内容。您无需安装或使用 Microsoft Excel 即可实现此要求。在前面的示例中,我们探索了 如何在 C++ 中编辑 Excel 文件,其中包含在 Excel 文件中设置公式的示例。