1、如果Table中有Input(text)之类的元素,需要先修改Input的outerHTML;

具体例子:

function resetInput()
{
var controls = document.getElementsByTagName('input');
for(var i=0; i<controls.length; i++){
if(controls[i].type=='text')
{
if(controls[i].value =="")
{
controls[i].outerHTML=" ";
}
else
{
controls[i].outerHTML=controls[i].value;
}
}
}
}

方法一:先复制到剪贴板,打开excel文件再粘贴;

window.clipboardData.setData('text',document.getElementById('table1').outerHTML);

方法二:直接创建一个excel或者word文件,将数据写入创建的文件中;

//导出到excel
function AutomateExcel(tableid){
var elTable = document.getElementById(tableid); //要导出的table id。
var oRangeRef = document.body.createTextRange();
oRangeRef.moveToElementText(elTable);
oRangeRef.execCommand("Copy");
var appExcel = new ActiveXObject("Excel.Application");
appExcel.Workbooks.Add().Worksheets.Item(1).Paste();
appExcel.Visible = true;
appExcel = null;
}
//导出到word
//指定页面区域内容导入Word
function AllAreaWord(tableid)
{
var elTable = document.getElementById(tableid);
var sel = document.body.createTextRange();
sel.moveToElementText(elTable);
sel.execCommand("Copy");
var oWD = new ActiveXObject("Word.Application");
var oDC = oWD.Documents.Add("",0,1);
var orange =oDC.Range(0,1);
//sel.select();
orange.Paste();
oWD.Application.Visible = true;
oWD = null;
}
调用方法:
<input name="word" type="button" value="导出到word"  />&nbsp;<input name="excel" type="button" value="导出到excel" οnclick="AutomateExcel('tableid');"/>