How to Use Javascript With Excel

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.
Create Excel spreadsheets with Javascript.

The Javascript language provides developers with the tools to set dynamic content after a web page has loaded into the user's web browser. You can use Excel with Javascript and generate a spreadsheet for your web readers. This opens an Excel spreadsheet with the content you specify, and the user can choose to read, edit and save the file onto the computer. This is beneficial for web developers who want to create Excel spreadsheets on the fly in their web pages.


Step 1

Create your Javascript block. This block indicates to the web browser that the contained code is executable scripts. Add the following code between the "<head>" and "</head>" tags in your HTML web page:

Video of the Day

<script language="javascript"> </script>


All your Javascript that interfaces with Excel is placed within these script blocks.

Step 2

Initiate the Excel application variable. This variable loads the Excel libraries, which are used to interact with Excel. The following code initiates your variable:


var excel = new ActiveXObject ( "Excel.Application" ); excel.visible = true;

Step 3

Create the spreadsheet and activate it. After the Excel variable initializes, you need to create a workbook and worksheet, which are the components of an Excel file that contain your information. The following code creates the spreadsheet:


var book = excel.Workbooks.Add; book.Worksheets.Add; book.Worksheets(1).Activate;

Step 4

Write some text to the Excel worksheet. You can fill each row of the spreadsheet line by line. In this example, the first cell is set for a string value:



book.Worksheets(1).Cells(1,1).value="My First Spreadsheet";

Step 5

Save the Excel file. This is an optional step, and it opens a "Save As" prompt for the user. The user is asked if he wants to save the file and to what folder the file is saved. If you are writing an internal application for a business, you can set the browser's security to explicitly allow the document to save to the hard drive without any user interaction, but this is a security hazard for outside web visitors who do not trust the website. The following code asks the user if he wants to save the file:





Report an Issue

screenshot of the current page

Screenshot loading...