c#通過Word實現列印的代碼(表格為例)

來源:互聯網
上載者:User

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); /

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.