加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#操作word 打印 打印预览

(2010-11-27 15:25:29)
标签:

c的打印

打印预览

word

c操作word

it

分类: IT技术相关文章推荐
/// word 应用对象  
        ///   
        private Microsoft.Office.Interop.Word.Application _wordApplication;

        ///   
        /// word 文件对象  
        ///   
        private Microsoft.Office.Interop.Word.Document _wordDocument;
        ///   
        /// 创建文档  
        ///   
        public void CreateAWord()
        {
            //实例化word应用对象  
            this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
            Object myNothing = System.Reflection.Missing.Value;

            this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing);
            //_wordDocument. = _wordApplication.bo;
            //Microsoft.Office.Interop.Word.Border=;
            //bo.Color = Color.Black;
            //bo.LineStyle = 1;
        }
        ///   
        /// 添加页眉  
        ///   
        ///   
        public void SetPageHeader(string pPageHeader)
        {
            //.wdOutlineView,wdSeekPrimaryHeader;
            //添加页眉  
            this._wordApplication.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;
            this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
            //this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);
            //设置中间对齐  
            this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
            //跳出页眉设置  
            this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
            //_wordApplication.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsMessageBox;
          
        }
        ///   
        /// 插入文字  
        ///   
        /// 文本信息  
        /// 字体打小  
        /// 字体颜色  
        /// 字体粗体  
        /// 方向  
        public void InsertText(object fileName)
        {
            Object missing = System.Reflection.Missing.Value;
            Person person = personManager.GetPersonById(id);
            string notableWords = person.Name + "," + person.Job + "," + person.Sex + "," + person.Address + "," + person.Age + "," + person.Enterdate.ToString() + "," + person.Marital + "," + person.GetHistoryDate.ToString() + "," + person.Native + "," + person.RecordDate + "," + person.National + "," + person.HistoryPerson ;
            //string findWords = "name,job,sex,address,age,enterdate,marital,getHistoryDate,native,recordDate,

national,historyPerson,complaints,nowhistory,pasthistory,personSystemSee,personSeebody,firstlog1";
            //string  noTable= "姓名,工作,性别,地址,年龄,入院日期,婚否,病史采集日期,籍贯,病史记录日期,民族,病史陈述者";
           
           //表格
            string [,] colRow =new string [6,4]{{ "姓名",person.Name, "单位",person.Job},{"性别",person.Sex, "职别" ,person.PerZB} ,{"年龄",person.Age.ToString(), "入院日期",person.Enterdate.ToString()}, {"婚姻",person.Marital, "病史采集日期",person.HistoryPerson}, {"籍贯",person.Native, "病史记录日期",person.RecordDate.ToString()},{ "民族",person.National, "病史陈述者", person.HistoryPerson }};
            object start = 0; object end = 0;
            Microsoft.Office.Interop.Word.Range tableLocation =_wordDocument.Range(ref start,ref end);
            Microsoft.Office.Interop.Word.Table table =   _wordDocument.Tables.Add(tableLocation,6,4,ref missing ,ref missing);                                                                                                                                             
            table.Rows.HeightRule = Microsoft.Office.Interop.Word.WdRowHeightRule.wdRowHeightAtLeast;
            table.Rows.Height = _wordApplication.CentimetersToPoints(float.Parse("0.8"));             
            table.Range.Font.Size = 10F;              
            table.Range.Font.Name = "宋体";              
            table.Range.Font.Bold = 0;
            for (int row = 0; row <6; row++)
            {
                for (int col = 0; col <4; col++)
                {
                    table.Cell(row + 1, col + 1).Range.Text =colRow[row,col];
                }
             
       
           
             //主诉
            string words =person.Address+","+ person.Complaints + "," + person.Nowhistory + "," + person.Pasthistory + "," + person.PSystemSee + "," + person.PSeeBody;
            string[] wordContent = words.Split(',');
            string findWords="通讯地址,主诉,现病史,过去史,系统回顾,体格检查";
            string[] findWordConent = findWords.Split(',');
            for (int i = 0; i < wordContent.Length; i++)
            {
                for (int j = 0; j < findWordConent.Length; j++)
                {
                    if (i == j)
                    {
                        Microsoft.Office.Interop.Word.Paragraph op1;
                        op1 = _wordDocument.Content.Paragraphs.Add(ref missing);
                        op1.Range.Text = findWordConent[j] + ":" + wordContent[i];
                        op1.Format.SpaceAfter = 24;
                        op1.Range.Font.Size = 10F;
                        //op1.Range.Font.Bold = 1;
                        op1.Range.InsertParagraphAfter();
                    }
                }
            }


            //小结

            StateSummary s = stateManager.GetStateSummary(person.Id.ToString());
            string [,] txtSummary=new string [5,2]{{"最后诊断","初步诊断  "},{s.Lastz1,s.Lastc1},{s.Lastz2,s.Lastc2},{s.Lastz3,s.Lastc3},{s.Lastz4,s.Lastc4}};

            Microsoft.Office.Interop.Word.Paragraph op2;
            op2 = _wordDocument.Content.Paragraphs.Add(ref missing);
            op2.Range.Text = "小结:" + s.SummaryContent;
            op2.Format.SpaceAfter = 24;
            op2.Range.Font.Size = 10F;
            op2.Range.InsertParagraphAfter();

            Microsoft.Office.Interop.Word.Table table1 = _wordDocument.Tables.Add(_wordDocument.Paragraphs.Last.Range, 4,2, ref missing, ref missing);
            table1.Rows.HeightRule = Microsoft.Office.Interop.Word.WdRowHeightRule.wdRowHeightAtLeast;
            table1.Rows.Height = _wordApplication.CentimetersToPoints(float.Parse("0.8"));
            table1.Range.Font.Size =10F;
            table1.Range.Font.Name = "宋体";
            table1.Range.Font.Bold = 0;
            table1.Range.InsertParagraphAfter();
            for (int row = 0; row <5; row++)
            {
                for (int col = 0; col < 2; col++)
                {
                    table1.Cell(row + 1, col + 1).Range.Text = txtSummary[row, col];
                }
             

            //首页病程记录

            Microsoft.Office.Interop.Word.Paragraph op9;
            op9 = _wordDocument.Content.Paragraphs.Add(ref missing);
            op9.Range.Text ="首次病程记录:"+person.Firstlog1;
            op9.Format.SpaceAfter = 24;
            op9.Range.Font.Size = 10F;
            op9.Range.InsertParagraphAfter();
            //小结
 

            //接班小结
            IList<TwoSummary> _listTwoJ = twoManager.GetStateSummaryAll(person.Id.ToString(), 0);
            foreach (var _two in _listTwoJ)
            {
                Microsoft.Office.Interop.Word.Paragraph op3;
                op3 = _wordDocument.Content.Paragraphs.Add(ref missing);
                op3.Range.Text =_two.Logdate.ToString() +"  接班小结:";
                op3.Format.SpaceAfter = 24;
                op3.Range.Font.Size = 10F;
                op3.Range.InsertParagraphAfter();
                Microsoft.Office.Interop.Word.Paragraph op4;
                op4 = _wordDocument.Content.Paragraphs.Add(ref missing);
                op4.Range.Text =_two.SummaryContent;
                //op1.Range.Font.Bold = 1;
                op4.Format.SpaceAfter = 24;
                op4.Range.Font.Size = 10F;
                op4.Range.InsertParagraphAfter();
            }

            //,阶段小结
            IList<TwoSummary> _listTwo = twoManager.GetStateSummaryAll(person.Id.ToString(), 1);
            foreach (var _two in _listTwo)
            {
                Microsoft.Office.Interop.Word.Paragraph op3;
                op3 = _wordDocument.Content.Paragraphs.Add(ref missing);
                op3.Range.Text =_two.Logdate.ToString()+ "  阶段小结:";
                //op1.Range.Font.Bold = 1;
                op3.Range.Font.Size = 10F;
                op3.Format.SpaceAfter = 24;
                op3.Range.InsertParagraphAfter();
                Microsoft.Office.Interop.Word.Paragraph op4;
                op4 = _wordDocument.Content.Paragraphs.Add(ref missing);
                op4.Range.Text =_two.SummaryContent;
                //op1.Range.Font.Bold = 1;
                op4.Range.Font.Size = 10F;
                op4.Format.SpaceAfter = 24;
                op4.Range.InsertParagraphAfter();
            }


            //出院记录
            IList<DischargeRecords> _listdischarRecord = disManager.GetDischargeRecords(person.Id.ToString());
            foreach (var _d in _listdischarRecord)
            {
                Microsoft.Office.Interop.Word.Paragraph op4;
                op4 = _wordDocument.Content.Paragraphs.Add(ref missing);
                op4.Range.Text = _d.LogDate.ToString()+"   出院记录:";
                op4.Range.Font.Size = 10F;
                op4.Format.SpaceAfter = 24;
                op4.Range.InsertParagraphAfter();
                Microsoft.Office.Interop.Word.Paragraph op5;
                op5 = _wordDocument.Content.Paragraphs.Add(ref missing);
                op5.Range.Text =_d.RecordContent;
                op5.Range.Font.Size = 10F;
                op5.Format.SpaceAfter = 24;
                op5.Range.InsertParagraphAfter();
            }
          

        }


        ///   
        /// 换行  
        ///   
        public void NewLine()
        {
            //换行  
            this._wordApplication.Application.Selection.TypeParagraph();
        }
        ///   
        /// 插入一个图片  
        ///   
        ///   
        public void InsertPicture(string pPictureFileName)
        {
            object myNothing = System.Reflection.Missing.Value;
            //图片居中显示  
            this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
            this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing);
        }
        ///   
        /// 保存文件   
        ///   
        /// 保存的文件名  
        public void SaveWord(object pFileName)
        {
            if (File.Exists(pFileName.ToString()))
                File.Delete(pFileName.ToString());
            object myNothing = System.Reflection.Missing.Value;
            object myFileName = pFileName;
            object myWordFormatDocument = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
            object myLockd =false;
            object myPassword = "";
            object myAddto = true;
            try
            {
                this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,
                    ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,
                    ref myNothing, ref myNothing, ref myNothing);
            }
            catch
            {
                throw new Exception("导出word文档失败!");
            }
        }
        public void Print(object fileName)
        {
            try
            {
                foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("WINWORD"))
                {
                    p.Kill();
                }
                this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
                Object missing = System.Reflection.Missing.Value;
                object redOlny = false ;
                this._wordDocument = this._wordApplication.Documents.Open(ref fileName, ref missing, ref redOlny, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

                //------------------------使用Printout方法进行打印------------------------------
                object background = false; //这个很重要,否则关闭的时候会提示请等待Word打印完毕后再退出,加上这个后可以使Word所有
                _wordDocument.PrintOut(ref background, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref

missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,

    ref missing);
                object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges;
                this._wordDocument.Close(ref saveOption, ref missing, ref missing); //关闭当前文档,如果有多个模版文件进行操作,则执行完这一步后接着执行打开Word文档的方法即可
                saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                this._wordApplication.Quit(ref saveOption, ref missing, ref missing); //关闭Word进程
            }
            catch (Exception ex)
            {
                MessageBox.Show("没安装打印机或者打印机出故障");

            }

            }
        public void PrintViewWord(object fileNmae)
        {
            foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("WINWORD"))
            {
                p.Kill();
            }
          Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
          object Missing = System.Reflection.Missing.Value;
          object readOnly = false;
          Microsoft.Office.Interop.Word.Document wordDoc = wordApp.Documents.Open(ref fileNmae, ref Missing, ref readOnly, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing);
          wordApp.Visible = true;
          wordDoc.PrintPreview();
        }

 

C#的打印 和打印预览word请查看http://www.b2csofts.com/CompanyNewsDetail.aspx?id=31

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有