Three methods of c# loading word

Time:2022-5-3

This experience content share three different methods of loading word documents through c# program. namely:

1. Load local word document

2. Load word document in read-only mode

3. Load word from stream

[program environment]

  • Windows 10
  • Visual Studio 2017
  • Word class library – spire Doc for . NET
  • Word version-2013 (. Docx)

[reference assembly]

Before editing code and loading word document, spire. XML needs to be introduced doc. DLL to VS, which can be directly downloaded and installed into the program through nuget in vs. The specific steps are as follows:

 

 

 

Waiting for installation. After installation, the effect is shown in the figure below:

 

1. Load local word

Method 1: initializes a new instance of the document class from the specified existing document through the document (string filename) constructor.

C#

using Spire.Doc;
namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document(@"E:\Work\Documents\test.docx");
        }
    }
}

Method 2Initialize the object of the document class, and then call the loadfromfile (string filename) method to load the word document in the local path.

C#

using Spire.Doc;
namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\test.docx"); 
        }
    }
}

2. Load word in read-only mode

Initialize the object of document class and call document The loadfromfileinreadmode (string strfilename, fileformat) method loads word in the local path in read-only mode.

C#

using Spire.Doc;
namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();        
            document.LoadFromFileInReadMode(@"E:\Work\Documents\test.docx", FileFormat.Docx);
        }
    }
}

3. Load word from stream

Method 1The constructor document (stream stream) initializes a new instance of the document class from the specified data stream.

C#

using Spire.Doc;
using System.IO;
namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            Stream stream = File.OpenRead(@"E:\Work\Documents\test.docx");
            Document document = new Document(stream);
        }
    }
}

Method 2: initialize the object of document class and call document The loadfromstream (stream stream, fileformat, fileformat) method loads word from the stream.

C#

using Spire.Doc;
using System.IO;
namespace TEST
{
    class Program
    {
        static void Main(string[] args)
        {
            Stream stream = File.OpenRead(@"E:\Work\Documents\test.docx");
            Document document = new Document();
            document.LoadFromStream(stream, FileFormat.Docx);
        }
    }
}

 

 

These are the three methods of loading word documents through c# this time.

 

Recommended Today

Big data Hadoop — spark SQL + spark streaming

catalogue 1、 Spark SQL overview 2、 Sparksql version 1) Evolution of sparksql 2) Comparison between shark and sparksql 3)SparkSession 3、 RDD, dataframes and dataset 1) Relationship between the three 1)RDD 1. Core concept 2. RDD simple operation 3、RDD API 1)Transformation 2)Action 4. Actual operation 2)DataFrames 1. DSL style syntax operation 1) Dataframe creation 2. SQL […]