Skip to main content Skip to footer

How to Export Excel XLSX to PDF, Image, JSON, HTML in C# .NET

Quick Start Guide
What You Will Need

DsExcel API

Visual Studio 2022

Controls Referenced

Document Solutions for Excel, .NET Edition

Documentation

Tutorial Concept This tutorial demonstrates all the exporting capabilities of the DsExcel API including exporting Excel XLSX to formats such as PDF, Image, JSON, and HTML.

Exporting reports in different formats guarantees optimal accessibility and seamless integration.

In today's data-driven world, exporting reports in various formats is crucial for analyses and high-quality presentations. Having data available in multiple formats enhances user experience and allows for meaningful insights. With our Document Solutions for Excel (DsExcel) API, you can easily create advanced reports programmatically. DsExcel supports features like data analysis, charting, and exporting to various formats, making it a powerful tool for automating Excel-related tasks.

In this blog, we'll explore how to programmatically export Excel reports to multiple formats using the DsExcel API. Below is a list of the file formats supported by DsExcel API:

Ready to get started? Download Document Solutions for Excel, .NET Edition Today!

Export to Excel

Exporting reports in Excel allows for dynamic data manipulation and analysis, making it ideal for financial reports, data summaries, and interactive dashboards. Excel's features, like formulas and charts, help users make informed decisions by helping them better understand data.

To save the workbook in Excel file format using DsExcel, you can use the Save method with FileFormatEnum set to Xlsx, Xlsm, or Xltx.

Below is the DsExcel code to export the workbook as an Excel file:

// Create a new workbook
var workbook = new GrapeCity.Documents.Excel.Workbook();
// Load your data into the workbook or create your report
// Example: workbook.Open("path_to_your_template_or_data.xlsx");
// Save the workbook in the desired Excel file format
workbook.Save("report.xlsx", GrapeCity.Documents.Excel.SaveFileFormat.Xlsx);
// You can also save in other formats like Xlsm or Xltx
// Example:
// workbook.Save("report.xlsm", GrapeCity.Documents.Excel.SaveFileFormat.Xlsm);
// workbook.Save("report.xltx", GrapeCity.Documents.Excel.SaveFileFormat.Xltx);

Export to PDF

Exporting reports in PDF ensures that content looks the same on any device, making it perfect for sharing and printing. PDFs maintain their layout and design, providing a professional and consistent presentation.

To save the workbook as a PDF using DsExcel, use the Save method with a PdfSaveOptions object as a parameter. This allows you to define various settings such as image quality, border options, shrink settings, security options, and so on.

Below is the code to export a PDF file with multiple settings via DsExcel:

// Create a new workbook
var workbook = new Workbook();
// Load your data into the workbook or create your report
// Example: workbook.Open("path_to_your_template_or_data.xlsx");
//pdf settings
var pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.ImageQuality = 100;
pdfSaveOptions.ShrinkToFitSettings.CanShrinkToFitWrappedText = true;
pdfSaveOptions.PrintTransparentCell = true;
// Save the workbook as a PDF with the specified options
workbook.Save("report.pdf", pdfSaveOptions);

Export to Image

Exporting reports in image format makes sharing and viewing files simple without the need for special software. Images are perfect for quick visual presentations and can be easily added to emails, websites, and other documents.

You can save worksheets, cell ranges, and shapes as images using the ToImage method of DsExcel. To customize settings like background color, resolution, and gridline visibility, use the ImageSaveOptions object as a parameter.

Below is the DsExcel code to export the workbook as an image with different settings:

// Create a new workbook
var workbook = new Workbook();
// Load your data into the workbook or create your report
// Example: workbook.Open("path_to_your_template_or_data.xlsx");
// Define the worksheet and range to be exported
var worksheet = workbook.Worksheets[0];
var range = worksheet.Range["A1:D10"]; // Adjust the range as needed
// Create ImageSaveOptions with various settings
var imageSaveOptions = new ImageSaveOptions
{
    // Set background color (e.g., white)
    BackgroundColor = System.Drawing.Color.White,
    // Set resolution (DPI)
    Resolution = 300,
    // Show gridlines
    ShowGridlines = true,
    // Show Row Headings
    ShowRowHeadings = true
};
// Export the specified range to an image file with the defined options
range.ToImage("report.png", imageSaveOptions);

