Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exporting to Excel 1

Status
Not open for further replies.

DANDARSMASH

Programmer
May 17, 2013
17
US
Hello,

I modified the code from here but i can't get it to work on my site. the only thing i changed was the id of the table: tbl2013-06-03

JavaScript:
function write_to_excel() 
{
    str="";
    var myTable = document.getElementByID('tbl2013-06-03');
    var rows = myTable.getElementsByTagName('tr');
    var rowCount = myTable.rows.length;
    var colCount = myTable.getElementsByTagName("tr")[0].getElementsByTagName("th").length; 

    var ExcelApp = new ActiveXObject("Excel.Application");
    var ExcelWorkbook = ExcelApp.Workbooks.Add();
    var ExcelSheet = ExcelWorkbook.ActiveSheet;//new ActiveXObject("Excel.Sheet"); 
    //ExcelSheet.Application.Visible = true;
    ExcelApp.Visible = true;

        ExcelSheet.Range("A1", "Z1").Font.Bold = true;
    ExcelSheet.Range("A1", "Z1").Font.ColorIndex = 23;     

//Format table headers
    for(var i=0; i<1; i++) 
    {   
        for(var j=0; j<colCount-2; j++) 
        {           
            str= myTable.getElementsByTagName("tr")[i].getElementsByTagName("th")[j].innerHTML;
            ExcelSheet.Cells(i+1,j+1).Value = str;
        }
        ExcelSheet.Range("A1", "Z1").EntireColumn.AutoFit();
    }
    for(var i=1; i<rowCount; i++) 
    {
        for(var k=0; k<colCount-2; k++) 
        {
            str= rows[i].getElementsByTagName('td')[k].innerHTML;
            ExcelSheet.Cells(i+1,k+1).Value = myTable.rows[i].cells[k].innerText;
        }
        ExcelSheet.Range("A"+i, "Z"+i).WrapText = true;
        ExcelSheet.Range("A"+1, "Z"+i).EntireColumn.AutoFit();
    }

    //ExcelSheet.SaveAs("C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\thankYouExport.xls");
    //ExcelSheet.Application.Quit();

    return; 
}

and then my call:

HTML:
<a href="javascript:write_to_excel();">Export to Excel</a>

Thanks in advance for any help with this.
 
what browser and what operating system?
and do you have excel installed and the correct permissions in your browser security?

 
I'm trying this in firefox, chrome, and ie. I'm running Windows 7. Excel is installed, and I am an administrator (all permissions)
 
can you confirm that in IE you have explicitly set the correct permissions to run unsafe activex? post a screen shot to confirm.
 
I am not getting any error, clicking the link does nothing at all. Here is a screenshot, all of the settings are at their lowest.

ieActiveX.jpg
 
and have you set up active content permissions in the advanced tab (needs a restart)?

and, as with any error-less bug, go back to basics and build from there. so try this (in IE only - of course the others wont work)

Code:
if (window.ActiveXObject) {
    try {
        var xl = new ActiveXObject("Excel.Application");
        xl.Visible = true;
    }
    catch (e) {
        alert (e.message);
    }
}
else {
    alert ("Your browser does not support this example.");
}
 
@jpadie- that opens up the excel application, so thanks.

@Diancecht- no yellow warning at all.
 
@jpadie- yes, thanks. We decided to go with a slightly different method; pulling directly from the DB to excel. It cut out several languages that didn't need to be there.
 
yes. much better to do that kind of thing as a script. easy to do with server side scripts or cgi's like php. easy also to do in windows using odbc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top