組織通常會通過公司軟件更新存儲在 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); |