文档库 最新最全的文档下载
当前位置:文档库 › 通过Java往word中写入内容

通过Java往word中写入内容

1、向word中写入内容

首先在word中设置书签,如书签名为bookmark,javascript中可以这样写

varword;
word=newActiveXObject("Word.Application");
varrange=word.Range;
word.Visible=true;
varpath="filepath";
word.Documents.Open(path);
range=word.ActiveDocument.Bookmarks("bookmark").Range;
range.InsertBefore("哈哈哈哈哈哈");

庸-人发表于2010-07-06 12:48:38Zmj一起聊
2、把word文件转成html文件

<scriptlanguage=javascript>
functionsaveword(){
varoWordApp=newActiveXObject("Word.Application");
varoDocument=oWordApp.Documents.Open("C:\\doc2html\\x.doc");
oDocument.SaveAs("C:\\test.htm",8)
oWordApp.Quit();
}
</script>
</HEAD>
<BODY>
Clickthe"save"buttontosavethefile"C:\test.doc"to"C:\test.htm":
<inputtype=buttononclick="saveword()"value=save>

</BODY>
</HTML>

庸-人发表于2010-07-06 12:53:09Zmj一起聊
拷贝table1内的内容到word

<scriptlanguage="javascript">
functionOpenWord(){//导出word
Layer1.style.border=0;
ExcelSheet=newActiveXObject('Word.Application');
ExcelSheet.Application.Visible=true;
varmydoc=ExcelSheet.Documents.Add('',0,0);
myRange=mydoc.Range(0,1);
myRange=mydoc.Range(myRange.End-1,myRange.End);//设定起始点
varsel=Layer1.document.body.createTextRange();
sel.moveToElementText(table1);
sel.select();
Layer1.document.execCommand('Copy');
sel.moveEnd('character');
myRange.Paste();

myRange=mydoc.Range(myRange.End-1,myRange.End);
myRange.InsertAfter("\n");
ExcelSheet.ActiveWindow.View.TableGridlines=false;
}
</script>

庸-人发表于2010-07-06 12:57:43Zmj一起聊
操作excel:

<%@pagec%>
<h1>content</h1>
<html>
<head>
<scriptlanguage="javascript"type="text/javascript">
functionMakeExcel(){
vari,j,n;
try{
varxls=newActiveXObject("Excel.Application");
}
catch(e){
alert("要打印该表,您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX控件”,您的浏览器须允许执行控件。请点击了解浏览器设置方法!");
return"";
}
xls.visible=true;//设置excel为可见
varxlBook=xls.Workbooks.Add;
varxlsheet=xlBook.Worksheets(1);
<!--合并-->
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,7)).mergecells=true;
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,7)).value="发卡记录";
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,3)).Interior.ColorIndex=5;//设置底色为蓝色
//xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,6)).Font.ColorIndex=4;//设置字体色
//xlsheet.Rows(1).Interior.ColorIndex=5;//设置底色为蓝色设置背景色Rows(1).Font.ColorIndex=4
<!--设置行高-->
xlsheet.Rows(1).RowHeight=25;
<!--设置字体ws.Range(ws.Cells(i0+1,j0),ws.Cells(i0+1,j1)).Font.Size=13-->
xlsheet.Rows(1).Font.Size=14;
<!--设置字体设置选定区的字体xlsheet.Range(xlsheet.Cells(i0,j0),ws.Cells(i0,j0))https://www.wendangku.net/doc/5b13063390.html,="黑体"-->
xlsheet.Rows(1)https://www.wendangku.net/doc/5b13063390.html,="黑体";
<!--设置列宽xlsheet.Columns(2)=14;-->
xlsheet

