简体   繁体   English

MS-Word标签通过c#.net优化问题

[英]Ms-Word Labeling through c#.net optimization issue

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. 目前,我正在使用.NET桌面应用程序使用Ms-Word标签功能。我需要一次打印约5000条记录。 It takes approximately 3.5 minutes according to my code. 根据我的代码,大约需要3.5分钟。 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!!! 谢谢!!!

The main problem of your code it, that it uses Interop. 您的代码的主要问题是它使用了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. 充其量,您可以使用Word或Office open xml直接创建不使用Word的文件,而无需打开word即可直接创建.docx文件。 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. 也许这会有所帮助: http : //docx.codeplex.com/我不知道该库,但是ANY库比直接处理Office Open XML更可取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM