Excel File Viewer – Display Excel Data using C#

We can display data from Excel files in HTML, PDF, or as an image programmatically in .NET applications. It allows showing data to others without sharing the actual Excel files. In this article, we will learn how to create an Excel file viewer and display Excel data using C#.

The following topics shall be covered in this article:

C# Excel File Viewer API — Free Download

For displaying data from XLS or XLSX spreadsheets, we will be using GroupDocs.Viewer for .NET API. It allows rendering and viewing of supported spreadsheet formats programmatically. Please either download the DLL of the API or install it using NuGet.

PM> Install-Package GroupDocs.Viewer

Display Excel Data in HTML using C#

We can render the Excel file and display data in HTML by following the simple steps given below:

  1. Firstly, load an Excel file using the _Viewer _class.
  2. Create an instance of the HtmlViewOptions class for EmbeddedResources.
  3. Provide the output file path as an argument.
  4. Optionally, set various view options, such as RenderToSinglePage.
  5. Finally, call the View() method and pass HtmlViewOptions as an argument.

The following code sample shows how to render an Excel file in HTML using C#.

Display Excel Data in HTML using C#.

Display Excel Data in HTML using C#.

Render Excel Data in PDF using C#

We can render the Excel file and display data in PDF by following the steps given below:

  1. Firstly, load an Excel file using the Viewer class.
  2. Create an instance of the PdfViewOptions class.
  3. Provide the output file path as an argument.
  4. Finally, call the View() method and pass PdfViewOptions as an argument.

The following code sample shows how to render an Excel file in PDF using C#.

Render Excel Data in PDF using C#.

Render Excel Data in PDF using C#.

View Excel File as JPG Image using C#

We can render the Excel file and display data as JPG images by following the steps given below:

  1. Firstly, load an Excel file using the Viewer class.
  2. Create an instance of the JpgViewOptions class.
  3. Provide the output file path.
  4. Finally, call the View() method and pass JpgViewOptions as an argument.

The following code sample shows how to render an Excel file as JPG using C#.

View Excel File as JPG Image using C#.

View Excel File as JPG Image using C#.

Similarly, we can also render an Excel file to PNG images as shown below:

Adjust Text Overflow in Cells using C#

We can adjust the text-overflow in cells while rendering an Excel worksheet. The API provides the following types of overflow adjustments:

  • Overlay – Overlay next cells even they are not empty.
  • OverlayIfNextIsEmpty – Overlay next cells only if they are empty.
  • AutoFitColumn – Expand columns to fit the text.
  • HideText – Hide overflow text.

Please follow the steps given below to adjust the text-overflow:

  1. Firstly, load an Excel file using the _Viewer _class.
  2. Create an instance of PdfViewOptions class
  3. Provide the output file path.
  4. Set TextOverflowMode property of SpreadsheetOptions to HideText.
  5. Optionally, set RenderHeadings and RenderGridLines to true.
  6. Finally, call the View() method and pass PdfViewOptions as an argument.

The following code sample shows how to adjust text-overflow while rendering an Excel file using C#.

Adjust Text Overflow in Cells using C#.

Adjust Text Overflow in Cells using C#.

Render Hidden Rows and Columns of Excel

We can render the hidden rows and columns of an Excel worksheet by following the steps mentioned earlier. However, we just need to set the following properties to true at step # 4:

viewOptions.SpreadsheetOptions.RenderHiddenColumns = true;
viewOptions.SpreadsheetOptions.RenderHiddenRows = true;

The following code sample shows how to show hidden rows and columns of an Excel file in PDF using C#.

Render Hidden Rows and Columns of Excel.

Render Hidden Rows and Columns of Excel.

Skip Empty Rows and Columns in Excel using C#

We can skip the rendering of empty rows and columns while viewing the Excel worksheet by following the steps mentioned earlier. However, we just need to set the following properties to true at step # 4:

viewOptions.SpreadsheetOptions.SkipEmptyColumns = true;
viewOptions.SpreadsheetOptions.SkipEmptyRows = true;

The following code sample shows how to skip the rendering of empty rows and columns of an Excel file using C#.

Skip Empty Rows and Columns in Excel using C#

Skip Empty Rows and Columns in Excel using C#.

Split Excel Worksheet by Rows and Columns

We can render large Excel worksheets and split them by the number of rows and columns on one page. We can split the worksheet by following the steps given below:

  1. Firstly, load an Excel file using the _Viewer _class.
  2. Create an instance of PdfViewOptions class
  3. Provide the output file path.
  4. Initialize SpreadsheetOptions using the ForSplitSheetIntoPages method. It takes count of rows and columns per page as arguments.
  5. Finally, call the View() method and pass PdfViewOptions as an argument.

The following code sample shows how to split an Excel worksheet by rows and columns using C#.

Split Excel Worksheet by Rows and Columns

Split Excel Worksheet by Rows and Columns.

Get a Free License

Please try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, we have learned how to:

  • render or view Excel worksheets in HTML, PDF, PNG, and JPG using C#;
  • adjust text overflow in Cells of Excel and render grid lines;
  • display headings of Excel columns and rows;
  • skip empty rows/columns and show hidden rows and columns;
  • limit display of worksheets by rows and columns.

Besides, you can learn more about GroupDocs.Viewer for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also