.Columns("A:D").ColumnWidth=18;
<!--设置显示字符而不是数字-->
xlsheet.Columns(2).NumberFormatLocal="@";
xlsheet.Columns(7).NumberFormatLocal="@";
//设置单元格内容自动换行range.WrapText=true;
//设置单元格内容水平对齐方式range.HorizontalAlignment=Excel.XlHAlign.xlHAlignCenter;//设置单元格内容竖直堆砌方式
//range.VerticalAlignment=Excel.XlVAlign.xlVAlignCenter
//range.WrapText=true;xlsheet.Rows(3).WrapText=true自动换行
//设置标题栏
xlsheet.Cells(2,1).Value="卡号";
xlsheet.Cells(2,2).Value="密码";
xlsheet.Cells(2,3).Value="计费方式";
xlsheet.Cells(2,4).Value="有效天数";
xlsheet.Cells(2,5).Value="金额";
xlsheet.Cells(2,6).Value="所属服务项目";
xlsheet.Cells(2,7).Value="发卡时间";
varoTable=document.all['fors:data'];
varrowNum=oTable.rows.length;
for(i=2;i<=rowNum;i++){
for(j=1;j<=7;j++){
//htmltable类容写到excel
xlsheet.Cells(i+1,j).Value=oTable.rows(i-1).cells(j-1).innerHTML;
}
}
<!--xlsheet.Range(xls.Cells(i+4,2),xls.Cells(rowNum,4)).Merge;-->
//xlsheet.Range(xlsheet.Cells(i,4),xlsheet.Cells(i-1,6)).BorderAround,4
//for(mn=1,mn<=6;mn++).xlsheet.Range(xlsheet.Cells(1,mn),xlsheet.Cells(i1,j)).Columns.AutoFit;
xlsheet.Columns.AutoFit;
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(rowNum+1,7)).HorizontalAlignment=-4108;//居中
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,7)).VerticalAlignment=-4108;
xlsheet.Range(xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Font.Size=10;
xlsheet.Range(xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(3).Weight=2;//设置左边距
xlsheet.Range(xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(4).Weight=2;//设置右边距
xlsheet.Range(xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(1).Weight=2;//设置顶边距
xlsheet.Range(xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(2).Weight=2;//设置底边距
https://www.wendangku.net/doc/5b13063390.html,erControl=true;//很重要,不能省略,不然会出问题意思是excel交由用户控制
xls=null;
xlBook=null;
xlsheet=null;
}
</script>
<linkhref="css/styles3.css"rel="stylesheet"type="text/css"/>
<title>SOHUProgramm</title>
</head>
<body>
<formid="fors"method="post"action="/WebModule/admins/card/showcard.faces"enctype="application/x-www-form-urlencoded">
<tableid="fors:top"border="0"cellpadding="0"cellspacing="0"width="100%">
<tbody>
<tr>
<tdclass="left"><imgsrc="images/jiao1.gif"alt=""/></td>
<tdclass="topMiddle"></td>
<tdclass="right"><imgsrc="images/jiao2.gif"alt=""/></td>
</tr>
</tbody>
</table>
<tableborder="0"cellpadding="0"cellspacing="0"width="100%">
<tbody>
<tr>
<tdclass="middleLeft"></td>
<tdclass="btstyle">
<tableid="fors:sort"border="0"cellpadding="0"cellspacing="0"style="valign:center"width="100%">
<tbody>
<tr>
<tdclass="btstyle">
<inputtype="button"name="fors:_id7"value="生成excel文件"/>
</td>
</tr>
</tbody>
</table>
<tableid="fors:d

ata"border="1"cellpadding="0"cellspacing="1"width="100%">
<thead>
<tr>
<thscope="col"><spanid="fors:data:headerText1">卡号</span></th>
<thscope="col"><spanid="fors:data:headerText2">密码</span></th>
<thscope="col"><spanid="fors:data:headerText3">计费方式</span></th>
<thscope="col"><spanid="fors:data:headerText4">有效天数</span></th>
<thscope="col">金额</th>
<thscope="col"><spanid="fors:data:headerText6">所属服务项目</span></th>
<thscope="col"><spanid="fors:data:headerText7">发卡时间</span></th>
</tr>
</thead>
<tbody>
<tr>
<td>h000010010</td>
<td>543860</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2010-06-2310:14:40.843</td>
</tr>
<tr>
<td>h000010011</td>
<td>683352</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2010-06-2310:14:40.843</td>
</tr>
<tr>
<td>h000010012</td>
<td>433215</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2010-06-2310:14:40.843</td>
</tr>
<tr>
<td>h000010013</td>
<td>393899</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2010-06-2310:14:40.843</td>
</tr>
<tr>
<td>h000010014</td>
<td>031736</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2010-06-2310:14:40.843</td>
</tr>
<tr>
<td>h000010015</td>
<td>188600</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2010-06-2310:14:40.843</td>
</tr>
<tr>
<td>h000010016</td>
<td>363407</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2010-06-2310:14:40.843</td>
</tr>
<tr>
<td>h000010017</td>
<td>175315</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2010-06-2310:14:40.843</td>
</tr>
<tr>
<td>h000010018</td>
<td>354437</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2010-06-2310:14:40.843</td>
</tr>
<tr>
<td>h000010019</td>
<td>234750</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2010-06-2310:14:40.843</td>
</tr>
</tbody>
</table>
</td>
<tdclass="middleRight"></td>
</tr>
</tbody>
</table>
<tableid="fors:bottom"border="0"cellpadding="0"cellspacing="0"width="100%">
<tbody>
<tr>
<tdclass="left">
<imgsrc="images/jiao3.gif"alt=""/>
</td>
<tdclass="bottomMiddle"></td>
<tdclass="right">
<imgsrc="images/jiao4.gif"alt=""/>
</td>
</tr>
</tbody>
</table>
<inputtype="hidden"name="fors"value="fors"/>
</form>
</body>
</html>

庸-人发表于2010-07-06 12:59:04Zmj一起聊
<%--
//-------------------------

---------1----------------------------------\\
<HTML>
<HEAD>
<TITLE>导出到excel</TITLE>
<METANAME="Generator"C>
<METANAME="Author"C>
<METANAME="Keywords"C>
<METANAME="Description"C>
</HEAD>
<scriptlanguage="javascript">
functionexportExcel(tableid){
if(typeof(EXPORT_OBJECT)!="object"){
document.body.insertAdjacentHTML("afterBegin","<OBJECTstyle='display:none'classid='clsid:0002E510-0000-0000-C000-000000000046'id='EXPORT_OBJECT'></Object>");
}
with(EXPORT_OBJECT){
DataType="HTMLData";
HTMLData=tableid.outerHTML;
try{
ActiveSheet.Export("d:\\表格.xls",0);
alert('成功导出EXCEL表格!');
}
catch(e){
alert('导出EXCEL表格失败,请确定已安装Excel2000(或更高版本),并且没打开同名xls文件');
}
}
}
</script>
<BODY>
<tableid="tableid"border="1">
<tr>
<td>第一个单元格</td>
<td>第二个单元格</td>
<td>第三个单元格</td>
</tr>
<tr>
<td>第四个单元格</td>
<td>第五个单元格</td>
<td>第六个单元格</td>
</tr>
</table>
<inputtype="button"value="导出EXCEL表格">
</BODY>
</HTML>
//----------------------------------2----------------------------------\\
<!--导出到office2000版本的word或excel中-->
<scriptlanguage="javascript">
functiontableToExcel(){
window.clipboardData.setData("Text",document.all('theObjTable').outerHTML);
try{
varExApp=newActiveXObject("Excel.Application");
varExWBk=ExApp.workbooks.add();
varExWSh=ExWBk.worksheets(1);
ExApp.DisplayAlerts=false;
ExApp.visible=true;
}
catch(e){
alert("您的电脑没有安装MicrosoftExcel软件!");
returnfalse;
}
ExWBk.worksheets(1).Paste;
}
functiontableToWord(){
varoWD=newActiveXObject("Word.Application");
varoDC=oWD.Documents.Add("",0,1);
varoRange=oDC.Range(0,1);
varsel=document.body.createTextRange();
sel.moveToElementText(theObjTable);
sel.select();
sel.execCommand("Copy");
oRange.Paste();
oWD.Application.Visible=true;
}
</script>
<tableid="theObjTable">
<tr>
<td>在html页面中</td>
<td>把网页中的表</td>
<td>格内容导入到</td>
<td>word中</td>
<td>也可以导入到excel</td>
</tr>
</table>
<inputtype="button"value="导入到excel">
<inputtype="button"value="导入到word">
//----------------------------------3----------------------------------\\
--%>

庸-人发表于2010-07-06 13:00:13Zmj一起聊
有关用js实现网页中的内容直接转化为excel的方法

<htmlxmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="https://www.wendangku.net/doc/5b13063390.html,/TR/REC-html40">
<head>
<metahttp-equiv=Content-Typec>
<script>
functionout(){
try{
varelTable=document.getElementById("out");
varoRangeRef=document.body.createTextRange();
oRangeRef.moveToElementText(elTable);
oRangeRef.execCommand("Copy");
varoXL=newActiveXObject("Excel.Application")
varoWB=oXL.Workbooks.Add;
varoSheet=oW

B.ActiveSheet;
oSheet.Paste();
oSheet.Cells.NumberFormatLocal="@";
oSheet.Columns("D:D").Select
oXL.Selection.ColumnWidth=20
//oSheet.Columns("A:A").Select
//oSheet.Columns("A").Width=1000;
oXL.Visible=true;
oSheet=null;
oWB=null;
appExcel=null;
}catch(e){alert(e.description)}
}
</script>
</head>
<body>
<tableid='out'>
<tr>
<tdx:strbgcolor="#FF0000">00001</td>
<tdx:str>0002</td>
</tr>
<tr>
<tdheight=18align=rightstyle='height:13.5pt'x:str>00001</td>
<tdalign=rightx:str>000002</td>
</tr>
<tr>
<tdx:str>0003</td>
<tdx:str>00003</td>
</tr>
</table>
<INPUTtype="button"value="Button"id=button1name=button1>
</body>
</html>

相关文档
相关文档 最新文档