Asp.Net调用打印机

我们先引用Microsoft.Office.Interop.Word,

 

 

object fileNames = HttpContext.Current.Request.MapPath("..") + @"\images\ApplyReport1.dot";
            object saveFileName = HttpContext.Current.Request.MapPath("..") + @"\images\ApplyReport1.doc";
            Word.ApplicationClass oWordApp = new Word.ApplicationClass();          
            object readOnly = false;
            object isVisible = true;
            object missing = System.Reflection.Missing.Value;
            object oTemplate = "D:\\BankrollApplyReport1.dot";         

            Word.Document oWordDoc = oWordApp.Documents.Add(ref oTemplate, ref readOnly, ref readOnly, ref isVisible);
            oWordDoc.Activate();

            ////object oBookMark = "";
            ////oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = "3333";
            ////oBookMark = "ApplyUnitName";
            ////oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = "开发单位";
        Dictionary<string,string> ReportDic=new Dictionary<string,string>();
        ReportDic.Add("BarCode","20200202");
        ReportDic.Add("ApplyUnitName","开发单位");
        SetBookMark(ref oWordDoc,ReportDic);
            Object background = true;

            Object append = Type.Missing;

            Object range = Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument;

            Object outputFileName = Type.Missing;

            Object from = Type.Missing;

            Object to = Type.Missing;

            Object item = Microsoft.Office.Interop.Word.WdPrintOutItem.wdPrintDocumentContent;

            Object copies= 1;

            Object pages = "";

            Object pageType = Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages;

            Object printToFile = false;

            Object collate = true;

            Object fileName = "";

            Object activePrinterMacGX = Type.Missing;

            Object manualDuplexPrint = false;

            Object printZoomColumn = 0;

            Object printZoomRow = 0;

            Object printZoomPaperWidth = 0;

            Object printZoomPaperHeight = 0;

            //oWordDoc.Application.ActivePrinter = \\\\Jupitor\\HP550 on Ne03:;

            oWordDoc.PrintOut(ref background, ref append,

            ref range, ref outputFileName, ref from, ref to,

            ref item, ref copies, ref pages, ref pageType,

            ref printToFile, ref collate, ref activePrinterMacGX,

            ref manualDuplexPrint, ref printZoomColumn,

            ref printZoomRow, ref printZoomPaperWidth, ref printZoomPaperHeight);

 

因为模板书签定义好了,我们需要赋值,采用字典循环

  protected void SetBookMark(ref  Word.Document oWordDoc, Dictionary<string, string> dicField)
    {
        foreach (var dic in dicField)
        {
            object bookMark = (object)dic.Key;
            if (oWordDoc.Bookmarks.get_Item(ref bookMark) != null)
            {
                oWordDoc.Bookmarks.get_Item(ref bookMark).Range.Text = dic.Value;
            }
        }
    }

 

posted @ 2010-09-10 17:20  会游泳dě鱼  阅读(599)  评论(0编辑  收藏  举报