Fix a VB.NET Excel Export That Opens as a Gray Screen
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.

- 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.
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.
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.
In Visual Studio, open Tools > NuGet Package Manager > Manage NuGet Packages for Solution, search for NPOI, and install it in the export project.
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.
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.
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.

Check the Filename and Headers of an Existing Workbook Export
Use this check if the application already generates genuine Excel workbook bytes.
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.

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.




