Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created January 18, 2021 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/8ee937b58ee2881ce8ec9dd1cb986d02 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/8ee937b58ee2881ce8ec9dd1cb986d02 to your computer and use it in GitHub Desktop.
Convert JSON to CSV or CSV to JSON Programmatically in C# VB.NET
// Load CSV file
Aspose.Cells.LoadOptions loadOptions = new Aspose.Cells.LoadOptions(Aspose.Cells.LoadFormat.CSV);
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(dataDir + "Sample_out.csv", loadOptions);
Aspose.Cells.Cell lastCell = workbook.Worksheets[0].Cells.LastCell;
// Set ExportRangeToJsonOptions
Aspose.Cells.Utility.ExportRangeToJsonOptions options = new Aspose.Cells.Utility.ExportRangeToJsonOptions();
Aspose.Cells.Range range = workbook.Worksheets[0].Cells.CreateRange(0, 0, lastCell.Row + 1, lastCell.Column + 1);
string data = Aspose.Cells.Utility.JsonUtility.ExportRangeToJson(range, options);
// Write from CSV to a JSON file
System.IO.File.WriteAllText(dataDir + "CSV_out.json", data);
// Read JSON file
string str = File.ReadAllText(dataDir + "SampleJson.json");
// Create empty workbook
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
// Get Cells
Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;
// Set JsonLayoutOptions
Aspose.Cells.Utility.JsonLayoutOptions importOptions = new Aspose.Cells.Utility.JsonLayoutOptions();
importOptions.ConvertNumericOrDate = true;
importOptions.ArrayAsTable = true;
importOptions.IgnoreArrayTitle = true;
importOptions.IgnoreObjectTitle = true;
Aspose.Cells.Utility.JsonUtility.ImportData(str, cells, 0, 0, importOptions);
// Save Workbook
workbook.Save(dataDir + @"Sample_out.csv");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment