How to Convert JSON to PDF, CSV, HTML, Image Using .NET C#
JSON is one of the essential technologies in this era of modern software programming. First introduced in the 2000s, it has gradually expanded its market to become the most common medium for exchanging and transmitting data across web applications (e.g., sending some data from the server to the client).
The exchanged data can be used to meet several programming needs, including displaying information on a web page, storing data in a local or cloud location, or printing. However, this process requires writing explicit codes, first to convert the JSON data from its native form to a structured layout and then to export it to a format suitable for the described data need.
GrapeCity Documents for Excel (.NET Edition | Java Edition) offers simple and easy-to-use APIs to meet these programming needs. To see how to convert native JSON to a structured layout, you can follow this article. Here, we will see how to export it to popular document formats - PDF, HTML, CSV, and Image.
Download GrapeCity Documents for Excel .NET or Java!
Convert JSON to PDF (Portable Document Format)
PDF is a popular format when you want to save files that cannot be modified and are easily shared and printed. This format is suitable for structured documents such as invoices, Medical Test reports, etc.
With GrapeCity Documents for Excel, you can use the Excel template to create a structured document and fetch the JSON data using the JsonDataSource class and IWorkbook.AddDataSource method or IWorksheet.DataSource property. Once the data is added to the document, use IWorkbook.Save method and SaveFileFormat.PDF enum to export it to PDF as shown in the code below:
.NET
var datasource = new JsonDataSource(jsonText);
workbook.AddDataSource("ds", datasource);
. . .
workbook.Save("Invoice.pdf");
OR
workbook.Save("Invoice.pdf", SaveFileFormat.Pdf);
JAVA
worksheet.setDataSource(new JsonDataSource(jsonText));
. . .
workbook.save("Invoice.pdf");
OR
workbook.save("Invoice.pdf", SaveFileFormat.Pdf);
GcExcel offers several properties with the PdfSaveOptions class that you can use to manage how the PDF is generated.
.NET
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions
{
//Set pdf options
DocumentProperties = documentProperties
SecurityOptions = securityOptions
. . .
};
workbook.Save("Invoice.pdf", pdfSaveOptions);
JAVA
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setSecurityOptions(securityOptions);
pdfSaveOptions.setDocumentProperties(documentProperties);
. . .
workbook.save("Invoice.pdf", pdfSaveOptions);
Have a look at the .NET | Java demos.
For more details, follow the .NET | Java documentation.
Convert JSON to HTML
Companies often want to publish information on websites for customers, employees, and other people involved in the business. For example, an automobile website publishing the car price list, a finance company posting financial documents, and many more such scenarios.
In this situation, you may want to convert the JSON data to HTML format to embed it in your website page easily.
With GcExcel, use the JsonDatasource object in GcExcel APIs to get the data in a Table, Worksheet, or Template file using IWorkbook.AddDataSource method or IWorksheet.DataSource property. Then use the IWorkbook.Save method and SaveFileFormat.HTML enum to convert it to HTML, as shown in the sample snippet below:
.NET
var datasource = new JsonDataSource(jsonText);
workbook.AddDataSource("ds", datasource);
OR
workbook.ActiveSheet.datasource = datasource;
. . .
workbook.Save("PriceList.html");
OR
workbook.Save("PriceList.html", SaveFileFormat.Html);
JAVA
worksheet.setDataSource(new JsonDataSource(jsonText));
OR
workbook.getActiveSheet.datasource = new JsonDataSource(jsonText);
. . .
workbook.save("PriceList.html");
OR
workbook.save("PriceList.html", SaveFileFormat.Html);
GcExcel offers several properties in HtmlSaveOptions class that you can use to control the exported content in various ways, like exporting headings, gridlines, and document properties or applying other settings like scalable width, page title, displaying tooltip text, etc.
.NET
HtmlSaveOptions options = new HtmlSaveOptions();
options.ExportSheetName = workbook.Worksheets[0].Name;
options.ExportFileName = "PriceList";
workbook.Save("PriceList.html", options);
JAVA
HtmlSaveOptions options = new HtmlSaveOptions();
options.setExportSheetName(workbook.getWorksheets().get(0).getName());
options.setExportArea("D2:G23");
options.setExportFileName("PriceList");
workbook.save("PriceList.html", options);
Explore the .NET | Java demos.
For more details, follow the .NET | Java documentation.
Convert JSON to CSV
CSV file format is popular with developers because it is easier to import into a spreadsheet or another storage database and is supported by most data upload interfaces. Though JSON is taking over the market for data exchange scenarios, CSV still finds its use when data is required in the form of a matrix where each row represents a record.
GcExcel offers to convert the JSON data to CSV using two simple steps - load the JSON data in a worksheet using the JsonDataSource object and IWorkbook.AddDataSource method or IWorksheet.DataSource property as shown in the code snippet below:
.NET
var datasource = new JsonDataSource(jsonText);
workbook.AddDataSource("ds", datasource);
OR
workbook.ActiveSheet.datasource = datasource;
Java
JsonDataSource datasource = new JsonDataSource(jsonText);
workbook.addDataSource("ds", datasource);
OR
workbook.getActiveSheet.datasource = datasource;
Then exporting the worksheet to CSV using the IWorkbook.Save method, SaveFileFormat.CSV enum, and CsvSaveOptions class as shown below:
.NET
CsvSaveOptions options = new CsvSaveOptions
{
ColumnSeparator = "-",
RowSeparator = "\r\n",
CellSeparator = '"'
};
workbook.Save("Data.csv", options);
Java
CsvOpenOptions options = new CsvOpenOptions();
options.setColumnSeparator(",");
options.setRowSeparator("\r\n");
options.setCellSeparator('"');
workbook.save("Data.csv", options);
Check out the .NET | Java demos.
For more details about import and export to CSV, follow the .NET | Java documentation.
Convert JSON to Image
Images are often required to present a defined snapshot of data in documents such as Analysis reports, PPT presentations, etc.
With JSON data, there are two common situations where image conversion is required. One where JSON contains base64 image strings must be converted to Image types such as .jpg, .png, and so on. Other, where the raw JSON data is to be visualized using a table, graph, or chart and then captured as an image snapshot.
Case 1 - Convert base64 image string from JSON to Image
To meet this need, use the JsonDataSource class with IWorkbook.AddDataSource method or IWorksheet.DataSource property to bind the data and display it in a sheet or table. Then convert the base64 image strings to the image stream and then use this stream to add a picture to a worksheet in .NET and Java as shown below:
.NET
//base64 to image stream conversion
byte[] imageBytes = Convert.FromBase64String(worksheet.Range["A1"].Value.ToString());
MemoryStream stream = new MemoryStream();
stream.Write(imageBytes, 0, imageBytes.Length);
//convert image stream to picture object
worksheet.Shapes.AddPicture(stream, ImageType.JPG, worksheet.Range["B1"]);
Java
BufferedImage image = null;
byte[] imageByte;
BASE64Decoder decoder = new BASE64Decoder();
imageByte = decoder.decodeBuffer(imageString);
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
bis.close();
worksheet.getShapes.addPicture(image,ImageType.JPG, worksheet.getRange("B1"));
Case 2 - Convert JSON data to a visual for an image snapshot
Similar to the case above, to meet this need, use the JsonDataSource class with IWorkbook.AddDataSource method or IWorksheet.DataSource property to bind the data and display it in a Chart, Range, or Table. Finally, convert them using the ToImage method with worksheet, shape, or range as shown below:
.NET
var datasource = new JsonDataSource(jsonText);
worksheet.DataSource = datasource;
worksheet.AutoGenerateColumns = true;
. . .
IShape chartObj = worksheet.Shapes.AddChart(ChartType.Bubble, 250, 20, 360, 230);
chartObj.Chart.SeriesCollection.Add(worksheet.Range["A1:C5"], GrapeCity.Documents.Excel.Drawing.RowCol.Columns);
. . .
chartObj.ToImage("JsonToImage.png", ImageType.PNG);
Java
worksheet.setDataSource(new JsonDataSource(jsonText));
worksheet.setAutoGenerateColumns(true);
. . .
IShape chartObj = worksheet.getShapes().addChart(ChartType.Line, 250, 20, 400, 250);
chartObj.getChart().getSeriesCollection().add(worksheet.getRange("A1:C5"));
. . .
chartObj.toImage("JsonToImage.png");
Have a look at the .NET | Java demos.
Learn exporting to image from .NET | Java documentation.
Download GrapeCity Documents for Excel .NET or Java!