Reputation:
Currently I am working on the .net desktop application which uses the Ms - Word Labeling functionality.I need to print approximate 5000 records at a time. It takes approximately 3.5 minutes according to my code. Here, in my code I am passing little dummy data but in real scenario I need to pass large data.
My code is:
oWord = new Word.Application();
oWord.Visible = false;
object missing = System.Reflection.Missing.Value;
object isVisible = false;
object filename = @"C:\Users\jc\Desktop\label1.docx";
object saveFile;
oDoc = oWord.Documents.Open(ref filename, ConfirmConversions: false, ReadOnly: true);
var innerTable = oDoc.Content.Tables[1];
int totalLabelsPerPage = innerTable.Rows.Count * innerTable.Columns.Count;
int totalRowsPerPage = innerTable.Rows.Count;
int totalColumnsPerPage = innerTable.Columns.Count;
String[] collection = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63" };
object StartPoint = 0;
Range MyRange;
int requiredTables = 1;
while ((requiredTables * totalRowsPerPage * totalColumnsPerPage) < collection.Length)
{
MyRange = oDoc.Range(ref StartPoint, ref missing);
MyRange.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd);
MyRange.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage);
MyRange.InsertFile(@"C:\Users\jc\Desktop\label.docx");
requiredTables++;
}
int count = oDoc.Content.Tables.Count;
int no = 1;
int columnCount = innerTable.Columns.Count;
int currentColumn = 1;
int cont = 0;
int columns = 1;
int currentTableNo = 1;
while (cont <= collection.Length)
{
innerTable = oDoc.Content.Tables[currentTableNo];
innerTable.Range.Bold = 1;
innerTable.Range.Bold = 1;
innerTable.Range.Text = "sdadad";
innerTable.Range.Font.Bold = 1;
innerTable.Range.Font.Color = Word.WdColor.wdColorBlue;
innerTable.Range.Font.Size = 15F;
innerTable.Range.Font.Name = "Verdana";
while (columns <= columnCount)
{
innerTable.Columns[columns].Cells[no].Range.Text = collection[cont];
no++;
cont++;
if (cont == collection.Length)
break;
else if (no == totalRowsPerPage+1)
{
columns++;
no=1;
}
}
if (cont == collection.Length)
break;
else
{
columns = 1;
currentTableNo++;
}
}
this.Cursor = Cursors.Default;
oWord.Visible = true;
oDoc.Activate();
Any help could be greatly appriciated..!!
Thank you!!!
Upvotes: 0
Views: 120
Reputation: 10909
The main problem of your code it, that it uses Interop. Interop is inherently slow as hell - you might speed it up, but it WILL still take ages. At best, you could create your file without Word, with a library or Office open xml directly which can create a .docx file without opening word. The downside is, that this consumes a lot more development resources than your simple code mentioned above. So you have to decide if its worth the development time. Maybe this would help:http://docx.codeplex.com/ I don't know the library, but ANY library is preferable to dealing with Office Open XML directly.
Upvotes: 1