我在程序中添加和处理形状时遇到了一些性能问题。
我必须添加 1000 多个形状,并且在 excel 中创建大约需要 18 sek - 这是不可接受的。
您将在下面找到代码,为了让生活更轻松,我在 这里放置了一个虚拟控制台应用程序以便于测试。
请帮忙 :)
代码:
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);
}
}
}