zoukankan      html  css  js  c++  java
  • c#打印(通过Word)

    DateTime dt = DateTime.Now;
                        Object none = System.Reflection.Missing.Value;
                        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
                        Microsoft.Office.Interop.Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);
                        //设置文档
                        document.PageSetup.TopMargin = 20;
                        document.PageSetup.BottomMargin = 20;
                        document.PageSetup.LeftMargin = 20;
                        document.PageSetup.RightMargin = 20;
                        wordApp.Visible = false;
                        wordApp.Selection.ParagraphFormat.LineSpacing = 10;// WdLineSpacing.wdLineSpace1pt5;//1f;//行间距wdLineSpaceSingle
                        object count = 14;
                        object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行
                        wordApp.Selection.MoveDown(ref WdLine, ref count, ref none);//移动鼠标
                        wordApp.Selection.Font.Size = 14;
                        wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                        wordApp.Selection.TypeText(label_RangeWeekFold.Text.Trim());//插入文本
                        //wordApp.Selection.Font.Borders.DistanceFromBottom=wdfo;
                        wordApp.Selection.TypeParagraph();//插入段落
                        wordApp.Selection.MoveDown(ref WdLine, ref count, ref none);
                        string str_temp = label13.Text.Trim() + label_MiddleZZBL.Text.Trim() + "%," + label14.Text.Trim() + label_ColoredPer.Text.Trim() + "%," + label17.Text.Trim() + label_BlackPer.Text.Trim() + "%," + label7.Text.Trim() + label_color.Text.Trim();
                        wordApp.Selection.Font.Size = 9;
                        wordApp.Selection.ParagraphFormat.LineSpacing = 10;
                        wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphJustify;
                        wordApp.Selection.TypeText(str_temp);
                        wordApp.Selection.MoveDown(ref WdLine, ref count, ref none);
                        wordApp.Selection.TypeParagraph();
                        try
                        {

                            //建立表格
                            Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count, srcDgv.Columns.Count, ref none, ref none);
                            //加入表格线
                            table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
                            table.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
                            for (int k = 1; k <= srcDgv.Rows.Count; k++)//填充数据
                            {
                                for (int j = 0; j < srcDgv.Columns.Count; j++)
                                {

                                    if (srcDgv[j, k - 1].Style.BackColor == Color.LightSteelBlue)
                                    {
                                        table.Cell(k, j + 1).Range.Shading.ForegroundPatternColor = WdColor.wdColorGray10;//改变单元格背景颜色
                                    }
                                    //table.Cell(k,j+1).Range.Text.
                                    table.Cell(k, j + 1).Range.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceExactly;
                                    table.Cell(k, j + 1).Range.ParagraphFormat.LineSpacing = 12;
                                    string ssTemp = srcDgv[j, k - 1].Value.ToString();
                                    if (ssTemp.IndexOf("\r\n") < 0) ssTemp = ssTemp + "\r\n";
                                    table.Cell(k, j + 1).Range.Text = ssTemp;// srcDgv[j, k - 1].Value.ToString();
                                }
                            }
                            object saveOption = WdSaveOptions.wdDoNotSaveChanges;
                            object background = false;
                            PrintDialog pd = new PrintDialog();
                            DialogResult dd = new DialogResult();
                            if (pd.ShowDialog() != DialogResult.Cancel)
                            {

                                wordApp.ActivePrinter = pd.PrinterSettings.PrinterName;
                                document.PrintOut(ref background, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none);
                                //foreach()
                                //{
                                //}
                                document.Close(ref saveOption, ref none, ref none); //关闭当前文档,如果有多个模版文件进行操作,则执行完这一步后接着执行打开Word文档的方法即可
                                saveOption = WdSaveOptions.wdDoNotSaveChanges;
                                wordApp.Quit(ref saveOption, ref none, ref none); //关闭Word进程
                                return;
                            }
                            document.Close(ref saveOption, ref none, ref none); //关闭当前文档,如果有多个模版文件进行操作,则执行完这一步后接着执行打开Word文档的方法即可
                            saveOption = WdSaveOptions.wdDoNotSaveChanges;
                            wordApp.Quit(ref saveOption, ref none, ref none); /

  • 相关阅读:
    part7-1 Python 的异常处理(try...except 捕获异常、异常类继承、访问异常信息、else、finally回收资源、raise引发异常、自定义异常类、except 和 raise 同时使用)
    part6-3 Python 类和对象练习
    part6-2 Python 类和对象(类变量和实例变量区别、property 函数定义属性及装饰器方法、隐藏和封装、继承(方法重写、未绑定方法调用)、super函数使用、动态与__slots__、type()定义类、metaclass使用、多态、issubclass、isinstance、__bases__ 属性、__subclasses__()方法、枚举类)
    贪心 No Time for Dragons Gym
    Bellman-Ford变形 Currency Exchange POJ
    Heavy Transportation POJ
    最短路变形 Frogger POJ
    最短路 Til the Cows Come Home POJ
    Song Jiang's rank list UVALive
    简单BFS +打印路径 迷宫问题 POJ
  • 原文地址:https://www.cnblogs.com/zhwl/p/2210597.html
Copyright © 2011-2022 走看看