JavaScript - Export Excel Javascript
Asked By Lokesh M on 27-Aug-08 02:03 AM
Hi All,
Is it possible to export html table contents to excel file using javascript
Advice me.
Thanks
Lokesh
yes
Web Star replied to Lokesh M on 27-Aug-08 02:06 AM
function CreateExcelSheet()
{
var x=myTable.rows
var xls = new ActiveXObject("Excel.Application")
xls.visible = true
xls.Workbooks.Add
for (i = 0; i < x.length; i++)
{
var y = x[i].cells
for (j = 0; j < y.length; j++)
{
xls.Cells( i+1, j+1).Value = y[j].innerText
}
}
u convert html table to excel
Sushma kumari replied to Lokesh M on 27-Aug-08 02:12 AM
<table id="tblExample" border="1">
<tr> <b><td>Name </td> <td>Age</td></b></tr>
<tr> <td>Shivani </td> <td>25</td> </tr>
<tr> <td>Naren </td> <td>28</td> </tr>
<tr> <td>Logs</td> <td>57</td> </tr>
<tr> <td>Kas</td> <td>54</td> </tr>
<tr> <td>Sent </td> <td>26</td> </tr>
<tr> <td>Bruce </td> <td>7</td> </tr>
</table>
function CreateExcelSheet()
{
var x=tblExample.rows
var xls = new ActiveXObject("Excel.Application")
xls.visible = true
xls.Workbooks.Add
for (i = 0; i < x.length; i++)
{
var y = x[i].cells
for (j = 0; j < y.length; j++)
{
xls.Cells( i+1, j+1).Value = y[j].innerText
}
}
}
</script>
Try this
Kalit Sikka replied to Lokesh M on 27-Aug-08 02:12 AM
u can try the following code,
DataSet ds=objAdmin.getErrorsInDetail();
Response.AppendHeader("Content-Disposition", "attachment; filename=Error.xls") ;
Response.ContentType="application/ms-excel";
Response.Write("<table>");
Response.Write("<tr>");
for(int i=0;i<ds.Tables[0].Columns.Count;i++)
{
Response.Write("<td>"+ ds.Tables[0].Columns[i].ColumnName +"</td>");
}
Response.Write("</tr>");
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
Response.Write("<tr>");
DataRow row=ds.Tables[0].Rows[i];
for(int j=0;j<ds.Tables[0].Columns.Count;j++)
{
Response.Write("<td>"+ row[j]+"</td>");
}
Response.Write("</tr>");
}
Response.Write("</table>");
Response.End();
Try this code
Atul Shinde replied to Lokesh M on 27-Aug-08 02:32 AM
Code:
function CreateExcelSheet()
{
var x=myTable.rows
var xls = new ActiveXObject("Excel.Application")
xls.visible = true
xls.Workbooks.Add
for (i = 0; i < x.length; i++)
{
var y = x[i].cells
for (j = 0; j < y.length; j++)
{
xls.Cells( i+1, j+1).Value = y[j].innerText
}
}
reply
Binny ch replied to Lokesh M on 27-Aug-08 02:34 AM
<html>
<head>
<script type="text/javascript">
function CreateExcelSheet()
{
var x=myTable.rows
var xls = new ActiveXObject("Excel.Application")
xls.visible = true
xls.Workbooks.Add
for (i = 0; i < x.length; i++)
{
var y = x[i].cells
for (j = 0; j < y.length; j++)
{
xls.Cells( i+1, j+1).Value = y[j].innerText
}
}
}
</script>
</head>
<body marginheight="0" marginwidth="0">
<form>
<input type="button" onclick="CreateExcelSheet()" value="Create Excel Sheet">
</form>
<table id="myTable" border="1">
<tr> <b><td>Name </td> <td>Age</td></b></tr>
<tr> <td>Shivani </td> <td>25</td> </tr>
<tr> <td>Naren </td> <td>28</td> </tr>
<tr> <td>Logs</td> <td>57</td> </tr>
<tr> <td>Kas</td> <td>54</td> </tr>
<tr> <td>Sent </td> <td>26</td> </tr>
<tr> <td>Bruce </td> <td>7</td> </tr>
</table>
</body>
</html>
see this example link:
http://www.webdeveloper.com/forum/archive/index.php/t-101730.html
javascript Error
Lokesh M replied to Sushma kumari on 27-Aug-08 02:38 AM
I tried the above code and i'm getting the following js error:
Automation Server Can't create an object.. at the line
var
xls = new ActiveXObject("Excel.Application")
Re : Export Excel Javascript
Deepak Ghule replied to Lokesh M on 27-Aug-08 02:41 AM
<script language=javascript>
function exportToExcel()
{
var oExcel = new ActiveXObject("Excel.Application");
var oBook = oExcel.Workbooks.Add;
var oSheet = oBook.Worksheets(1);
for (var y=0;y<detailsTable.rows.length;y++)
// detailsTable is the table where the content to be exported is
{
for (var x=0;x<detailsTable.rows(y).cells.length;x++)
{
oSheet.Cells(y+1,x+1) =
detailsTable.rows(y).cells(x).innerText;
}
}
oExcel.Visible = true;
oExcel.UserControl = true;
}
</script>
<button onclick="exportToExcel();">Export to Excel File</button>
<table name="detailsTable">
<tr>
<td> hello
</td>
</tr>
</table>
See this lokesh
Sagar P replied to Lokesh M on 27-Aug-08 03:14 AM
to solve your error;
This error comes because of IE's security mechanism, which allows for different security settings for Local intranet, internet, trusted sites etc. So when you are running the script from an HTML on your local disk, the browser considers this to be secure and allows it to run.
When you run it from wwwroot, I guess it would come under local intranet and would have a different set of permissions.
However, it should be quite easy to do.
Open IE -> Tools ->Internet Options -> Security -> Custom Level -> ActiveX controls and plug-ins ->Enable "Initialize and script ActiveX controls not marked as safe for scripting"
Best Luck!!!!!!!!!!!!!!!!!!
Sujit.
Export Excel Javascript
Lokesh M replied to Sagar P on 27-Aug-08 04:38 AM
Hi Sujit,
I had set the mentioned internet options in IE6.0 and IE7.0, but i'm still getting the same error. Where i'm wrong.
Thanks for the reply
Lokesh
Client Side Scripting
Lokesh M replied to Sagar P on 27-Aug-08 06:21 AM
Hi Sujit,
I'm sorry, Under the security tab - there are 4 zones, Internet, Local Internet, Trusted sites, Restricted Sites. I was doing mentioned option in Internet Zone but not in Local Internet. Now it is working fine...
Thanks
Lokesh
question
travis carmack replied to Binny ch on 20-Nov-09 05:02 PM
I like this code, however what if i have multiple <table>'s I want to export to excel.
I'm trying the following but it's not working:
var x=myTable.rows + myTable2.rows;
Of course i added a second id="myTable2" in the below the firs myTable. They have the same number of columns.
Any idea?
Jagadeesh replied to Deepak Ghule on 04-Aug-10 08:42 AM

Hi!!!!
I should be very thankful to you for the above code in Javascript.
My requirement is almost reached except one issue.I developed a table using Visualforce in Salesforce.I've added your javascript in the visualforce code.I was successful in exporting the data to Excel having the tags <apex:inputText> but not <apex:outputText>.
My sample code is below for your information:
<table id="ExportTable">
<tr>
<td width="10%" BGCOLOR="#99CCFF"><center><b>April</b></center> </td>
<td> <apex:inputText value="{!April1}" disabled="{!Apr1a}"/> </td>
<td> <apex:inputText value="{!April2}" disabled="{!Apr2a}"/></td>
<td> <apex:inputText value="{!April3}" disabled="{!Apr3a}"/> </td>
<td> <apex:outputText value="{0,number,0.00}" id="AprilTotal">
<apex:param value="{!AprilTotal}"/>
</apex:outputText>
</td>
</tr>
</table>
I was able to export the "AprilTotal" value in to Excel with your code but not the values in "April1","April2","April3".
Kindly suggest a solution on how to export the values in <apex:outputText> tags.
Thanks & Regards,
Jagadeesh K.
Mani replied to Jagadeesh on 03-Sep-10 08:30 AM
Can any one suggest the way to do the same for firefox ?
Mandar replied to Kalit Sikka on 08-Nov-10 10:44 AM
This code doesn't work. And I am searching for the similar code as creating excel application through active x control is not allowed and blocked.
If u have any client side code that doesn't use active-x and exports HTML data to excel. Please do provide it to me.
joseph replied to Sushma kumari on 30-Nov-10 06:20 AM
Thank you for the post . It helped me a lot and worked from my side.
Utsav replied to joseph on 27-Dec-10 12:54 AM
can anyone help me getting the same coloring format, in the excel sheet, for the text and the background as it is in HTML view.
Thank you.
Marlene replied to Utsav on 15-Apr-11 03:58 AM
HI
I have the same issue as you, did you get the coloring right?
Please help.
Thanks
Marlene