How to load a plain text (txt) files and save it as word document

Plain text format (TXT) is a basic format with the advantage of small size and it is compatible with almost all applications. With Spire.Doc for .NET, developers can convert word document into TXT easily. This article will show you how to load a plain text files and convert it to word documents.

Spire.Doc allows developers to import plain text file the same way as other document formats, by using the class Spire.Doc.Document. The whole process only need you three lines of codes. Firstly, please check the original plain text file as below:

How to load a plain text (txt) files and save it as word document

Here comes to the steps of how to save txt document into .docx.

Step 1: Create document instance and load the plain text file.

Document doc = new Document();
doc.LoadText("Sample.txt");

Step 2: Save the file as word document in .docx by using the code below

doc.SaveToFile("Result.docx", FileFormat.Docx2010);

Below is an effective screenshot of the result word document.

How to load a plain text (txt) files and save it as word document

Full codes:

namespace txttoword
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadText("Sample.txt");
            doc.SaveToFile("Result.docx", FileFormat.Docx2010);
        }
    }
}