faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

101 of 118 people (86%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

Is there a way to write text to an MS Excel file from a javascript user prompt?

Apr 5th, 2008 20:02
ha mo, David Carlisle, Mark Szlazak, Neel Desai,


This function uses ActiveX to get a forms input into a spreadsheet cell.
function xlWrite(r,c) {
  var xlApp, xlSheet;
  xlApp = new ActiveXObject("Excel.Application");
  xlApp.Visible = true;
  xlApp.Workbooks.Add();
  xlSheet = xlApp.ActiveSheet;
  xlSheet.Cells(r,c).Value = frm.txt.value;
  xlSheet.SaveAs("C:\\xlText.xls");
Note that Excel has a bug and doesn't shut down after calling the 
Quit() method when automating from JScript. To work around this 
problem, the CollectGarbage() method is used to force JScript's garbage 
collection to occur almost immediately.
  xlApp.Quit();
  xlApp = null;
  setTimeout("CollectGarbage()",1);
}
I set the form up so the default input should be in the cell of the 
first row and second column.
<FORM NAME="frm">
  <INPUT TYPE="Text" NAME="txt" Value="Some cell text"><BR>
  <INPUT TYPE="Button" VALUE="Put in row 1, column 2" ONCLICK="xlWrite
(1,2)">
</FORM>
Also, to avoid the "File exists, overwrite (Yes,No)" dialog - use 
SaveCopyAs instead of SaveAs.
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com