C#导出到Excel性能问题的形状

我在程序中添加和处理形状时遇到了一些性能问题。

我必须添加1000 +形状,并需要〜18 SEK创build在Excel中 – 这是不能接受的。

下面你会发现代码,并使生活更轻松我已经在这里放置了一个虚拟控制台应用程序,以便于testing。

请帮忙 :)

码:

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

微软不推荐Interop,因为它会导致诸如许可,性能等各种问题,请转到OpenXML

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