Sayareddy G

Sayareddy G

  • NA
  • 62
  • 19.4k

Export html table to excel with styles using jquery

Sep 5 2013 1:54 AM
Hi Guys,

I am trying to export html table to excel using below code. Here I am not able to apply styles and format cells like text alignment using jquery. And below code is not working in IE also. Please help me out to resolve above issues.

function exportToExcel() {
            var oExcel = new ActiveXObject("Excel.Application");
            var oBook = oExcel.Workbooks.Add;
            var oSheet = oBook.Worksheets(1);
            oSheet.Range("A1", "Z1").Font.Bold = true;
            oSheet.Range("A1", "Z1").Font.ColorIndex = 23;

            for (var i = 0; i < detailsTable.rows.length; i++) {
                for (var j = 0; j < detailsTable.rows(i).cells.length; j++) {
                    oSheet.Cells(i + 1, j + 1) = detailsTable.rows(i).cells(j).innerText;
                }
            }
            oSheet.Range("A" + i, "Z" + i).WrapText = true;
            oSheet.Range("A" + 1, "Z" + i).EntireColumn.AutoFit();

            oExcel.Visible = true;
            oExcel.UserControl = true;
        }

Any help would be greatly appreciated.

Regards,
Saya

Answers (2)