还没开始读写Excel,程序启动特别的慢。我的代码:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); } private Microsoft.Office.Interop.Excel.Application xlApp ; private void Form1_Load(object sender, EventArgs e) { xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
if (xlApp == null) { MessageBox.Show("Can't open Excel!"); return; }
}
}
}窗口半天才出现,究竟是为什么????
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); } private Microsoft.Office.Interop.Excel.Application xlApp ; private void Form1_Load(object sender, EventArgs e) { xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
if (xlApp == null) { MessageBox.Show("Can't open Excel!"); return; }
}
}
}窗口半天才出现,究竟是为什么????
我的方法是显示一个等待图片。
{
private static Application _excelApp;
private static ExcelApplication _instance;
protected ExcelApplication()
{
}
private Application CreateApplication()
{
try
{
_excelApp = new ApplicationClass();
_excelApp.Visible = false;
return _excelApp;
}
catch
{
return null;
}
} public Application GetApplication()
{
try
{
_excelApp = this.CreateApplication();
_excelApp.Visible = false;
return _excelApp;
}
catch
{
this.KillwordProcess();
return this.CreateApplication();
}
} public static ExcelApplication GetIntance()
{
if (_instance == null)
{
lock (typeof(ExcelApplication))
{
if (_instance == null)
{
_instance = new ExcelApplication();
}
}
}
return _instance;
}
public void KillwordProcess()
{
try
{
foreach (Process process in Process.GetProcessesByName("EXCEL"))
{
process.Kill();
}
}
catch
{
}
} public void Quit()
{
try
{
_excelApp.Quit();
}
catch
{
}
}
}
{
object[,] aDat = new object[10000, 26];
for (int i = 0; i < aDat.GetLength(0); i++)
{
for (int j = 0; j < aDat.GetLength(1); j++) aDat[i, j] = i + j;
}
System.Reflection.Missing oMss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application oApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook oBok = oApp.Workbooks.Add(oMss);
Microsoft.Office.Interop.Excel.Worksheet oSht = (Microsoft.Office.Interop.Excel.Worksheet)oBok.Worksheets.Add(oMss, oMss, oMss, oMss);
oSht.Name = "报表";
oSht.get_Range(oSht.Cells[1, 1], oSht.Cells[aDat.GetLength(0), aDat.GetLength(1)]).Value2 = aDat;
oApp.Visible = true;
}处理器 : AMD Athlon(tm) 7750 2.70GHz
操作系统: XPsp2
测试结果: < 1s
机器打开Excel很正常,一点也不慢
代码很简单,就是我发的那个。就是用vs2008建个winform程序,然后引用excel组件,别的什么都没做。