How to write and open an Excel file in browser using Response stream
EasyXLS™ library allows you to create Excel files in xls, xlsx, xlsm or xlsb file format. The Excel file can be written to a stream like the Response stream and opened in web browser.
An open/save dialog is opened in browser from ASP.NET or Classic ASP (VBScript) and launches the Excel file directly from the browser.
Source code sample
The below example shows how to export an Excel file in C#, VB.NET and J# from ASP.NET and also Classic ASP. When the export is finished, the Excel file is opened in browser.
// Create an instance of the class that creates Excel files
ExcelDocument workbook = new ExcelDocument();
...
// Choose a name for the Excel filestring shortFileName = "Excel.xls";
// Prepare Response stream
Response.AppendHeader("content-disposition",
"attachment; filename=" + shortFileName);
Response.ContentType = "application/octetstream";
Response.Clear();
// Write Excel file and prompt the "Open or Save Dialog Box"
workbook.easy_WriteXLSFile(Response.OutputStream);
// Dispose memory
workbook.Dispose();
Response.End()
' Create an instance of the class that creates Excel filesDim workbook As New ExcelDocument
...
' Choose a name for the Excel fileDim shortFileName As String = "Excel.xls"' Prepare Response stream
Response.AppendHeader("content-disposition", _
"attachment; filename=" + shortFileName)
Response.ContentType = "application/octetstream"
Response.Clear()
' Write Excel file and prompt the "Open or Save Dialog Box"
workbook.easy_WriteXLSFile(Response.OutputStream)
' Dispose memory
workbook.Dispose()
Response.End()
' Create an instance of the class that creates Excel filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
...
' Write Excel file on hard-disk
workbook.easy_WriteXLSFile ("<AbsolutePath>/Excel.xls")
' Open the previously written Excel fileDim strXlsFileName
strXlsFileName = Server.MapPath("<RelativePath>/Excel.xls")
Dim objFileSystem
Set objFileSystem = Server.CreateObject("Scripting.FileSystemObject")
If objFileSystem.FileExists(strXlsFileName) ThenDim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1 'Binary Type
objStream.LoadFromFile strXlsFileName
' Prepare Response stream
Response.Clear
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", _
"attachment; filename=" & strXlsFileName & ""
Response.BinaryWrite objStream.Read
objStream.close
Set objStream = Nothing
Response.End
End If
<!-- Create an instance of the class that creates Excel files --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE">
...
<!-- Write Excel file on hard-disk --><cfset ret = workbook.easy_WriteXLSXFile("C:\Samples\Excel.xlsx")><!-- Open the previously written Excel file --><cfheader name="Content-Disposition"value="inline; filename=Excel.xlsx"><cfcontent type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"file="C:\Samples\Excel.xlsx">