Export to CSV

Exporting reports in CSV format offers a simple and widely compatible way to share data, making it ideal for database imports and data analysis. CSV files can be easily opened in various applications, making them perfect for quick and efficient data exchange.

You can save a workbook as a CSV file using the Save method. By using the CsvSaveOptions parameter, you can customize settings like the separator string, encoding, and more.

Below is the DsExcel code to export a CSV file with advanced CSV settings:

// Create a new workbook
var workbook = new Workbook();
// Load your data into the workbook or create your report
// Example: workbook.Open("path_to_your_template_or_data.xlsx");
// Create CsvSaveOptions with advanced settings
var csvSaveOptions = new CsvSaveOptions
{
    SeparatorString = ";", // Use semicolon as separator
    Encoding = System.Text.Encoding.UTF8, // Set encoding to UTF-8
};
// Save the workbook as a CSV file with the specified options
workbook.Save("report.csv", csvSaveOptions);

Export to HTML

Exporting reports in HTML format allows for interactive and web-friendly presentations ideal for online dashboards and web-based reporting. HTML reports can include hyperlinks, images, and dynamic content, making them highly versatile and engaging.

You can save the workbook to an HTML file using the Save method. Using the HtmlSaveOptions parameter, you can apply various settings like sheet name, export area, export gridlines, and so on.

Below is the DsExcel code to export the HTML file with different settings:

// Create a new workbook
var workbook = new Workbook();
// Load your data into the workbook or create your report
// Example: workbook.Open("path_to_your_template_or_data.xlsx");
// Create HtmlSaveOptions with advanced settings
var htmlSaveOptions = new HtmlSaveOptions
{
    ExportSheetName = "ReportSheet", // Set the sheet name
    ExportArea = "A1:D10", // Define the export area
    ExportGridlines = true, // Export gridlines
    ExportImageAsBase64 = true,
};
// Save the workbook as an HTML file with the specified options
workbook.Save("report.html", htmlSaveOptions);

Export to JSON

Exporting reports in JSON format provides a structured and lightweight way to exchange data, which is ideal for web applications and APIs. JSON's readable format makes it easy to integrate with various programming languages and tools for seamless data processing.

You can save the workbook to a JSON file using the ToJson method. The saved JSON file can also be directly imported to our JavaScript Spreadsheet widget SpreadJS.

Below is the code to export the workbook to a JSON file: 

// Create a new workbook
var workbook = new Workbook();
// Load your data into the workbook or create your report
// Example: workbook.Open("path_to_your_template_or_data.xlsx");
// Save the workbook as a JSON file
string jsonContent = workbook.ToJson();
// Write the JSON content to a file
System.IO.File.WriteAllText("report.json", jsonContent);var jsonstr = workbook.ToJson();
File.WriteAllText(“result.json”, jsonstr);

Export to SJS

Exporting reports in SJS (SpreadJS) format leverages a powerful JavaScript spreadsheet library, enabling interactive data manipulation directly in web applications. This format is perfect for creating user-friendly spreadsheets that can be easily integrated and customized within any web environment.

To save the workbook to SJS file format via DsExcel, you can use the Save method with FileFormatEnum as SJS.

Below is the DsExcel code to export the workbook as an SJS file:

// Create a new workbook
var workbook = new Workbook();
// Load your data into the workbook or create your report
// Example: workbook.Open("path_to_your_template_or_data.xlsx");
// Save the workbook in SJS format
workbook.Save("report.sjs", SaveFileFormat.Sjs);

You can check out this sample to implement all the export formats mentioned above.

Conclusion

In this blog, we reviewed how to export Excel reports to various formats using the DsExcel API, making your data more accessible and versatile. Document Solutions for Excel makes it easy to programmatically integrate advanced features like filtering, sorting, conditional formatting, data validation, and more in your generated Excel files.

Ready to try it out? Download Document Solutions for Excel, .NET Edition Today!

If you have any questions about this topic, drop them in the comments below!

More References:

Tags:

comments powered by Disqus