我用C#写了一个打印程序。利用PrintDocument这个类配合Graphics画出表格进行打印,现在客户要求在打印预览或者其他时候能把表格输出或者另存为WORD文档。
请问哪位知道该怎么做?

解决方案 »

  1.   


                        //做一个打印的模板,用word里面的replace方法。
                          //将预览另存为word时,先将模板copy到另外的文件夹,然后查找替换。
                        object oMissing = System.Reflection.Missing.Value;
                        Microsoft.Office.Interop.Word._Application oWord;
                        Microsoft.Office.Interop.Word._Document oDoc;
                        oWord = new Microsoft.Office.Interop.Word.Application();
                        oWord.Visible = false;
                        object fileName = "需要替换的word文件.doc";
                        oDoc = oWord.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);                    object FindText, ReplaceWith, Replace;
                        Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;                    FindText = "要查找的文本1";//要查找的文本
                        ReplaceWith = "str1";//替换文本
                        oDoc.Content.Find.Execute(ref FindText, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref ReplaceWith, ref Replace, ref oMissing, ref oMissing, ref oMissing, ref oMissing);                    FindText = "要查找的文本2";//要查找的文本
                        ReplaceWith = "str2";//替换文本
                        oDoc.Content.Find.Execute(ref FindText, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref ReplaceWith, ref Replace, ref oMissing, ref oMissing, ref oMissing, ref oMissing);                    oDoc.Save();                    if (oDoc != null)
                            oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                        if (oWord != null)
                            oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
      

  2.   

    谢谢楼上的
    不过我不是通过WORD模版来实现的
    因为我的打印表格是通过坐标配置后打印出来的,没有固定的格式和位置。
    所以希望能直接将打印的内容输出到一个WORD文件即可。
      

  3.   

    那就自己操作word自己画吧.
    或者用word的Microsoft Office Document Image Writer虚拟打印机,打印成.mdi文件,不知道可不可以?
      

  4.   

    自己操作word自己画这个说不清楚.需要根据你自己的需要做.
    Microsoft Office Document Image Writer虚拟打印机office自带的,安装之后,你在打印的时候,只需要选择Microsoft Office Document Image Writer进行打印,虚拟打印机就会打印成一个.mdi的文件,一样的可以对里面的信息copy等操作.可以先google一下Microsoft Office Document Image Writer,了解了解.
      

  5.   

    根据你的项目情况,目前,比较容易实现的方法,先存一个图,然后把图插入到WORD。
      

  6.   

    打印预览,
    截图存为image
    image放入word
      public Image CatchImage()
            {
              
                Bitmap bmpCatched = new Bitmap(预览控件.Width, 预览控件.Height);
                Graphics g = Graphics.FromImage(bmpCatched);
                Cursor.Hide();
                g.CopyFromScreen(FlashBox.Location, new Point(0, 0), FlashBox.ClientRectangle.Size);
              
             
                return bmpCatched;        }   public static void ImageToWord(Image image)
            {
                string fileName = "tmp.jpg"; //重命名要要插入的图片
                image.Save(fileName);
                FileInfo fi = new FileInfo(fileName);
                try
                {                //ApplicationClass wordApp = new ApplicationClass();
                    Object Nothing = System.Reflection.Missing.Value;
                    Microsoft.Office.Interop.Word.Application oword = new Microsoft.Office.Interop.Word.Application();                Document doc = oword.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);//word文档                oword.Selection.Font.Bold = 1;
                    oword.Selection.Font.Size = 12;
                    oword.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;                oword.Visible = true;                object LinkToFile = false;                oword.Application.Selection.InlineShapes.AddPicture(fi.FullName, ref Nothing, ref Nothing, ref Nothing); //doc为活动文档的名称
                }
                catch (Exception ex) { Log.WriteErLog(ex.Message); }
                finally
                {
                    System.Diagnostics.Process[] CurrentProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD");
                    for (int i = 0; i < CurrentProcess.Length; i++)
                    {
                        if (CurrentProcess[i].MainWindowHandle.ToInt32() == 0)
                        {
                            try
                            {
                                CurrentProcess[i].Kill();
                            }
                            catch
                            {
                            }
                        }
                    }
                }        }
      

  7.   

    截图放入word,不能copy文字信息吧?
      

  8.   

    yuxuanji的方案有个问题,就是我每次打印预览的时候,弹出的窗口并不是最大化的,而且有时候他默认的显示比例是50%的,所以预览的时候直接截屏取得的图片会有偏差。
      

  9.   

    如果可以用图片的话,那你不是都已经用Graphics画过吗?同样可以在画了保存图片插入到word.
      

  10.   

    可以用excel代替么?
    如果可以的话,你可以试着用reporting service
    这个服务可以导出excel,pdf等,还可以打印
    工具自带的
      

  11.   

    我这个是个小应用软件,如果应用reporting service 的话显得太庞大了
      

  12.   

    LZ,可以考虑Microsoft Office Document Image Writer虚拟打印机打印成其它文件保存.
    而且可以通过修改注册表的值,让虚拟打印机打印时不打开文件.
    HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\MODI\MDI writer 
    DWORD OpenInMODI=0