Convertir CSV en JSON
Contents
[
Hide
]
Convertir CSV en JSON
Aspose.Cells prend en charge la conversion de CSV en JSON. Pour cela, le API fournit**ExportRangeToJsonOptions**et**JsonUtility** Des classes. Le**ExportRangeToJsonOptions**fournit les options d’exportation de la plage vers JSON. La**ExportRangeToJsonOptions**classe a les propriétés suivantes.
- ExportAsString: cela exporte la valeur de chaîne des cellules vers JSON.
- HasHeaderRow: Cela indique si la plage contient une ligne d’en-tête.
- Retrait: Indique le retrait.
Le**JsonUtility**classe exporte le JSON en utilisant les options d’exportation définies avec le**ExportRangeToJsonOptions**classe.
L’exemple de code suivant illustre l’utilisation de**ExportRangeToJsonOptions**et**JsonUtility** classes pour chargerfichier source CSVet imprime la sortie JSON dans la console.
Exemple de code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.Csv); | |
// Load CSV file | |
Workbook workbook = new Workbook(sourceDir + "SampleCsv.csv", loadOptions); | |
Cell lastCell = workbook.Worksheets[0].Cells.LastCell; | |
// Set JsonSaveOptions | |
JsonSaveOptions jsonSaveOptions = new JsonSaveOptions(); | |
Range range = workbook.Worksheets[0].Cells.CreateRange(0, 0, lastCell.Row + 1, lastCell.Column + 1); | |
string data = JsonUtility.ExportRangeToJson(range, jsonSaveOptions); | |
// Print JSON | |
Console.WriteLine(data); |
Sortie console
[
{
"id": 1,
"language": "Java",
"edition": "third",
"author": "Herbert Schildt",
"streetAddress": 126,
"city": "San Jone",
"state": "CA",
"postalCode": 394221
},
{
"id": 2,
"language": "C++",
"edition": "second",
"author": "EAAAA",
"streetAddress": 126,
"city": "San Jone",
"state": "CA",
"postalCode": 394221
},
{
"id": 3,
"language": ".Net",
"edition": "second",
"author": "E.Balagurusamy",
"streetAddress": 126,
"city": "San Jone",
"state": "CA",
"postalCode": 394221
}
]