js导出wps或excle 代码
function AutomateWPS(tableid)
{
try
{
var curTbl = document.getElementById(tableid);
var oXL = new ActiveXObject("ET.Application");
if (oXL == null)
{
alert(oXL);
}
//创建AX对象excel
var oWB = oXL.Workbooks.Add();
//获取workbook对象
var oSheet = oWB.ActiveSheet;
//激活当前sheet
var Lenr = curTbl.rows.length;
//取得表格行数
oXL.Range("A1:E1").MergeCells = true; //合并单元格
oSheet.Rows("1:1").Font.Size = 14; //设置第1行的字体大小
oSheet.Rows("1:2").Font.Bold = true; //设置第1,2行为粗体
//oSheet.Cells(1,1).HorizontalAlignment = 3; //单元格居中
oSheet.Columns("A:A").ColumnWidth = 160; //设置单元格宽度
oSheet.Columns("B:C").ColumnWidth = 120;
oSheet.Columns("D:E").ColumnWidth = 90;
for (i = 0; i < Lenr; i++)
{
var Lenc = curTbl.rows(i).cells.length;
//取得每行的列数
for (j = 0; j < Lenc; j++)
{
var cellValue;
cellValue = curTbl.rows(i).cells(j).innerText
if(j==1 || j==2)
{
cellValue = "'"+cellValue; //转换为文本格式
}
oSheet.Cells(i + 1, j + 1).value = cellValue;
}
}
oXL.Visible = true;
//设置excel可见属性
}
catch(e)
{
alert("导出WPS失败,确定是否安装了WPS");
oXL = null;
oWB = null;
oSheet = null;
}
oSheet.Columns.AutoFit;
oXL.UserControl = true; //wps交由用户控制 很重要
}
////////////////////下面是excle的
function exportData(){
var tt=document.all.printtable;
try {
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var Lenr = tt.rows.length;
for (i=0;i<Lenr;i++)
{
var Lenc = tt.rows(i).cells.length;
for (j=0;j<Lenc;j++)
{
oSheet.Cells(i+1,j+1).value = tt.rows(i).cells(j).innerText;
}
}
oXL.Visible = true;
}
catch(e){
alert("您的电脑没有安装Microsoft Excel软件!");
return false;
}
}