(转)WEB页面导出为Word文档后分页&横向打印的方法

 

<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);

                    
//横向打印设置部分
                    oDC.PageSetup.LineNumbering.Active = false;
                    oDC.PageSetup.Orientation. wdOrientLandscape 
=  1//设置为横向打印
                    oDC.PageSetup.TopMargin =  oDC.Application.CentimetersToPoints(3.17);
                    oDC.PageSetup.BottomMargin 
=  oDC.Application.CentimetersToPoints(3.17);
                    oDC.PageSetup.LeftMargin 
= oDC.Application.CentimetersToPoints(2.54);
                    oDC.PageSetup.RightMargin 
= oDC.Application.CentimetersToPoints(2.54);
                    oDC.PageSetup.Gutter 
= oDC.Application.CentimetersToPoints(0);
                    oDC.PageSetup.HeaderDistance 
= oDC.Application.CentimetersToPoints(1.5);
                    oDC.PageSetup.FooterDistance 
= oDC.Application.CentimetersToPoints(1.75);
                    oDC.PageSetup.PageWidth 
= oDC.Application.CentimetersToPoints(29.7);
                    oDC.PageSetup.PageHeight 
= oDC.Application.CentimetersToPoints(21);
                    oDC.PageSetup.FirstPageTray 
=  0;
                    oDC.PageSetup.OtherPagesTray 
= 0;
                    oDC.PageSetup.SectionStart 
=  2;
                    oDC.PageSetup.OddAndEvenPagesHeaderFooter 
= false;
                    oDC.PageSetup.DifferentFirstPageHeaderFooter 
= false;
                    oDC.PageSetup.VerticalAlignment 
= 0;
                    oDC.PageSetup.SuppressEndnotes 
= false;
                    oDC.PageSetup.MirrorMargins 
= false;
                    oDC.PageSetup.TwoPagesOnOne 
= false;
                    oDC.PageSetup.BookFoldPrinting 
= false;
                    oDC.PageSetup.BookFoldRevPrinting 
= false;
                    oDC.PageSetup.BookFoldPrintingSheets 
= 1
                    oDC.PageSetup.GutterPos 
= 0;
                    oDC.PageSetup.LayoutMode 
= 2;

                    
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);
                    }

                    
                    
//保存word文档为test.doc
                    oDC.SaveAs("test.doc");
                }

    
</SCRIPT>
    
<body>
        
<BR>
        
<div id="test">
            aaa
            ^
            bbb
            ^
            ccc
        
</div>
        
<input type="button" onclick="javascript:AllAreaWord(test,'^');" value="导出页面指定区域内容到Word"/>
    
</body>
</html>

posted @ 2019-11-28 14:11  magel  阅读(626)  评论(0编辑  收藏  举报