1

プログラムで図形を追加および処理する際に、いくつかのパフォーマンスの問題が発生しました。

1000 個以上の図形を追加する必要があり、Excel で作成するのに約 18 秒かかります。これは受け入れられません。

以下にコードを示し ます。簡単にテストできるように、ダミーのコンソール アプリケーションをここに配置しました。

助けてください :)

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using Microsoft.Office.Core;

namespace DummyPreformanceTestProject
{
class Program
{
    static void Main(string[] args)
    {
        //Create Excel application
        Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
        if (xlApp == null) { return; }

        //Handle Culture
        System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

        //Create Wookbook and workSheet
        Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
        Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
        Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];

        //Shapes
        Excel.Shape shp;

        ///// PREFORMANCE TIMER START
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        ///// PREFORMANCE TIMER START

        //Optimise Excel app preformance by not updating the sheet.
        xlApp.ScreenUpdating = false;

        //Insert shapes in Excel and applie color + text 
        for (int i = 0; i < 1000; i++)
        {
            //Preformance Issue here!
            shp = worksheet.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, i, i, 100, 10);
            shp.Fill.ForeColor.RGB = (65536 * i) + (256 * i) + (i);//Must be individually
            shp.Line.ForeColor.RGB = (65536 * i) + (256 * i) + (i);//Must be individually
            shp.TextFrame2.TextRange.Characters.Text = i.ToString();//Must be individually                  
        }

        //Hax to getting the collection of shapes in a ShapeRange
        var drawObjs = (Excel.DrawingObjects)worksheet.DrawingObjects();
        Excel.ShapeRange shapeRng = (Excel.ShapeRange)drawObjs.ShapeRange;

        //Setting common atributes on all shapes in the ShapeRange
        shapeRng.TextEffect.Alignment = MsoTextEffectAlignment.msoTextEffectAlignmentCentered;
        //shapeRng.TextFrame2.TextRange.Font.Bold = MsoTriState.msoTrue;
        //shapeRng.TextFrame2.TextRange.Font.Name = "Arial";
        //shapeRng.TextFrame2.TextRange.Font.Size = 10;
        //shapeRng.TextFrame2.TextRange.Font.Fill.Visible = MsoTriState.msoTrue;
        shapeRng.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = (int)Excel.XlRgbColor.rgbDarkBlue;

        //Vertical allignemnt seems to need individual handeling.
        Excel.Shapes theShapes = worksheet.Shapes;
        foreach (Excel.Shape aShape in theShapes)
        {
            aShape.TextFrame.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
        }

        xlApp.ScreenUpdating = true;
        xlApp.Visible = true;

        ///// PREFORMANCE TIMER STOP
        stopWatch.Stop();
        TimeSpan ts = stopWatch.Elapsed;
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
        Console.WriteLine("Export RunTime: " + elapsedTime);
        ///// PREFORMANCE TIMER STOP

        Console.WriteLine("The goal is export in under 3 sek");
        Console.WriteLine("Press any key to continue...");
        Console.ReadKey(true);

    }
}
}
4

1 に答える 1

1

Microsoft は、ライセンス、パフォーマンスなどのさまざまな問題につながるため、Interop を推奨していません。OpenXML を選択してください。

http://msdn.microsoft.com/en-us/library/office/bb448854(v=office.14).aspx

于 2012-12-05T12:23:25.987 に答える