473,372 Members | 717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

How to mimic Excel download?

kj

Executive summary:

Is there a way for the client side JavaScript to tell the browser
to use MS Excel to view some data?

The question:

I'm changing an webapp from standard CGI to pure AJAX. I.e. only
one page gets loaded at startup, and all subsequent interaction is
done exclusively via JavaScript or AJAX.

The original app had a "Send results as spreadsheet" checkbox that
when checked would instruct the server to send the results in the
form of an Excel spreadsheet (setting the Content-type header of
the response to application/vnd.ms-excel).

(Actually, the table that gets returned is very simple. It does
not include any Excel-specific features. I understand that even
if it were sent as a plain-text TSV table, as long as the Content-type
header was set as above, Excel would be able to handle it. But I
have not tested this in depth.)

In the new version of the app, normally (i.e. when the spreadsheet
box is not checked) the data is retrieved from a web service through
an AJAX call. This web service knows nothing of CGI. It just
returns some JSON-encoded data in tabular form.

One (admittedly disgusting) way I could achieve the same effect as
before when the spreadsheet box is checked is simply to bounce the
data back to a server-side CGI script whose only job is to bounce
the data again to the client, with the appropriate Content-type
headers.

But I was wondering if there is a way to achieve the same effect
without this otherwise unnecessary extra round-trip of the data.
In other words, is there a way for the client side JavaScript to
tell the browser to use Excel to view some data?

TIA!

Kynn


--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jun 27 '08 #1
2 2472
Hi Kynn,

I see variations of this post often - and my experience is that people
that don't know Excel get lost when it comes to understanding my
solution.

See if you can follow:

This example will get you started.

If you get this going and can't do it on your own - I'll take a look
at extending this code to dump data from a YUI DataSource.

Yes, alternatively you could use the Java POI library to create
Workbooks on the server side - but you can't create VBA macros with
POI. Off hand I don't think POI supports creating multiple Worksheets
either.

This solution allows you to build a real Excel Workbook complete with
VBA modules and multiple Worksheets.

It only works using IE though.

And you might have to modify your security settings:
Tools->Options->Security->Macro Security (Medium).

Save the following as h2.html :

<html>
<body>
<script language="JavaScript">
<!--
function view_report() {
var form = document.forms['excel_form'];
form.elements['emmetts_1'].value = 100;
form.elements['emmetts_2'].value = "This is some text hello
world";
form.submit();
}
//-->
</script>
<form name="excel_form" action ="h2.hta"/>
<input type="hidden" name="emmetts_1" value=""/>
<input type="hidden" name="emmetts_2" value=""/>
<input type="button" value="View Report" onclick="view_report();"/>
</form>
</body>
</html>

Now save the following into a file named h2.hta . NOTE the HTA suffix.

<head>

<title>Excel Publish Example</title>
<HTA:APPLICATION
APPLICATIONNAME="Excel Publish Example"
SCROLL="no"
SINGLEINSTANCE="yes"
>
</head>

<script language="JavaScript">
var emmetts_1 = "";
var emmetts_2 = "";
var qs = window.location.search.substring(1);
var qe = qs.split("&")
var q1 = qe[0].split("=");
emmetts_1 = q1[1];
var q2 = qe[1].split("=");
emmetts_2 = q2[1];

function build_workbook() {
var scr = "Set objXL = CreateObject(\"Excel.Application\")\n";
scr += "objXL.Visible = True\n";
scr += "Set objwb = objXL.Workbooks.Add\n";
scr += "Set objws = objwb.Sheets.Add\n";
scr += populate_worksheet();
return scr;
}

function populate_worksheet() {
da = "data = Array (\"Hello\",\"World\")\n";
da += "objws.Range(objws.Cells(1,1),objws.Cells(1,2)).Va lue = data\n";
da += "data = Array (\"Emmetts1\",\""+emmetts_1+"\")\n";
da += "objws.Range(objws.Cells(2,1),objws.Cells(2,2)).Va lue = data\n";
da += "data = Array (\"Emmetts2\",\""+ emmetts_2 +"\")\n";

da += "objws.Range(objws.Cells(3,1),objws.Cells(3,2)).Va lue = data\n";
return da;
}
// alert(build_workbook());
</script>

<script language="VBScript">
Function CreateReport
Command = build_workbook()
Set ms = CreateObject("msscriptcontrol.scriptcontrol")
ms.language = "vbscript"
ms.AllowUI = True
ms.ExecuteStatement Command
Window.Close
End Function

CreateReport
</script>

<body bgcolor="lightblue">
Excel Publish Example
</body>
Vist h2.html and click "View Report".


Jun 27 '08 #2
kj

In <d4**********************************@u36g2000prf. googlegroups.com"gi*******************@yahoo.com" <gi*******************@yahoo.comwrites:
>Hi Kynn,
>I see variations of this post often - and my experience is that people
that don't know Excel get lost when it comes to understanding my
solution.
Thanks! I'll give it a go.

k.

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: jillandgordon | last post by:
I am trying to import an excel file into Access 97. It looks perfectly all right but, every time I try to import it, I get to the lst step and am told that it was not imported due to an error. ...
4
by: Ali | last post by:
I need a functionality where my clients download Excel files and after they do, I do some processing. Downloading is easily achieved using a anchor or hyperlink tag, but that does not give me the...
4
by: Lewis Edward Moten III | last post by:
I have a file that users can download through a web page protected by forms authentication: Download.aspx?ID=45 and within that file ... FileInfo fileToDownload = new FileInfo(fileName);
4
by: Vishal | last post by:
Hello, I have a dataset with some content. I can convert this dataset in a csv format which is then stored in a stringwriter. Now I want this be open via excel, so I set the contenttype...
3
by: Rik Moed | last post by:
Hi, I am having a problem with Excel 2003 worksheets when I upload them using the HtmlIputFile. After the upload, I start to download the worksheet and it appears to be currupt. I recieve the...
1
by: George Homorozeanu | last post by:
We create an excel object with Excel/Office Interop Assembly and we want to offer this excel object as download without creating physically the file. The download window appears with "Save As.",...
0
by: techusky | last post by:
I'm in the process of making a custom directory listing script for my website, and I want to learn how to mimic Gmail's feature that lets you download all the attachments in an e-mail as a single...
6
by: Eddie | last post by:
Hi all, I am displaying a number of reports, and giving the users an option to display them on the web or download them to Excel. If they want the Excel file, I just use the PHP header command...
0
by: =?Utf-8?B?ZGlzcGxheW5hbWU=?= | last post by:
The following code used to outputs to Excel 2003 fine. Do I need to change it to output to Excel 2007? <% Response.ContentType = "application/vnd.ms-excel" Response.AddHeader...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.