一、把jsp頁面數據復制到word文檔
var table=document.getElementById(printTable);
row=table.rows.length;
column=table.rows(1).cells.length;
var word = new ActiveXObject("Word.Application");
word.Application.Visible = true;
word.Selection.Text ="<%=reportTitle%>";
var mydoc=word.Documents.Add(,0,0);
myRange =mydoc.Range(0,1);
var sel=document.body.createTextRange();
sel.moveToElementText(table);
sel.select();
sel.execCommand(Copy);
myRange.Paste();
二、把jsp頁面數據寫入word文檔
var table=document.getElementById(printTable);
row=table.rows.length;
column=table.rows(1).cells.length;
var wdapp=new ActiveXObject("Word.Application");
wdapp.visible=true;
wddoc=wdapp.Documents.Add(); //添加新的文檔
thearray=new Array();
//將頁面中表格的內容存放在數組中
for(i=0;i<row;i ){
thearray[i]=new Array();
for(j=0;j<column;j ){
thearray[i][j]=table.rows(i).cells(j).innerHTML;
}
}
var range = wddoc.Range(0,0);
range.Text="<%=reportTitle%>" " ";
wdapp.Application.Activedocument.Paragraphs.Add(range);
wdapp.Application.Activedocument.Paragraphs.Add();
rngcurrent=wdapp.Application.Activedocument.Paragraphs(3).Range;
var objTable=wddoc.Tables.Add(rngcurrent,row,column) //插入表格
for(i=0;i<row;i ){
for(j=0;j<column;j ){
objTable.Cell(i 1,j 1).Range.Text = thearray[i][j].replace(" ","");
}
}