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;
}
}
}
}
}