如何将Console.WriteLine输出保存到Word文件中

问题描述 投票:0回答:2

寻找一些帮助,弄清楚如何将C#程序的输出保存到word文件中?感谢我提供任何指导,因为我是这个编码领域的新手。

class Test
        {
            static void Main(string[] args)
            {
              Application app = new word.Application();
              Document doc = app.Documents.Open(filePath);

                int p, lastInt = 0, input;
                Console.WriteLine("Enter the Number of Rows : ");
                input = int.Parse(Console.ReadLine());
                for (int i = 1; i <= input; i++)
                {
                    for (p = 1; p <= i; p++)
                    {
                        if (lastInt == 1)
                        {
                            Console.Write("0");
                            lastInt = 0;
                        }
                        else if (lastInt == 0)
                        {
                            Console.Write("1");
                            lastInt = 1;
                        }
                    } Console.Write("\n");
                }
                Console.WriteLine();

                //How to save the above Console.Writeline in the word file??

                Doc.close();
                App.Quit();
            }
        }
c# office-interop
2个回答
0
投票

[我使用以下代码创建了Word文档,创建了一个段落,编写了文本并保存了它。

请注意,当您使用Console.WriteConsole.WriteLine时,仅将数据打印到Console。您可以使用辅助方法,也可以使用类似委托的方法来为您做这两种事情。我通常使用StringBuilder保留我需要保存的所有数据,最后,将其添加到文件/文档中,然后保存。

    StringBuilder sb = new StringBuilder();
    void printer(string message, bool newLine = true) 
    { 
        if (newLine) 
        {
            sb.AppendLine(message); 
            Console.WriteLine(message); 
        }
        else 
        {
            sb.Append(message); 
            Console.Write(message); 
        } 
    }

    string filePath = @"C:\temp\newdoc.doc";
    _Application word = new Application();
    var doc = word.Documents.Add();

    int p, lastInt = 0, input;
    printer("Enter the Number of Rows : ", false);
    input = int.Parse(Console.ReadLine());
    printer(input.ToString());
    for (int i = 1; i <= input; i++)
    {
        for (p = 1; p <= i; p++)
        {
            if (lastInt == 1)
            {
                printer("0", false);
                lastInt = 0;
            }
            else if (lastInt == 0)
            {
                printer("1", false);
                lastInt = 1;
            }
        }
        printer("\n");
    }
    printer("");

    //How to save the above Console.Writeline in the word file??
    var para = doc.Paragraphs.Add();
    para.Range.Text = sb.ToString();

    doc.SaveAs2(filePath);
    doc.Close();

0
投票

在讨论使用打印机功能简单地创建StringBuilder然后在末尾附加的基础知识之外增加了一个不同的答案。

您可以看到如何通过以下方式更改文本的颜色和背景。创建新行时设置了颜色。

    public static void CreateWordDocument()
    {
        //How to save the above Console.Writeline in the word file??
        string filePath = @"C:\temp\newdoc.doc";
        _Application word = new Application();
        var doc = word.Documents.Add(); 
        var para = doc.Paragraphs.Add();

        para.Range.Text = string.Empty;

        void printer(string message, WdColor color = WdColor.wdColorBlack, WdColor background = WdColor.wdColorWhite, bool newLine = true)
        {
            if (newLine)
            {
                para.Range.InsertAfter(message + Environment.NewLine);
                para.Range.Font.Color = color;
                para.Range.Shading.BackgroundPatternColor = background;
                Console.WriteLine(message);
            }
            else
            {
                para.Range.InsertAfter(message);
                Console.Write(message);
            }
        }

        int p, lastInt = 0, input;
        printer("Enter the Number of Rows : ", WdColor.wdColorDarkTeal, newLine: false);
        input = int.Parse(Console.ReadLine());
        printer(input.ToString(), WdColor.wdColorGreen);

        for (int i = 1; i <= input; i++)
        {
            for (p = 1; p <= i; p++)
            {
                if (lastInt == 1)
                {
                    printer("0", newLine:false);
                    lastInt = 0;
                }
                else if (lastInt == 0)
                {
                    printer("1", newLine: false);
                    lastInt = 1;
                }

            }
            printer(Environment.NewLine, WdColor.wdColorLightOrange);
        }

        doc.SaveAs2(filePath);
        doc.Close();

    }

我强烈建议您阅读如何在Word文档中处理数据,但这应该使您对如何将要打印的内容保存到控制台有个好主意。您使用Console.WriteLine()打印到Console的任何内容都不能重定向到c#代码内的word文档写入。

© www.soinside.com 2019 - 2024. All rights reserved.