0

現在、Ms - Word のラベル付け機能を使用する .net デスクトップ アプリケーションに取り組んでいます。一度に約 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();

どんな助けでも大いに感謝することができます..!!

ありがとうございました!!!

4

1 に答える 1

1

コードの主な問題は、Interop を使用していることです。相互運用性は本質的に非常に遅いです。速度を上げることはできますが、それでも時間がかかります。せいぜい、Word を使用せずに、Word を開かずに .docx ファイルを作成できるライブラリまたは Office オープン xml を使用して、ファイルを作成できます。欠点は、これが上記の単純なコードよりも多くの開発リソースを消費することです。したがって、開発時間に見合う価値があるかどうかを判断する必要があります。おそらくこれが役立つでしょう: http://docx.codeplex.com/ライブラリはわかりませんが、Office Open XML を直接処理するよりも任意のライブラリを使用することをお勧めします。

于 2013-08-06T08:26:49.627 に答える