通过 C++ 更新 Excel 文档

在基于 C++ 的应用程序中修改 Microsoft Excel XLSX、XLS 文件,而无需安装 Microsoft Office®

 

组织通常会通过公司软件更新存储在 excel 文件中的数据,例如学生数据、患者记录和仓库物品清单等。 Aspose.Total for C++ API 提供使用软件修改电子表格的功能。 程序员只需编写几行 API 代码就可以通过修改功能增强软件。 作为 Aspose.Total for C++ 包的一部分的 Aspose.Cells for C++ API 使这个修改过程变得容易。 下面是更新Excel文件的过程。

使用 C++ 更新 Excel 文档

使用 Aspose.Cells for C++ API,加载使用 CreateIWorkbook 的源文件。 使用 GetIWorksheets()->GetObjectByIndex(0) 访问 Worksheet ,使用 GetICells()->GetObjectByIndex 访问所需的单元格。 使用PutValue方式,修改被访问单元格中的内容。 最后调用 save() 方法来保存文档。

C++ 代码 - 更新 Excel 文档

StringPtr dirPath = new String("..\\Data\\sourcePath\\");
StringPtr outPath = new String("..\\Data\\OutputPath\\");
StringPtr srcCSV = dirPath->StringAppend(new String(L"srcFile.csv"));
StringPtr updatedCSV = outPath->StringAppend(new String(L"outReadWriteCSV.csv"));
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(srcCSV);
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0);
intrusive_ptr<ICell> cell = ws->GetICells()->GetObjectByIndex(new String("A1"));
StringPtr strVal = cell->GetStringValue();
StringPtr cellValue = new String("Cell Value: ");
Console::WriteLine(cellValue->StringAppend(strVal));
cell = ws->GetICells()->GetObjectByIndex(new String("C4"));
intrusive_ptr<String> strValPtr = new String(strVal);
cell->PutValue(strValPtr);
wb->Save(updatedCSV, SaveFormat_CSV);