Data Import & Export

Fix a VB.NET Excel Export That Opens as a Gray Screen

Emma BrownEmma Brown Nov 25, 2026 868 views

Question details

The user needs an Excel file generated by an ASP.NET or VB.NET response to open as a workbook instead of showing a gray screen.

Fix a VB.NET Excel Export That Opens as a Gray Screen
Product
Microsoft Excel and ASP.NET/VB.NET
Device & OS
not provided
Scenario
An ASP.NET application exports a DataTable or DataGrid for users to open in Excel.
Observed behavior
The downloaded file may open to a blank or gray Excel window when its content, response headers, or filename are invalid.
Before you start

Check the export code for the actual response content, Content-Type, Content-Disposition, and filename. Confirm whether the intended output is a genuine .xls or .xlsx workbook rather than HTML with an Excel extension.

Solution 1Recommended

Generate a Valid Excel Workbook with NPOI

Use this approach when the application sends HTML or DataGrid markup as an Excel file.

A filename extension and MIME type do not convert HTML into an Excel workbook. Create workbook bytes that match the extension you send.

1
Install NPOI

In Visual Studio, open Tools > NuGet Package Manager > Manage NuGet Packages for Solution, search for NPOI, and install it in the export project.

2
Create the workbook

In the VB.NET export handler, create an NPOI HSSFWorkbook for .xls, add a worksheet with CreateSheet, and use CreateRow and CreateCell to copy the DataTable column names and rows into it.

3
Write the response

Write the workbook to a MemoryStream. Before sending the bytes, clear the response, set Response.ContentType to application/vnd.ms-excel, and add a Content-Disposition header such as attachment; filename="report.xls". Send the stream's bytes with Response.BinaryWrite.

4
Stop page output and test

After writing the file, call HttpContext.Current.ApplicationInstance.CompleteRequest() and return from the handler so no HTML is appended. Download report.xls and open it in Excel to verify that the worksheet and data appear.

Generate a Valid Excel Workbook with NPOI
For .xlsx exports: Use NPOI XSSFWorkbook, a single .xlsx extension, and the MIME type application/vnd.openxmlformats-officedocument.spreadsheetml.sheet instead.
Free Microsoft Office alternative

Open Valid Excel Exports with WPS Spreadsheet

WPS Office is a free, lightweight alternative with a familiar interface and support for common Microsoft Office formats, making it easy to move existing files. WPS Spreadsheet can open a valid export, but it cannot repair incorrect server-side workbook generation.

Open valid .xls and .xlsx exports in WPS SpreadsheetCompatible with common Microsoft Excel workbook formatsUse a familiar spreadsheet interface with existing filesWork with a free, lightweight Office alternative
microsoft office alternative - wps office

Frequently Asked Questions

Can I export a DataGrid as HTML and name it .xls?

Renaming HTML to .xls does not create a genuine Excel workbook and can cause compatibility warnings or opening problems. Use a library such as NPOI to create workbook and worksheet bytes instead.

Which Content-Type should I use for an Excel export?

Use application/vnd.ms-excel for a genuine .xls workbook. For a genuine .xlsx workbook, use application/vnd.openxmlformats-officedocument.spreadsheetml.sheet. The response content and filename extension must match the format.

Why does my downloaded filename have two .xls extensions?

The export code may append .xls to a name that already contains it. Check the filename in the Content-Disposition header and construct it with only one extension.

Why might a valid workbook become unreadable during download?

Extra HTML, an error message, or other page output can be appended to the workbook bytes. In the export handler, send only the workbook response, call CompleteRequest, and return without rendering the page.