zoukankan      html  css  js  c++  java
  • ActiveXObject Word.Application 打印小票

     javascript 时间格式

     1  Date.prototype.format = function (format) {
     2             var o = {
     3                 "M+": this.getMonth() + 1, //month
     4                 "d+": this.getDate(), //day
     5                 "h+": this.getHours(), //hour
     6                 "m+": this.getMinutes(), //minute
     7                 "s+": this.getSeconds(), //second
     8                 "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
     9                 "S": this.getMilliseconds() //millisecond
    10             }
    11             if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    12             (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    13             for (var k in o) if (new RegExp("(" + k + ")").test(format))
    14                 format = format.replace(RegExp.$1,
    15                 RegExp.$1.length == 1 ? o[k] :
    16                 ("00" + o[k]).substr(("" + o[k]).length));
    17             return format;
    18         }
    View Code

     javascript 调用word 进行打印

     function viewToOrder() {
                try {
                    //获取Word 过程
                    //请设置IE的可信任站点
                    wdapp = new ActiveXObject("Word.Application");
                }
                catch (e) {
                    alert("无法调用Office对象,请确保您的机器已安装了Office并已将本系统的站点名加入到IE的信任站点列表中!");
                    wdapp = null;
                    return;
                }
    
                try {
                    wdapp.Documents.Open("f:\2.doc"); //打开本地(客户端)word模板
                    wddoc = wdapp.ActiveDocument;
    
                    //找到Word中的對應書籤,替换其内容
                    wddoc.Bookmarks("OrderDate").Range.Text = new Date().format("yyyy-MM-dd hh:mm:ss");
                    wddoc.Bookmarks("Address").Range.Text = "珠海拱北跨境工業區鴻豐大廈B409";
                    wddoc.Bookmarks("Checkout").Range.Text = "$ 142.90";
                    contentRange = wddoc.Bookmarks("List").Range;
    
                    var objTable = wddoc.Tables.Add(contentRange, 3, 2)     //插入5行3列表格
                    //設置表格無邊框
                    objTable.Borders.InsideLineStyle = 0;
                    objTable.Borders.OutsideLineStyle = 0;
    
                    //设置列宽
                    objTable.Columns(1).Width = 200;
                    objTable.Columns(2).Width = 60;
    
    
                    //寫入表格內容
                    //for (i = 0; i < 5; i++) {
                    //    for (j = 0; j < 3; j++) {
                    //        objTable.Cell(i + 1, j + 1).Range.Text = i + "" + j;
                    //    }
                    //}
                    objTable.Cell(1, 1).Range.Text = "日本裝殺牠死煙霧殺蟲劑10G";
                    objTable.Cell(1, 2).Range.Text = "67.00*1";
    
    
                    objTable.Cell(2, 1).Range.Text = "8月特价溫氏好味熟雞-鹽焗味";
                    objTable.Cell(2, 2).Range.Text = "48.00*1";
    
    
                    objTable.Cell(3, 1).Range.Text = "立頓絕品醇奶茶台式凍烏龍 10X19G";
                    objTable.Cell(3, 2).Range.Text = "27.90*1";
    
    
                    wdapp.visible = false; //word模板是否可见
                    wddoc.saveAs("f:\PrinterTemp2.doc"); //保存临时文件word
                    //wdapp.Application.Printout(); //调用自动打印功能
    
                    wdapp.quit();
                    wdapp = null;
                    alert("開始打印");
                } catch (e) {
                    alert(e.message);
                }
            }
    View Code

    Word 模板 2.doc

    澳門便利店
    送貨地點:<书签 Address>地點</书签>
    下單時間:<书签 OrderDate>時間<<书签>
    **********************************************
    訂單詳細:
    <书签 List>詳細</书签>
    **********************************************
    合計:<书签 Checkout>合計<书签>
    View Code

    缺陷:该web打印小票解决方案必须服务器装office

  • 相关阅读:
    POJ1609 UVALive2815 UVA1196 ZOJ1787 Tiling Up Blocks【二维最长上升子序列+DP】
    UVALive3638 UVA12100 POJ3125 HDU1972 Printer Queue【队列+模拟】
    UVA10391 ZOJ1825 Compound Words【SET+暴力】
    NUC1157 To the Max【最大子段和+DP】
    NUC1399 Sum It Up【DFS】
    NUC1742 Subsequence【前缀和+二分搜索+尺取法】
    NUC1371 Who's in the Middle【中位数+排序】
    NUC1312 Sum【水题+数学题】
    POJ2100 Graveyard Design【尺取法】
    UVALive3399 UVA1210 POJ2739 Sum of Consecutive Prime Numbers【素数筛选+尺取法】
  • 原文地址:https://www.cnblogs.com/xiaobuild/p/4775581.html
Copyright © 2011-2022 走看看