Create, read, write Excel files in Classic ASP

GemBox.Spreadsheet is a .NET library for processing Excel files, but since it's also a COM-accessible library, you can use it in Classic ASP web pages

System Requirements

To use GemBox.Spreadsheet in VBScript, you'll need to:

  1. Download and install GemBox.Spreadsheet Setup.
  2. Expose GemBox.Spreadsheet to COM Interop with Regasm.exe tool:
    :: Add GemBox.Spreadsheet to COM registry for x86 (32-bit) applications.
    C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe [path to installed GemBox.Spreadsheet.dll]
    
    :: Add GemBox.Spreadsheet to COM registry for x64 (64-bit) applications.
    C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe [path to installed GemBox.Spreadsheet.dll]

Working with Excel files in Classic ASP

The following example shows how you can create a new Excel file from a Classic ASP application and write some spreadsheet data into its cells. Also, the example shows how you can read an existing Excel file and update its cells.

<%
  ' Create ComHelper object.
  Set comHelper = CreateObject("GemBox.Spreadsheet.ComHelper")
  ' If using the Professional version, put your serial key below.
  comHelper.ComSetLicense("FREE-LIMITED-KEY")

  ''''''''''''''''''''
  ''' Create Excel '''
  ''''''''''''''''''''

  ' Create new ExcelFile object.
  Set workbook = CreateObject("GemBox.Spreadsheet.ExcelFile")
  ' Add new ExcelWorksheet object.
  Set worksheet = workbook.Worksheets.Add("Sheet1")

  ' Set width and format of column "A".
  Set columnA = comHelper.GetColumn(worksheet, 0)
  columnA.Width = 20 * 256
  columnA.Style.Font.Weight = 700

  ' Set values of cells "A1", "A2", "A3" and "A4".
  columnA.Cells.Item(0).Value = "John Doe"
  columnA.Cells.Item(1).Value = "Bob Garvey"
  columnA.Cells.Item(2).Value = "Ben Stilwell"
  columnA.Cells.Item(3).Value = "Peter Pan"
  
  ' Set values of cells "B1", "B2", "B3" and "B4".
  Set columnB = comHelper.GetColumn(worksheet, 1)
  columnB.Cells.Item(0).Value = 1000
  columnB.Cells.Item(1).Value = 2000
  columnB.Cells.Item(2).Value = 3000
  columnB.Cells.Item(3).Value = 4000

 ' Create new Excel file.
  workbook.Save(Server.MapPath(".") & "\New.xlsx")

  ''''''''''''''''''
  ''' Read Excel '''
  ''''''''''''''''''

  ' Read existing Excel file.
  Set book = comHelper.Load(Server.MapPath(".") & "\New.xlsx")
  ' Get first Excel sheet.
  Set sheet = book.Worksheets.Item(0)
  ' Get first Excel row.
  Set row1 = comHelper.GetRow(sheet, 0)

  ' Display values of cells "A1" and "B1".
  Response.Write("Cell A1:" & row1.Cells.Item(0).Value)
  Response.Write("<br>")
  Response.Write("Cell B1:" & row1.Cells.Item(1).Value)

  ''''''''''''''''''''
  ''' Update Excel '''
  ''''''''''''''''''''

  ' Update values of cells "A1" and "B1".
  row1.Cells.Item(0).Value = "Jane Doe"
  row1.Cells.Item(1).Value = 2000

  ' Write the updated Excel file.
  book.Save(Server.MapPath(".") & "\Updated.xlsx")
%>
Reading and writing spreadsheet data from new and existing Excel files in Classic ASP
Screenshot of a newly created and then updated Excel file

Wrapper Library

Not all members of GemBox.Spreadsheet are accessible because of the COM limitations like unsupported static and overload methods. That is why you can use the ComHelper class which provides alternatives for some members that cannot be called with COM Interop.

However, if you need to use many GemBox.Spreadsheet members from VBScript, a recommended approach is to create a .NET wrapper library instead. Your wrapper library should do all the work within and exposes a minimal set of classes and methods to the unmanaged code.

This will enable you to take advantage of GemBox.Spreadsheet's full capabilities, avoid any COM limitations, and improve performance by reducing the number of COM Callable Wrappers created at runtime.

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy