CONAN ZONE

你越挣扎我就越兴奋

BlogJava 首页 新随笔 联系 聚合 管理
  0 Posts :: 282 Stories :: 0 Comments :: 0 Trackbacks
需求是这样的,导出html指定元素内容为word,并按指定关键字分页.
网上找了半天只有保存为word的方法.在csdn上向专家提了问,没人回啊555.
蹒跚学步开始了.
对于分页,自己用vba录制了
命令,然后用JavaScript调试出来的.

直接看代码:
<html>
    
<HEAD>
        
<title>WEB页面导出为Word文档后分页的方法 </title>
    
</HEAD>
    
<SCRIPT LANGUAGE="javascript">
                
/*
                 * 
                 * @param {Object} cont  要导出的html元素内容的id,注意不要加双引号
                 * @param {Object} key   分页关键字
                 
*/

                
function AllAreaWord(cont,key)
                
{
                    
var oWD = new ActiveXObject("Word.Application");
                    
//默认为页面视图
                    var oDC = oWD.Documents.Add(""00);
                    
var oRange = oDC.Range(01);
                    
//var oRange1 = oDC.Range(0,2);
                    var sel = document.body.createTextRange();
                    
//参数为html元素id 
                    sel.moveToElementText(cont);
                    sel.select();
                    sel.execCommand(
"Copy");
                    oRange.Paste();
                    oWD.Application.Visible 
= true;
                    
//得到打开后word的selection对象
                    var selection = oWD.Selection;
                    
//设置字体大小
                    selection.Font.Size = 10;
                    
//ctrl+A 全选操作
                    selection.WholeStory();
                    
//清除格式
                    selection.Find.ClearFormatting();
                    
//指定查找关键字
                    selection.Find.Text = key;
                    
//^m为手动分页符标记
                    //向下查找
                    selection.Find.Forward = true;
                    selection.Find.Wrap 
= 1;
                    
//不区分大小写
                    selection.Find.MatchCase = false;
                    
//不匹配整个单词
                    selection.Find.MatchWholeWord = false;
                    
//如果找到指定字符串返回真,否则返回false
                    while (selection.Find.Execute()) 
                    
{
                        
//插入分页符,分页符常量为7,具体可查word api
                        selection.InsertBreak(7);
                    }

                }

    
</SCRIPT>
    
<body>
        
<BR>
        
<div id="test">
            aaa
            
^
            bbb
            
^
            ccc
        
</div>
        
<input type="button" onclick="javascript:AllAreaWord(test,'^');" value="导出页面指定区域内容到Word"/>
    
</body>
</html>
posted on 2008-07-09 19:07 CONAN 阅读(302) 评论(0)  编辑  收藏 所属分类: JS