簡體   English   中英

MS-Word標簽通過c#.net優化問題

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

目前,我正在使用.NET桌面應用程序使用Ms-Word標簽功能。我需要一次打印約5000條記錄。 根據我的代碼,大約需要3.5分鍾。 在這里,在我的代碼中,我傳遞的虛擬數據很少,但在實際情況下,我需要傳遞大數據。

我的代碼是:

        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();

任何幫助都可以得到極大的應用..!

謝謝!!!

您的代碼的主要問題是它使用了Interop。 互操作性本來就很慢,您可能會加快速度,但仍會花費很多時間。 充其量,您可以使用Word或Office open xml直接創建不使用Word的文件,而無需打開word即可直接創建.docx文件。 缺點是,這比上面提到的簡單代碼消耗了更多的開發資源。 因此,您必須確定它是否值得開發時間。 也許這會有所幫助: http : //docx.codeplex.com/我不知道該庫,但是ANY庫比直接處理Office Open XML更可取。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM