Lecture d un fichier CSV avec plusieurs encodages avec C++
Aspose.Cells fournit la propriété TxtLoadOptions.IsMultiEncoded, que vous devez définir à true pour charger correctement votre fichier CSV avec plusieurs encodages.
La capture d’écran suivante montre un fichier CSV d’exemple contenant deux lignes. La première ligne est en encodage ANSI et la seconde en Unicode.
Fichier d’entrée |
---|
![]() |
La capture d’écran suivante montre le fichier XLSX converti à partir du CSV ci-dessus sans définir la propriété TxtLoadOptions.IsMultiEncoded à true. Comme vous pouvez le voir, le texte Unicode n’a pas été converti correctement.
Fichier de sortie 1: aucune adaptation pour plusieurs encodages |
---|
![]() |
La capture d’écran suivante montre le fichier XLSX converti à partir du CSV ci-dessus après avoir réglé la propriété TxtLoadOptions.IsMultiEncoded à true. Comme vous pouvez le voir, le texte Unicode est désormais converti correctement.
Fichier de sortie 2: IsMultiEncoded est défini sur true |
---|
![]() |
Ci-dessous se trouve le code d’exemple qui convertit le fichier CSV ci-dessus en format XLSX correctement.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input CSV file
U16String filePath = srcDir + u"MultiEncoded.csv";
// Create TxtLoadOptions and set MultiEncoded property to true
TxtLoadOptions options;
options.SetIsMultiEncoded(true);
// Load the CSV file into Workbook with the specified options
Workbook workbook(filePath, options);
// Save the workbook in XLSX format
workbook.Save(filePath + u".out.xlsx", SaveFormat::Xlsx);
std::cout << "File converted successfully!" << std::endl;
Aspose::Cells::Cleanup();
}