Lire et écrire le format de fichier CSV
Scénarios d’utilisation possibles
Microsoft Excel prend en charge de nombreux formats tels que XLS, XLSX, XLSM, XLSB, CSV, etc. Aspose.Cells prend également en charge bon nombre de ces formats. Cet article explique comment lire et écrire le fichier Excel au format CSV à l’aide d’Aspose.Cells.
Lire et écrire le format de fichier CSV
Le code d’exemple suivant charge le fichier CSV source et lit sa cellule A1, puis en copie le contenu dans la cellule C4 et le sauvegarde en tant que fichier CSV de sortie.
Code d’exemple
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Source directory path | |
U16String dirPath(u"..\\Data\\LoadingSavingAndConverting\\"); | |
//Output directory path | |
U16String outPath(u"..\\Data\\Output\\"); | |
//Path of input csv file | |
U16String srcReadWriteCSV = dirPath + u"srcReadWriteCSV.csv"; | |
//Path of output csv file | |
U16String outReadWriteCSV = outPath + u"outReadWriteCSV.csv"; | |
//Read source csv file | |
Workbook wb(srcReadWriteCSV); | |
//Access first worksheet | |
Worksheet ws = wb.GetWorksheets().Get(0); | |
//Access cell A1 | |
Cell cell = ws.GetCells().Get(u"A1"); | |
//Get the string value of cell A1 | |
U16String strVal = cell.GetStringValue(); | |
//Print the string value of cell A1 | |
U16String cellValue(u"Cell Value: "); | |
std::cout << cellValue.ToUtf8() << strVal.ToUtf8() << std::endl; | |
//Access cell C4 | |
cell = ws.GetCells().Get(u"C4"); | |
//Put the string value of cell A1 into C4 | |
U16String strValPtr(strVal); | |
cell.PutValue(strValPtr); | |
//Save the workbook in csv format | |
wb.Save(outReadWriteCSV, SaveFormat::CSV); | |
Aspose::Cells::Cleanup(); |