javascript將網頁表格匯出Word
<script>
function AllAreaWord()
{
if(document.all("tab").rows.length==0)
{
alert("沒有內容可導!");
return;
}
try{
var oWD = new ActiveXObject("Word.Application");
}catch(e)
{
alert("無法調用Office對象,請確保您的機器已安裝了Office並已將本系統的網站名加入到IE的信任網站列表中!");
return;
}
var oDC = oWD.Documents.Add("",0,1);
var oRange =oDC.Range(0,1);
var sel = document.body.createTextRange();
sel.moveToElementText(tab); //tab 為匯出資料所在的表格ID
sel.select();
sel.execCommand("Copy");
oRange.Paste();
oWD.Application.Visible = true;
}
</script>
<table id="tab" align="center" width="300" border="1" bordercolor="#2baeff" cellpadding="0" cellspacing="0" bgcolor="lightblue">
<Tr>
<Td>1</td>
<Td>2</td>
<Td>3</td>
</tr>
<Tr>
<Td>4</td>
<Td>5</td>
<Td>6</td>
</tr>
<Tr>
<Td>7</td>
<Td>8</td>
<Td>9</td>
</tr>
</table>
<input type=button name='button_export' title='匯出到word' onclick=AllAreaWord() value=下載到word>
javascript將網頁表格匯出Excel
<SCRIPT LANGUAGE="javascript">
<!--
function AutomateExcel()
{
var elTable = document.getElementById("outtable"); //outtable 為匯出資料所在的表格ID;
var oRangeRef = document.body.createTextRange();
oRangeRef.moveToElementText( elTable );
oRangeRef.execCommand( "Copy" );
try{
var appExcel = new ActiveXObject( "Excel.Application" );
}catch(e)
{
alert("無法調用Office對象,請確保您的機器已安裝了Office並已將本系統的網站名加入到IE的信任網站列表中!");
return;
}
appExcel.Visible = true;
appExcel.Workbooks.Add().Worksheets.Item(1).Paste();
appExcel = null;
}
//-->
</SCRIPT>
<input type="button" name="out_excel" onClick="AutomateExcel();" value="匯出到excel" class="notPrint">
<table border="1" cellpadding="0" cellspacing="0" id=outtable>
<tr height="28">
<td width="27" height="86" rowspan="4" bgcolor="#ffffcc">序號</td>
<td width="111" rowspan="4" bgcolor="#ffffcc"><div align="center">服務網點</div></td>
<td width="402" colspan="7" bgcolor="#ffffcc"><div align="center">使用者滿意度</div></td>
</tr>
<tr height="19">
<td width="100" height="39" colspan="3" rowspan="2" bgcolor="#ffffcc"><div align="center">樣本數量</div></td>
<td width="218" colspan="3" rowspan="2" bgcolor="#ffffcc">對該服務網點實施本次活動的整體評價,滿分10分(平均分數)</td>
<td width="84" rowspan="3" bgcolor="#ffffcc"><div align="center">季度平均分</div></td>
</tr>
<tr height="20"> </tr>
<tr height="19">
<td width="29" height="19" bgcolor="#ffffcc"><div align="center">4月</div></td>
<td width="29" bgcolor="#ffffcc"><div align="center">5月</div></td>
<td width="42" bgcolor="#ffffcc"><div align="center">合計</div></td>
<td width="68" bgcolor="#ffffcc"><div align="center">4月</div></td>
<td width="68" bgcolor="#ffffcc"><div align="center">5月</div></td>
<td width="82" bgcolor="#ffffcc"><div align="center">合計</div></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td bgcolor="#ffffcc"> </td>
<td> </td>
<td> </td>
<td bgcolor="#ffffcc"> </td>
<td bgcolor="#ffffcc"> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td bgcolor="#ffffcc"> </td>
<td> </td>
<td> </td>
<td bgcolor="#ffffcc"> </td>
<td bgcolor="#ffffcc"> </td>
</tr>
</table>
在匯出時彈出如下對話方塊的解決辦法:
1、首先將網站的地址加入受信任的網站,如:
2、在"工具-internet選項-安全-受信任的網站"中點“自訂層級”,在“對沒有標註安全的Acitvex控制項進行初始化和指令碼運行”中點選“禁止”或“啟用”,就ok了。
如何取office版本號碼、安裝路徑和使用者
alert("您的" + wrd.Application.Caption + "安裝路徑為:/n" + wrd.Application.Path + "/n版本號碼是:" + wrd.Application.version
+ "/n註冊使//用者是:" + wrd.Application.UserName)
由於項目中又提出了新需求,需要匯入Wodr時文檔裡能自動一個表格佔一頁,以方便列印,於是又做了修改,支援了自動分頁。並且發現了一個老是彈出文法拼字檢查錯誤太多的Bug,進行了修複。Excel增加了一個手動設定列的寬度。二次修正並添加了新功能的代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
<script language="javascript">
function AutomateWordAutoPaging(prefixion,Count)
{
var oWD = new ActiveXObject("Word.Application");
var oDC = oWD.Documents.Add("",0,1);
oDC.ShowGrammaticalErrors = false; //屏蔽語法檢查
oDC.ShowSpellingErrors = false; //屏蔽語法檢查
var oRange =oDC.Range(0,1);
for (i=0;i<Count;i++)
{
var sel = document.body.createTextRange();
var TableName = prefixion+i
var Table = document.getElementById(TableName)
sel.moveToElementText(Table);
sel.select();
sel.execCommand("Copy");
oWD.Selection.Paste();
//oWD.Selection.TypeText("aaa")
oWD.Selection.InsertBreak(7) //插入分頁符
}
oWD.ActiveDocument.ActiveWindow.View.Type=3 //設定瀏覽模式
oWD.Application.Visible = true;
}
function AutomateExcel(prefixion)
{
var elTable = document.getElementById("AutomateExcel");
var oRangeRef = document.body.createTextRange();
oRangeRef.moveToElementText(elTable);
oRangeRef.execCommand("Copy");
try{
var appExcel = new ActiveXObject( "Excel.Application" );
}catch(e)
{
alert("無法調用Office對象,請確保您的機器已安裝了Office並已將本系統的網站名加入到IE的信任網站列表中!");
return;
}
appExcel.Visible = true;
appExcel.Workbooks.Add().Worksheets.Item(1).Paste();
appExcel.Workbooks(1).Worksheets.Item(1).Columns("A:A").ColumnWidth = 100;
//appExcel.Workbooks(1).Worksheets.Item(1).Columns("B:B").ColumnWidth = 21;
appExcel = null;
}
</script>
</head>
<body>
<input name="匯入" type="button" id="匯入" value="匯出到Word自動分頁" onclick="AutomateWordAutoPaging('Table',5)" />
<input name="button" type="button" class="INPUT-BUTTON-Excel" onclick="AutomateExcel('Table')" value="匯出到Excel控制列寬" />
<div id="AutomateExcel">
<TABLE class=tabp id="Table0" cellSpacing=0 cellPadding=2 width="100%"
align=center border=1>
<TR>
<TD width="100%" align="center">標題0</TD>
</TR>
<TR>
<TD align="center">內容0</TD>
</TR>
</TABLE>
<BR>
<TABLE class=tabp id="Table1" cellSpacing=0 cellPadding=2 width="100%"
align=center border=1>
<TR>
<TD width="100%" align="center">標題1</TD>
</TR>
<TR>
<TD align="center">內容1</TD>
</TR>
</TABLE>
<BR>
<TABLE class=tabp id="Table2" cellSpacing=0 cellPadding=2 width="100%"
align=center border=1>
<TR>
<TD width="100%" align="center">標題2</TD>
</TR>
<TR>
<TD align="center">內容2</TD>
</TR>
</TABLE>
<BR>
<TABLE class=tabp id="Table3" cellSpacing=0 cellPadding=2 width="100%"
align=center border=1>
<TR>
<TD width="100%" align="center">標題3</TD>
</TR>
<TR>
<TD align="center">內容3</TD>
</TR>
</TABLE>
<BR>
<TABLE class=tabp id="Table4" cellSpacing=0 cellPadding=2 width="100%"
align=center border=1>
<TR>
<TD width="100%" align="center">標題4</TD>
</TR>
<TR>
<TD align="center">內容4</TD>
</TR>
</TABLE>
<BR>
</div>
</body>
</html>