当前位置: 代码迷 >> JavaScript >> js导出excel的几种形式
  详细解决方案

js导出excel的几种形式

热度:370   发布时间:2012-10-26 10:30:59.0
js导出excel的几种方式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3<html xmlns="http://www.w3.org/1999/xhtml">
  4<head>
  5    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
  6    <title>WEB页面导出为EXCEL文档的方法</title>
  7</head>
  8<body>
  9<table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0">
10    <tr>
11        <td colspan="5" align="center">WEB页面导出为EXCEL文档的方法</td>
12    </tr>
13    <tr>
14        <td>列标题1</td>
15        <td>列标题2</td>
16        <td>列标题3</td>
17        <td>列标题4</td>
18        <td>列标题5</td>
19    </tr>
20    <tr>
21        <td>aaa</td>
22        <td>bbb</td>
23        <td>ccc</td>
24        <td>ddd</td>
25        <td>eee</td>
26    </tr>
27    <tr>
28        <td>AAA</td>
29        <td>BBB</td>
30        <td>CCC</td>
31        <td>DDD</td>
32        <td>EEE</td>
33    </tr>
34    <tr>
35        <td>FFF</td>
36        <td>GGG</td>
37        <td>HHH</td>
38        <td>III</td>
39        <td>JJJ</td>
40    </tr> 
41</table>
42<input type="button" onclick="javascript:method1('tableExcel');" value="第一种方法导入到EXCEL">
43<input type="button" onclick="javascript:method2('tableExcel');" value="第二种方法导入到EXCEL">
44<input type="button" onclick="javascript:getXlsFromTbl('tableExcel',null);" value="第三种方法导入到EXCEL">
45<SCRIPT LANGUAGE="javascript">
46function method1(tableid) {//整个表格拷贝到EXCEL中
47    var curTbl = document.getElementById(tableid);
48    var oXL = new ActiveXObject("Excel.Application");
49    //创建AX对象excel
50    var oWB = oXL.Workbooks.Add();
51    //获取workbook对象
52        var oSheet = oWB.ActiveSheet;
53    //激活当前sheet
54    var sel = document.body.createTextRange();
55    sel.moveToElementText(curTbl);
56    //把表格中的内容移到TextRange中
57    sel.select();
58    //全选TextRange中内容
59    sel.execCommand("Copy");
60    //复制TextRange中内容 
61    oSheet.Paste();
62    //粘贴到活动的EXCEL中      
63    oXL.Visible = true;
64    //设置excel可见属性
65}
66function method2(tableid) //读取表格中每个单元到EXCEL中
67{
68    var curTbl = document.getElementById(tableid);
69    var oXL = new ActiveXObject("Excel.Application");
70    //创建AX对象excel
71    var oWB = oXL.Workbooks.Add();
72    //获取workbook对象
73    var oSheet = oWB.ActiveSheet;
74    //激活当前sheet
75    var Lenr = curTbl.rows.length;
76    //取得表格行数
77    for (i = 0; i < Lenr; i++)
78    {
79        var Lenc = curTbl.rows(i).cells.length;
80        //取得每行的列数
81        for (j = 0; j < Lenc; j++)
82        {
83            oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText;
84            //赋值
85        }
86    }
87    oXL.Visible = true;
88    //设置excel可见属性
89}
90function getXlsFromTbl(inTblId, inWindow) {
91    try {
92        var allStr = "";
93        var curStr = "";
94        //alert("getXlsFromTbl");
95        if (inTblId != null && inTblId != "" && inTblId != "null") {
96            curStr = getTblData(inTblId, inWindow);
97        }
98        if (curStr != null) {
99            allStr += curStr;
100        }
101        else {
102            alert("你要导出的表不存在!");
103            return;
104        }
105        var fileName = getExcelFileName();
106        doFileExport(fileName, allStr);
107    }
108    catch(e) {
109        alert("导出发生异常:" + e.name + "->" + e.description + "!");
110    }
111}
112function getTblData(inTbl, inWindow) {
113    var rows = 0;
114    //alert("getTblData is " + inWindow);
115    var tblDocument = document;
116    if (!!inWindow && inWindow != "") {
117        if (!document.all(inWindow)) {
118            return null;
119        }
120        else {
121            tblDocument = eval(inWindow).document;
122        }
123    }
124    var curTbl = tblDocument.getElementById(inTbl);
125    var outStr = "";
126    if (curTbl != null) {
127        for (var j = 0; j < curTbl.rows.length; j++) {
128            //alert("j is " + j);
129            for (var i = 0; i < curTbl.rows[j].cells.length; i++) {
130                //alert("i is " + i);
131                if (i == 0 && rows > 0) {
132                    outStr += " \t";
133                    rows -= 1;
134                }
135                outStr += curTbl.rows[j].cells[i].innerText + "\t";
136                if (curTbl.rows[j].cells[i].colSpan > 1) {
137                    for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) {
138                        outStr += " \t";
139                    }
140                }
141                if (i == 0) {
142                    if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) {
143                        rows = curTbl.rows[j].cells[i].rowSpan - 1;
144                    }
145                }
146            }
147            outStr += "\r\n";
148        }
149    }
150    else {
151        outStr = null;
152        alert(inTbl + "不存在!");
153    }
154    return outStr;
155}
156function getExcelFileName() {
157    var d = new Date();
158    var curYear = d.getYear();
159    var curMonth = "" + (d.getMonth() + 1);
160    var curDate = "" + d.getDate();
161    var curHour = "" + d.getHours();
162    var curMinute = "" + d.getMinutes();
163    var curSecond = "" + d.getSeconds();
164    if (curMonth.length == 1) {
165        curMonth = "0" + curMonth;
166    }
167    if (curDate.length == 1) {
168        curDate = "0" + curDate;
169    }
170    if (curHour.length == 1) {
171        curHour = "0" + curHour;
172    }
173    if (curMinute.length == 1) {
174        curMinute = "0" + curMinute;
175    }
176    if (curSecond.length == 1) {
177        curSecond = "0" + curSecond;
178    }
179    var fileName = "leo_zhang" + "_" + curYear + curMonth + curDate + "_"
180            + curHour + curMinute + curSecond + ".csv";
181    //alert(fileName);
182    return fileName;
183}
184function doFileExport(inName, inStr) {
185    var xlsWin = null;
186    if (!!document.all("glbHideFrm")) {
187        xlsWin = glbHideFrm;
188    }
189    else {
190        var width = 6;
191        var height = 4;
192        var openPara = "left=" + (window.screen.width / 2 - width / 2)
193                + ",top=" + (window.screen.height / 2 - height / 2)
194                + ",scrollbars=no,width=" + width + ",height=" + height;
195        xlsWin = window.open("", "_blank", openPara);
196    }
197    xlsWin.document.write(inStr);
198    xlsWin.document.close();
199    xlsWin.document.execCommand('Saveas', true, inName);
200    xlsWin.close();
201}
202</SCRIPT>
203</body>
204</html>
  相关解决方案