2012-12-05 1 views
1

내 프로그램에서 셰이프를 추가하거나 채우는 데 몇 가지 예외가 있습니다.모양이있는 Excel 성능 문제로 내보내기

나는 1000 개 이상의 도형을 추가해야하며 Excel에서 만들려면 ~ 18 세크가 필요합니다. 이는 허용되지 않습니다.

아래에서 코드를 찾고 쉽게 테스트 할 수 있도록 더미 콘솔 응용 프로그램 here을 배치했습니다.

도와주세요 :)

코드 :

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

    } 
} 
} 

답변