向Word添加指定表格的问题
现有一项目,需要向Word中添加表格、文本等内容
项目架构为B/S,在线编辑Word采用重庆软航的Office控件
向文件添加指定文本已实现
function addInput()
{
var inputText = document.getElementById("inputText").value;OFFICE_CONTROL_OBJ.ActiveDocument.Application.Selection.TypeText(inputText);
}
function addTable()
{
var tableHtml= document.getElementById("div_test").innerHTML;
var tableValue = document.getElementById("div_test").innerText;
alert(tableHtml);
alert(tableValue);
OFFICE_CONTROL_OBJ.ActiveDocument.Application.Selection.TypeText(tableHtml);
OFFICE_CONTROL_OBJ.ActiveDocument.Application.Selection.Tables.Add(tableHtml);
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv=X-UA-Compatible content=IE=EmulateIE7>
<title>Office测试</title>
<script language="JavaScript" src="OfficeContorlFunctions.js"></script>
<script language="JavaScript">
function addChars1()
{
OFFICE_CONTROL_OBJ.ActiveDocument.Application.Selection.TypeText('acndefmdfasd');
}
function addChars2()
{
OFFICE_CONTROL_OBJ.ActiveDocument.Application.Selection.TypeText('猜测大是大非');
}
function addTable()
{
var tableHtml= document.getElementById("div_test").innerHTML;
var tableValue = document.getElementById("div_test").innerText;
alert(tableHtml);
alert(tableValue);
OFFICE_CONTROL_OBJ.ActiveDocument.Application.Selection.TypeText(tableHtml);
OFFICE_CONTROL_OBJ.ActiveDocument.Application.Selection.Tables.Add(tableHtml);
}
function addInput()
{
var inputText = document.getElementById("inputText").value;
OFFICE_CONTROL_OBJ.ActiveDocument.Application.Selection.TypeText(inputText);
}
</script>
</head>
<body onload='creatNewFile()'>
<script language="JavaScript" src="getOfficeControl.js"></script>
<input type="button" value="添加几个字母" onclick="addChars1();" ></input>
<input type="button" value="添加几个汉字" onclick="addChars2();" ></input>
<br/>
<textarea id="inputText" cols="45" rows="4"></textarea>
<input type="button" value="添加输入值" onclick="addInput();" ></input>
<br/>
<input type="button" value="添加表格" onclick="addTable();" ></input><br/>
<div id="div_test">
<TABLE style="BORDER-COLLAPSE: collapse">
<TBODY>
<TR>
<TH style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid">物料名称 </TH>
<TH style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid"> 规格型号 </TH>
<TH style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid"> 材质 </TH>
<TH style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid">单位 </TH>
<TH style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid"> 合同单价 </TH>
<TH style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid"> 合同数量 </TH>
<TH style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid"> 合同金额 </TH>
</TR>
<TR>
<TD style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid">钢板</TD>
<TD style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid">δ=10 图号6568.702-3</TD>
<TD style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid">1Cr20Ni14Si2</TD>
<TD style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid">公斤</TD>
<TD style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid">
<DIV style="TEXT-ALIGN: right">20.00 </DIV>
</TD>
<TD style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid">
<DIV style="TEXT-ALIGN: right">122.64 </DIV>
</TD>
<TD style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid">
<DIV style="TEXT-ALIGN: right">2452.80 </DIV>
</TD>
</TR>
<TR>
<TD style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid">钢板</TD>
<TD style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid">δ=10 图号6568.702-5</TD>
<TD style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid">1Cr20Ni14Si2</TD>
<TD style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid">公斤</TD>
<TD style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid">
<DIV style="TEXT-ALIGN: right">30.00 </DIV>
</TD>
<TD style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid">
<DIV style="TEXT-ALIGN: right">231.84 </DIV>
</TD>
<TD style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid">
<DIV style="TEXT-ALIGN: right">6955.20 </DIV>
</TD>
</TR>
</TBODY>
</TABLE>
</div>
</body>
</html>
function addTable()
{
var sel = document.body.createTextRange();
sel.moveToElementText(div_test);
sel.select();
sel.execCommand("Copy");
OFFICE_CONTROL_OBJ.ActiveDocument.Application.Selection.Paste();
}
<script language="javascript">
function tableToWord() {
try
{
var oWD = new ActiveXObject("Word.Application");
var oDC = oWD.Documents.Add("",0,1);
var oRange =oDC.Range(0,1);
var sel = document.body.createTextRange();
sel.moveToElementText(theObjTable);
sel.select();
sel.execCommand("Copy");
oRange.Paste();
oWD.Application.Visible = true;
}
catch(e)
{
alert("您的电脑没有安装Microsoft Word软件!")
return false
}
}
function tableToExcel() {
window.clipboardData.setData("Text",document.all('theObjTable').outerHTML);
try
{
var ExApp = new ActiveXObject("Excel.Application")
var ExWBk = ExApp.workbooks.add()
var ExWSh = ExWBk.worksheets(1)
ExApp.DisplayAlerts = false
ExApp.visible = true
}
catch(e)
{
alert("您的电脑没有安装Microsoft Excel软件!")
return false
}
ExWBk.worksheets(1).Paste;
}
</script>
<table id=theObjTable>
<tr>
<td>a</td>
<td>b</td>
</tr>
</table>
<input type="button" value="导入到excel" onclick="tableToExcel()">
<input type="button" value="导入到word" onclick="tableToWord()">