Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Sat Apr 19, 2014 4:28 am

I made a tool in C# that creates Word templates / documents with metadata from a csv file. I.e (Title,Subject,Authors,Category,Keywords,Comments)


You simply point dotxmaker.exe to a csv file (with the correct headers) and it will create them.
C:\Program Files (X86)\dotxMaker\dotxmaker.exe "path to csv file here"

I have a small problem with Spire.doc. I created a blank.dotx template in Word 2010 then saved it to C:\Program Files (X86)\dotxMaker\Data

var myDocument = new Spire.Doc.Document(wordTemplateFile)
(The wordTemplateFile variable contains the full path to the blank.dotx file.)

On line 73, myDocument.SaveToFile(fullFilePath, FileFormat.Dotx2010);
(The fullFilePath variable outputs the new dotx files where the csv file is located.)

if I set the FileFormat to Auto, the newly created dotx files are corrupt when I open them in Word 2010. If I change the FileFormat to DotX2010 the files will open correctly in Word 2010.

Code: Select all
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows.Forms;
using CsvHelper;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
 
namespace dotxMaker
{
    internal class Program
    {
        private static string CleanName(string name)
        {
            return Path.GetInvalidFileNameChars().Aggregate(name, (current, c) => current.Replace(c.ToString(), string.Empty));
        }
        [STAThread]
        public static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                MessageBox.Show("CSV file not found."  + '\n' + '\n' + "PathTo\\dotxMaker.exe" + " FileName.csv", "dotxMaker",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string ext = Path.GetExtension(args[0]);
 
            if (ext != ".csv")
            {
                MessageBox.Show("Error: " + ext + " is not a csv file!" + '\n' + '\n' + "PathTo\\dotxMaker.exe" + " FileName.csv", "dotXMaker",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            using (var sr = new StreamReader(args[0]))
            {
                var reader = new CsvReader(sr);
                reader.Configuration.Delimiter = ",";
                //CSVReader will now read the whole file into an enumerable
                IEnumerable<DataRecord> records = reader.GetRecords<DataRecord>();
                reader.Configuration.HasHeaderRecord = true;
                string currentFolder = System.IO.Path.GetDirectoryName(args[0]);
 
                try
                {
                    foreach (DataRecord record in records)
                    {
                        string cleanedFileName = CleanName(record.FileName);
                        string appPath = Path.GetDirectoryName(Application.ExecutablePath);
                        string[] getBlankDocumentFullFilePath = Directory.GetFiles(appPath + "\\" + "Data");
                        string getBlankDocument = Path.GetFileName(getBlankDocumentFullFilePath[0]);
                        string wordTemplateFile = appPath + "\\" + "Data" + "\\" + getBlankDocument;
                        string fileNameExt = Path.GetExtension(wordTemplateFile);
                        string fileName = cleanedFileName + fileNameExt;
                        string fullFilePath = System.IO.Path.Combine(currentFolder, fileName);
 
                        if (File.Exists(fullFilePath) == true)
                        {
                            continue;
                        }
 
                        var myDocument = new Spire.Doc.Document(wordTemplateFile);
                        myDocument.BuiltinDocumentProperties.Title = record.Title;
                        myDocument.BuiltinDocumentProperties.Subject = record.Subject;
                        myDocument.BuiltinDocumentProperties.Author = record.Author;
                        myDocument.BuiltinDocumentProperties.Category = record.Category;
                        myDocument.BuiltinDocumentProperties.Keywords = record.Keywords;
                        myDocument.BuiltinDocumentProperties.Comments = record.Comments;
                        myDocument.SaveToFile(fullFilePath, FileFormat.Dotx2010);
                        myDocument.Close();
                    }
                }catch(Exception error)
                {
                    MessageBox.Show(error.Message, "dotxMaker",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
           }
        } 
    }
}

jhn
 
Posts: 1
Joined: Sat Apr 19, 2014 1:55 am

Mon Apr 21, 2014 9:05 am

Hello,

Thanks for your inquiry.
Sorry for the delay response for the weekend here.
Kindly notice that FileFormat.Auto just is to recognize the format automatically when load a document, hence, you must define the real fileformat when you save the output.

Feel free to contact us if you have any problems.

Best wishes,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Thu Apr 24, 2014 9:17 am

Hello,

Has the issue been resolved? Could you please give us some feedback if convenience?

If there are any questions, welcome to get it back to us.

Thanks,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Return to Spire.Doc