Dear Justin,
thanks for your reply and sorry for not being more specific.
Here's the details:
#1 recipients is of type DataTable (10.000 DataRows, 5 Columns)
#2 Didn't know that was possible with the Document object of Spire.doc, so: no
#3 Out of memory exception (hitting 16 GB usage rather quickly)
#4 I think the script is very simple and straightforward - nothing special. The bottleneck is just the sheer amount of data. So the question is: can this be done in chunks, or is there another way of creating such a huge mailmerge document?
#5 Here's the whole code. It will automatically create a DataTable with 10000 rows, and to merge you can use any mail merge Word document with some text and 4 placeholder fields named Name,Street,City,Country.
- Code: Select all
public static void Main(string[] args)
{
string[] fields = "Name,Street,City,Country".Split(',');
DataTable dataTable = new DataTable();
dataTable.Columns.Add(new DataColumn("id"));
foreach (string field in fields) dataTable.Columns.Add(new DataColumn(field));
int numberOfRecords = 10000;
for (int i = 1; i <= numberOfRecords; i++)
{
DataRow newRow = dataTable.NewRow();
newRow["id"] = i;
foreach (string field in fields) newRow[field] = field + i;
dataTable.Rows.Add(newRow);
}
string saveAs = "result.doc";
string template = "any_word_file_that_contains_the_fields.doc";
Merge(dataTable, template, saveAs);
}
private static void Merge(DataTable recipients, string template, string saveAs)
{
Document document = new Document();
document.LoadFromFile(template);
document.MailMerge.ClearFields = true;
document.MailMerge.RemoveEmptyParagraphs = true;
document.MailMerge.Execute(recipients);
document.ViewSetup.DocumentViewType = DocumentViewType.PrintLayout;
document.SaveToFile(saveAs, FileFormat.Doc);
}
Everything works very well unless you have a large number of rows (depending on workstation or server memory).
It is ok to say that Spire.doc is not intended for so many rows. We might also try to create them in chunks and then combine the documents into one.
Thanks
Olaf