一、 直接拷贝整个表格到EXCEL中二、 通过遍历表格,给EXCEL中相应的单元格赋值。三、 把表格中的内容提取出来,利用IE的另存为csv的格式。各方法的好处:1 直接拷贝表格,能够保留表格中的原有的格式,比如,列,行的合并,对齐方式,底色等等,2 通过遍历表格,比较灵活,可以遍历表格某些需要部分的内容。3 利用IE的另存为,不用创建ActiveXObject对象,可以处理表格合并方面的问题。各方法的缺点:1 可能d出脚本错误:Automation不能创建对象。解决方法:启用IE安全设置中的:对没有标记为安全的ActiveX控件进行初始化和脚本运行。由于整个表格复制到EXCEL中,给表格加个标题,并加入到EXCEL中可能会遇到麻烦。解决方法:首先在表格中加入第一行<tr><td colspan="x" align="center">&</td></tr>X,表示整个表格的列数,复制完整个表格后,加如下代码,oSheet为当前活动的sheetoSheetRange(oSheetCells(1, 1), oSheetCells(1, x))value = "表格标题";//设置标题oSheetRows(1)FontSize = 16; //设置文字大小oSheetRows(1)FontName = "宋体";//设置文字字体注:以下属性我没用着,可能有用,也可能会报错oSheetRange(oSheetCells(1,1), oSheetCells(1,14))mergecells=true; //合并单元格oSheetRange(oSheetCells(1,1), oSheetCells(1,14))InteriorColorIndex=6;//设置底色oSheetRange(oSheetCells(1,1), oSheetCells(1,14))FontColorIndex=5;//设置字体色oSheetRows(1)RowHeight=20; //设置列高oSheetCells(iRow,iCol)Halignment=’2’//设置字体居中2 可能d出脚本错误:Automation不能创建对象(解决方法如上)。表格内容写入到EXCEL中无表格线(未解决)且有单元格合并时会有问题,解决方法:合并单元格后再写数据。oSheetRange(oSheetCells(1,1), oSheetCells(1,14))mergecells=true; //合并单元格3 表格内容写入到EXCEL中无表格线(未解决)表格格式复杂时,会有问题,(rowspan>1 or colspan>1),解决方法:一般都是表头格式比较复杂,可先把表头写死,然后再循环写其他数据。代码如下:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 40 Transitional//EN"><HTML><HEAD><TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD> <BODY>
<table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0"><tr><td colspan="5" align="center">WEB页面导出为EXCEL文档的方法</td></tr><tr><td>列标题1</td><td>列标题2</td><td>列标题3</td><td>列标题4</td><td>列标题5</td></tr><tr><td>aaa</td><td>bbb</td><td>ccc</td><td>ddd</td><td>eee</td></tr><tr><td>AAA</td><td>BBB</td><td>CCC</td><td>DDD</td><td>EEE</td></tr><tr><td>FFF</td><td>GGG</td><td>HHH</td><td>III</td><td>JJJ</td></tr></table>
<input type="button" onclick="javascript:method1('tableExcel');" value="第一种方法导入到EXCEL"><input type="button" onclick="javascript:method2('tableExcel');" value="第二种方法导入到EXCEL"><input type="button" onclick="javascript:getXlsFromTbl('tableExcel',null);" value="第三种方法导入到EXCEL">
<SCRIPT LANGUAGE="javascript">function method1(tableid)
{//整个表格拷贝到EXCEL中var curTbl = documentgetElementById(tableid);var oXL = new ActiveXObject("ExcelApplication");//创建AX对象excelvar oWB = oXLWorkbooksAdd();//获取workbook对象var oSheet = oWBActiveSheet;//激活当前sheetvar sel = documentbodycreateTextRange();selmoveToElementText(curTbl);//把表格中的内容移到TextRange中selselect();//全选TextRange中内容selexecCommand("Copy");//复制TextRange中内容oSheetPaste();//粘贴到活动的EXCEL中oXLVisible = true;//设置excel可见属性}function method2(tableid) //读取表格中每个单元到EXCEL中
{var curTbl = documentgetElementById(tableid);var oXL = new ActiveXObject("ExcelApplication");//创建AX对象excelvar oWB = oXLWorkbooksAdd();//获取workbook对象var oSheet = oWBActiveSheet;//激活当前sheetvar Lenr = curTblrowslength;//取得表格行数for (i = 0; i < Lenr; i++){var Lenc = curTblrows(i)cellslength;//取得每行的列数for (j = 0; j < Lenc; j++){oSheetCells(i + 1, j + 1)value = curTblrows(i)cells(j)innerText;//赋值}}oXLVisible = true;//设置excel可见属性}function getXlsFromTbl(inTblId, inWindow) {try {var allStr = "";var curStr = "";//alert("getXlsFromTbl");if (inTblId != null && inTblId != "" && inTblId != "null") {curStr = getTblData(inTblId, inWindow);}if (curStr != null) {allStr += curStr;}else {alert("你要导出的表不存在!");return;}var fileName = getExcelFileName();doFileExport(fileName, allStr);}catch(e) {alert("导出发生异常:" + ename + "->" + edescription + "!");}}
//---------------------------------------------
function getTblData(inTbl, inWindow) {var rows = 0;//alert("getTblData is " + inWindow);var tblDocument = document;if (!!inWindow && inWindow != "") {if (!documentall(inWindow)) {return null;}else {tblDocument = eval(inWindow)document;}}var curTbl = tblDocumentgetElementById(inTbl);var outStr = "";if (curTbl != null) {for (var j = 0; j < curTblrowslength; j++) {//alert("j is " + j);for (var i = 0; i < curTblrows[j]cellslength; i++) {//alert("i is " + i);if (i == 0 && rows > 0) {outStr += " ";rows -= 1;}outStr += curTblrows[j]cells[i]innerText + " ";if (curTblrows[j]cells[i]colSpan > 1) {for (var k = 0; k < curTblrows[j]cells[i]colSpan - 1; k++) {outStr += " ";}}if (i == 0) {if (rows == 0 && curTblrows[j]cells[i]rowSpan > 1) {rows = curTblrows[j]cells[i]rowSpan - 1;}}}outStr += " ";}}else {outStr = null;alert(inTbl + "不存在!");}return outStr;}function getExcelFileName() {var d = new Date();var curYear = dgetYear();var curMonth = "" + (dgetMonth() + 1);var curDate = "" + dgetDate();var curHour = "" + dgetHours();var curMinute = "" + dgetMinutes();var curSecond = "" + dgetSeconds();if (curMonthlength == 1) {curMonth = "0" + curMonth;}if (curDatelength == 1) {curDate = "0" + curDate;}if (curHourlength == 1) {curHour = "0" + curHour;}if (curMinutelength == 1) {curMinute = "0" + curMinute;}if (curSecondlength == 1) {curSecond = "0" + curSecond;}var fileName = "leo_zhang" + "_" + curYear + curMonth + curDate + "_"+ curHour + curMinute + curSecond + "csv";//alert(fileName);return fileName;}function doFileExport(inName, inStr) {var xlsWin = null;if (!!documentall("glbHideFrm")) {xlsWin = glbHideFrm;}else {var width = 6;var height = 4;var openPara = "left=" + (windowscreenwidth / 2 - width / 2)+ ",top=" + (windowscreenheight / 2 - height / 2)+ ",scrollbars=no,width=" + width + ",height=" + height;xlsWin = windowopen("", "_blank", openPara);}xlsWindocumentwrite(inStr);xlsWindocumentclose();xlsWindocumentexecCommand('Saveas', true, inName);xlsWinclose();}
1、JS获取表格的简便方法:获取tbody:tBodies 获取thead:tHead 获取tfoot:tFoot 获取行tr:rows 获取列td:cells
使用实例: oTabletBodies[0] oTabletHead[0] oTabletFoot[0] oTablerows[1] oTablecells[1]
2、遍历
var oTable=documentgetElementById("表格id");
oTabletBodies[0]可以看成是rows和cells组成的二维数组,用两个for循环嵌套遍历一下就可以了
以上就是关于Javascript实现把网页中table的内容导出到excel中的几种方法全部的内容,包括:Javascript实现把网页中table的内容导出到excel中的几种方法、如何抓取通过网页的js方法查询出动态的表格数据、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)