Click to See Complete Forum and Search --> : This topic has been beat to death...
BrainDonor
05-29-2003, 04:31 PM
I have searched around in the forums and not been able to find exactly what I'm looking for...
I was to be able to use the code below to not only open Excel, but to take the entire HTML page and copy it into a new sheet. The code below works, but what I can't figure out (I have been really tired lately :) ) is how to replace the "this works" with the actual data from the table. Any thoughts/ideas?
<html>
<head>
<script>
function copyToExcel()
{
ExcelSheetApp = new ActiveXObject("Excel.Application");
ExcelSheet = new ActiveXObject("Excel.Sheet");
ExcelSheet.Application.Visible = true;
ExcelSheet.ActiveSheet.Cells(1,1).Value = "this works";
}
</script>
</head>
<body>
<table border=1 width=50%>
<tr><td bgcolor=#D3D3D3>1. I'd like this data to go to Excel</td></tr>
<tr><td>2. This too.</td></tr>
<tr><td bgcolor=#D3D3D3>3. Last but not least...</td></tr>
</table>
<a href="javascript:copyToExcel()">click here to send data to excel</a>
</body>
</html>
Thanks for any help you can offer!
Tom
BrainDonor
05-30-2003, 11:38 AM
I'm not sure what the correct syntax is for all of this...please forgive my ignorance. I'm not a web developer, I'm an Informix Programmer creating reports that are being output in HTML for our Intranet. My experience with Java is almost nonexistent. :)
Here's what I have now...what do I need to change to get this to work properly? Thanks again for your help!!
Tom
<html>
<head>
<script>
function copyToExcel(varname)
{
var tbl=document.getElementById(varname);
ExcelSheetApp = new ActiveXObject("Excel.Application");
ExcelSheet = new ActiveXObject("Excel.Sheet");
ExcelSheet.Application.Visible = true;
ExcelSheet.ActiveSheet.Cells(1,1).Value=tbl;
}
</script>
</head>
<body>
<table name="test" border=1 width=50%>
<tr><td bgcolor=#D3D3D3>1. I'd like this data to go to Excel</td></tr>
<tr><td>2. This too.</td></tr>
<tr><td bgcolor=#D3D3D3>3. Last but not least...</td></tr>
</table>
<a href="javascript:copyToExcel("test");">click here to send data to excel</a>
</body>
</html>
khalidali63
05-30-2003, 01:47 PM
just for you to comprehend what dave mentioned above.
give id attributes to the td( or cells) which you need to get the value of.It will be something like below
<table border=1 width=50%>
<tr><td id="cel_1" bgcolor=#D3D3D3>1. I'd like this data to go to Excel</td></tr>
<tr><td id="cel_2">2. This too.</td></tr>
<tr><td id="cel_3"bgcolor=#D3D3D3>3. Last but not least...</td></tr>
</table>
now in the javascript section get values from cells and update the spread sheet
here is the updated code for JS part of it
<script>
function copyToExcel(varname)
{
var tbl=document.getElementById(varname);
ExcelSheetApp = new ActiveXObject("Excel.Application");
ExcelSheet = new ActiveXObject("Excel.Sheet");
ExcelSheet.Application.Visible = true;
var
cell1=document.getElementById("cell_1").innerHTML;
var cell2=document.getElementById("cell_2").innerHTML;
//now put the values in spread sheet
ExcelSheet.ActiveSheet.Cells(1,1).Value=cell1;
ExcelSheet.ActiveSheet.Cells(1,2).Value=cell2;
}
</script>
Hope this helps
BrainDonor
05-30-2003, 02:12 PM
Yes, it helps...thanks very much. Just could get really ugly if I have thousands of cells to fill. hehehe
:)
Tom
khalidali63
05-30-2003, 02:16 PM
You are welcome.
,naah its not that bad.now that you know how to use getElementById
you can use it id a row or a table and then get the required infor frm cells that way...
khalidali63
05-30-2003, 05:10 PM
Originally posted by Dave Clark
Well, you were misled just a triffle...
Dave
:confused:
BrainDonor
06-04-2003, 02:46 PM
What's happening now is that this bringing over the cell values into cell a1, but it is also bring over all of the underlying HTML code and putting it all into cell a1, rather than taking each TD and putting it in its own cell. Help! :)
Here's the code.
[EDIT]
Grabbed wrong code...here's the actual code.
<html>
<head>
<script>
function copyToExcel(varname) {
ExcelSheetApp = new ActiveXObject("Excel.Application");
ExcelSheet = new ActiveXObject("Excel.Sheet");
ExcelSheet.Application.Visible = true;
var tbl=document.getElementById("test").innerHTML;
//now put the values in spread sheet
ExcelSheet.ActiveSheet.Cells(1,1).Value=tbl;
}
</script>
</head>
<body>
<table id="test" border=1 width=50%>
<tr><td bgcolor=#D3D3D3>1. I'd like this data to go to Excel</td></tr>
<tr><td>2. This too.</td></tr>
<tr><td bgcolor=#D3D3D3>3. Last but not least...</td></tr>
</table>
<a href="javascript:copyToExcel('test');">click here to send data to excel</a>
</body>
</html>
Thanks!
Tom
BrainDonor
06-04-2003, 04:54 PM
As an add-on, I'd like this to show in Excel the same way it would if the user did a copy and paste.
Thanks again!
Tom
BrainDonor
06-05-2003, 11:19 AM
bump...
:D
greettech
11-16-2007, 07:17 AM
HI all,
I have tried this code.
it is printing the hole htmle table format.like this "<table id="test" border=1 width=50%>
<tr><td bgcolor=#D3D3D3>1. I'd like this data to go to Excel</td></tr>
<tr><td>2. This too.</td></tr>
<tr><td bgcolor=#D3D3D3>3. Last but not least...</td></tr>
</table>"
in cel(1,1)
but i want the values to be printed on excel sheet not the hole html table format.
like i have a table of 20 cloumns, how to print it to excel sheet?
can any body help me?
plase its urgent.
regards,
greet
toicontien
11-16-2007, 09:47 AM
Replying to BrainDonor's topic:
Read up on the TABLE Document Object Model. This can help you out, because each TABLE, TBODY, THEAD and TFOOT element has a property called rows which contain node references to all the TR elements within. Then each row has a property called cells, which contains node references to all the TD elements.
Table DOM (http://www.w3schools.com/htmldom/dom_obj_table.asp)
TR DOM (http://www.w3schools.com/htmldom/dom_obj_tablerow.asp)
TD DOM (http://www.w3schools.com/htmldom/dom_obj_tabledata.asp)