浏览器:ie
编程语言:js
使用ActiveXObject,该对象是微软的私有拓展名, 只在微软的IE浏览器上支持。指定打印机进行打印,是先将指定打印机设置成默认打印机,打印完成后,再恢复原来的打印机。

//打印条码
function GetPrintInfo(p1,p2,p3,p4,p5,p6,p7){
    //系统信息获取
    var localtor=new ActiveXObject("WbemScripting.SWbemLocator");
    var service=localtor.ConnectServer(".");
    //取得默认打印机
    var defaultPrint;
    var defPrint=service.ExecQuery("SELECT * FROM Win32_Printer Where Default = TRUE");
    var e=new Enumerator(defPrint);
    for(;!e.atEnd();e.moveNext()){
        var p=e.item();
        defaultPrint=p.Name
    }
    //将barcode设置成默认打印机
    SetDefaultPrint('打印机名称')
    //打印
    PrintInfo(p1,p2,p3,p4,p5,p6,p7);
    //恢复默认

    SetDefaultPrint(defaultPrint);
    //console.log(defaultPrint);
}

//设置默认打印机
function SetDefaultPrint(event)
{
    //系统信息获取
    var localtor=new ActiveXObject("WbemScripting.SWbemLocator");
    var service=localtor.ConnectServer(".");
    //取得默认打印机
    var defaultPrint;
    var defPrint=service.ExecQuery("SELECT * FROM Win32_Printer");
    var e=new Enumerator(defPrint);
    for(;!e.atEnd();e.moveNext()){
        var p=e.item();
        if(p.Name==event)
        {
            p.setDefaultPrinter();
            break;
        }
    }
}

//打印
function PrintInfo(p1,p2,p3,p4,p5,p6,p7)
{
    var Template = "excel模板";
    xlApp = new ActiveXObject("Excel.Application")
    xlBook = xlApp.Workbooks.Add(Template);
    xlsheet = xlBook.ActiveSheet;
    xlsheet.cells(1,1)="*"+p7+"*";
    //xlsheet.cells(1,1)=GetCode128B(p7);
    xlsheet.cells(1,2)=p1;
    xlsheet.cells(2,2)=p2;
    xlsheet.cells(3,2)=p3;
    xlsheet.cells(5,2)=p4;
    xlsheet.cells(6,2)=p5;
    xlsheet.cells(7,2)=p6;
    xlsheet.PrintOut;
    xlBook.Close(savechanges=false);
    xlBook=null;
    xlApp.Quit();
    xlApp=null;
    xlsheet=null;
}


//转换128码
function GetCode128B(inputdata)
{
    var result = "";
    var checksum = 104;
    var arr=new Array();
    
    for(var i=0;i<inputdata.Length;i++)
    {
        arr[i]=inputdata.substr(i,1);
    }
    
    for (var ii = 0; ii < arr.Length; ii++)
    {
        if (arr[ii].charCodeAt() >= 32)
        {
            checksum += (arr[ii].charCodeAt() - 32) * (ii + 1);
        }
        else
        {
            checksum += (arr[ii].charCodeAt() + 64) * (ii + 1);
        }
    }
    checksum = checksum % 103;
    if (checksum < 95)
    {
        checksum += 32;
    }
    else
    {
        checksum += 100;
    }
    result =String.fromCharCode(204) + inputdata.toString() + String.fromCharCode(checksum) +String.fromCharCode(206);
    return result;
}

参考资料

ActiveXObject
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Microsoft_Extensions/ActiveXObject
js指定打印机进行打印
https://www.iteye.com/blog/hy-zhym-1055403