参考网上的方法修改,1000条记录导出只要3S(1GRDM,C2.8CPU).
1.项目添加excel对象类库的引用,Microsoft Excel 11.0 object library(不同版本的Excel,类库不同,这是2003的)
2.代码

1 //-***************获取要写入excel的数据源***************
//-***************获取要写入excel的数据源***************
2 Dao model=new Dao();
            Dao model=new Dao();
3 DataTable dt=model.GetFileNameList(0,intPageSize,Convert.ToDateTime(dtPFrom.Value), Convert.ToDateTime(dtPTo.Value));//取得dataGrid绑定的DataSet
            DataTable dt=model.GetFileNameList(0,intPageSize,Convert.ToDateTime(dtPFrom.Value), Convert.ToDateTime(dtPTo.Value));//取得dataGrid绑定的DataSet
4 if(dt==null) return;
            if(dt==null) return;
5 DataGridTableStyle ts = dataGrid1.TableStyles[0];
            DataGridTableStyle ts = dataGrid1.TableStyles[0];
6
7
8 //-***************获取excel对象***************
            //-***************获取excel对象***************
9 string saveFileName="";
            string saveFileName="";
10 bool fileSaved=false;
            bool fileSaved=false;
11 SaveFileDialog saveDialog=new SaveFileDialog();
            SaveFileDialog saveDialog=new SaveFileDialog();
12 saveDialog.DefaultExt ="xls";
            saveDialog.DefaultExt ="xls";
13 saveDialog.Filter="Excel文件|*.xls";
            saveDialog.Filter="Excel文件|*.xls";
14 saveDialog.FileName ="导入记录查询结果 "+DateTime.Today.ToString("yyyy-MM-dd");
            saveDialog.FileName ="导入记录查询结果 "+DateTime.Today.ToString("yyyy-MM-dd");
15 saveDialog.ShowDialog();
            saveDialog.ShowDialog();
16 saveFileName=saveDialog.FileName;
            saveFileName=saveDialog.FileName;
17 if(saveFileName.IndexOf(":")<0) return; //被点了取消
            if(saveFileName.IndexOf(":")<0) return; //被点了取消
18 Excel.Application xlApp=new Excel.Application();
            Excel.Application xlApp=new Excel.Application();
19 if(xlApp==null)
            if(xlApp==null)
20 {
            {
21 MessageBox.Show("无法启动Excel,可能您的机子未安装Excel");
                MessageBox.Show("无法启动Excel,可能您的机子未安装Excel");
22 return;
                return;
23 }
            }
24 Excel.Workbook workbook = xlApp.Workbooks.Add(true);
            Excel.Workbook workbook = xlApp.Workbooks.Add(true);
25 Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
            Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];   
26 Excel.Range range;
            Excel.Range range;
27
28 
       
29 string oldCaption=dataGrid1.CaptionText;
            string oldCaption=dataGrid1.CaptionText;
30 // 列索引,行索引,总列数,总行数
            // 列索引,行索引,总列数,总行数                   
31 int colIndex = 0;
            int colIndex = 0;
32 int RowIndex = 0;
            int RowIndex = 0;
33 int colCount = ts.GridColumnStyles.Count;
            int colCount = ts.GridColumnStyles.Count;
34 int RowCount=dt.Rows.Count;
            int RowCount=dt.Rows.Count;
35 
           
36
37 // *****************获取数据*********************
            // *****************获取数据*********************
38 dataGrid1.CaptionVisible = true;
            dataGrid1.CaptionVisible = true;
39 dataGrid1.CaptionText = "正在导出数据
            dataGrid1.CaptionText = "正在导出数据
 ";
";
40 // 创建缓存数据
            // 创建缓存数据
41 object[,] objData = new object[RowCount + 1, colCount];
            object[,] objData = new object[RowCount + 1, colCount];
42 // 获取列标题
            // 获取列标题
43 foreach(DataGridColumnStyle cs in ts.GridColumnStyles)
            foreach(DataGridColumnStyle cs in ts.GridColumnStyles)
44 {
            {
45 objData[RowIndex,colIndex++] = cs.HeaderText;
                objData[RowIndex,colIndex++] = cs.HeaderText;   
46 }
            }
47 
       
48 // 获取具体数据
            // 获取具体数据
49 for(RowIndex =1;RowIndex< RowCount;RowIndex++)
            for(RowIndex =1;RowIndex< RowCount;RowIndex++)
50 {
            {
51 for(colIndex=0;colIndex < colCount;colIndex++)
                for(colIndex=0;colIndex < colCount;colIndex++)
52 {
                {               
53 objData[RowIndex,colIndex] =dt.Rows[RowIndex-1][colIndex+1];
                    objData[RowIndex,colIndex] =dt.Rows[RowIndex-1][colIndex+1];
54 }
                }
55 
               
56 }
            }   
57
58 //********************* 写入Excel*******************
           //********************* 写入Excel*******************
59 
               
60 range = worksheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[RowCount,colCount]);
            range = worksheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[RowCount,colCount]);           
61 range.Value2= objData;
            range.Value2= objData;            
62 Application.DoEvents();
            Application.DoEvents();   
63 
           
64 //*******************设置输出格式******************************
            //*******************设置输出格式******************************
65 
             
66 //设置顶部説明
            //设置顶部説明   
67 range = worksheet.get_Range(xlApp.Cells[1,1],xlApp.Cells[1,colCount]);
            range = worksheet.get_Range(xlApp.Cells[1,1],xlApp.Cells[1,colCount]);
68 range.MergeCells = true;
            range.MergeCells = true;
69 range.RowHeight=38;
            range.RowHeight=38;
70 range.Font.Bold=true;
            range.Font.Bold=true;
71 range.Font.Size=14;
            range.Font.Size=14;
72 range.Font.ColorIndex=10;//字体颜色
            range.Font.ColorIndex=10;//字体颜色
73 xlApp.ActiveCell.FormulaR1C1 = "导入记录查询结果";
            xlApp.ActiveCell.FormulaR1C1 = "导入记录查询结果";
74
75 //特殊数字格式
            //特殊数字格式
76 range = worksheet.get_Range(xlApp.Cells[2,colCount],xlApp.Cells[RowCount,colCount]);
            range = worksheet.get_Range(xlApp.Cells[2,colCount],xlApp.Cells[RowCount,colCount]);
77 range.NumberFormat="yyyy-MM-dd hh:mm:ss";
            range.NumberFormat="yyyy-MM-dd hh:mm:ss";
78
79 xlApp.Cells.HorizontalAlignment=Excel.Constants.xlCenter;
            xlApp.Cells.HorizontalAlignment=Excel.Constants.xlCenter;   
80 range = worksheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[2,colCount]);
            range = worksheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[2,colCount]);
81 range.Interior.ColorIndex = 10;//背景色
            range.Interior.ColorIndex = 10;//背景色
82 range.Font.Bold = true;
            range.Font.Bold = true;
83 range.RowHeight=20;
            range.RowHeight=20;
84 ((Excel.Range)worksheet.Cells[2,1]).ColumnWidth=25;
            ((Excel.Range)worksheet.Cells[2,1]).ColumnWidth=25;
85 ((Excel.Range)worksheet.Cells[2,2]).ColumnWidth=13;
            ((Excel.Range)worksheet.Cells[2,2]).ColumnWidth=13;
86 ((Excel.Range)worksheet.Cells[2,3]).ColumnWidth=18;
            ((Excel.Range)worksheet.Cells[2,3]).ColumnWidth=18;
87 ((Excel.Range)worksheet.Cells[2,4]).ColumnWidth=15;
            ((Excel.Range)worksheet.Cells[2,4]).ColumnWidth=15;
88 ((Excel.Range)worksheet.Cells[2,5]).ColumnWidth=22;
            ((Excel.Range)worksheet.Cells[2,5]).ColumnWidth=22;
89
90 //***************************保存**********************
            //***************************保存**********************
91 dataGrid1.CaptionVisible = false;
            dataGrid1.CaptionVisible = false;
92 dataGrid1.CaptionText = oldCaption;
            dataGrid1.CaptionText = oldCaption; 
93 if(saveFileName!="")
            if(saveFileName!="")
94 {
            {
95 try
                try
96 {
                {
97 workbook.Saved =true;
                    workbook.Saved =true;
98 workbook.SaveCopyAs(saveFileName);
                    workbook.SaveCopyAs(saveFileName);
99 fileSaved=true;
                    fileSaved=true;
100 }
                }
101 catch(Exception ex)
                catch(Exception ex)
102 {
                {
103 fileSaved=false;
                    fileSaved=false;
104 MessageBox.Show("导出文件时出错,文件可能正被打开!\n"+ex.Message);
                    MessageBox.Show("导出文件时出错,文件可能正被打开!\n"+ex.Message);
105 }
                }
106 }
            }
107 else
            else
108 {
            {
109 fileSaved=false;
                fileSaved=false;
110 }
            }
111 xlApp.Quit();
            xlApp.Quit();
112 GC.Collect();//强行销毁
            GC.Collect();//强行销毁   
113 TimeSpan dateEnd=new TimeSpan(DateTime.Now.Ticks);
            TimeSpan dateEnd=new TimeSpan(DateTime.Now.Ticks);           
114 TimeSpan tspan=dateBegin.Subtract(dateEnd).Duration();
            TimeSpan tspan=dateBegin.Subtract(dateEnd).Duration();
115 MessageBox.Show(tspan.ToString());
            MessageBox.Show(tspan.ToString());
116 if(fileSaved && File.Exists(saveFileName))
            if(fileSaved && File.Exists(saveFileName))
117 System.Diagnostics.Process.Start(saveFileName);
                System.Diagnostics.Process.Start(saveFileName); 
1.项目添加excel对象类库的引用,Microsoft Excel 11.0 object library(不同版本的Excel,类库不同,这是2003的)
2.代码

1
 //-***************获取要写入excel的数据源***************
//-***************获取要写入excel的数据源***************2
 Dao model=new Dao();
            Dao model=new Dao();3
 DataTable dt=model.GetFileNameList(0,intPageSize,Convert.ToDateTime(dtPFrom.Value), Convert.ToDateTime(dtPTo.Value));//取得dataGrid绑定的DataSet
            DataTable dt=model.GetFileNameList(0,intPageSize,Convert.ToDateTime(dtPFrom.Value), Convert.ToDateTime(dtPTo.Value));//取得dataGrid绑定的DataSet4
 if(dt==null) return;
            if(dt==null) return;5
 DataGridTableStyle ts = dataGrid1.TableStyles[0];
            DataGridTableStyle ts = dataGrid1.TableStyles[0];6

7

8
 //-***************获取excel对象***************
            //-***************获取excel对象***************9
 string saveFileName="";
            string saveFileName="";10
 bool fileSaved=false;
            bool fileSaved=false;11
 SaveFileDialog saveDialog=new SaveFileDialog();
            SaveFileDialog saveDialog=new SaveFileDialog();12
 saveDialog.DefaultExt ="xls";
            saveDialog.DefaultExt ="xls";13
 saveDialog.Filter="Excel文件|*.xls";
            saveDialog.Filter="Excel文件|*.xls";14
 saveDialog.FileName ="导入记录查询结果 "+DateTime.Today.ToString("yyyy-MM-dd");
            saveDialog.FileName ="导入记录查询结果 "+DateTime.Today.ToString("yyyy-MM-dd");15
 saveDialog.ShowDialog();
            saveDialog.ShowDialog();16
 saveFileName=saveDialog.FileName;
            saveFileName=saveDialog.FileName;17
 if(saveFileName.IndexOf(":")<0) return; //被点了取消
            if(saveFileName.IndexOf(":")<0) return; //被点了取消18
 Excel.Application xlApp=new Excel.Application();
            Excel.Application xlApp=new Excel.Application();19
 if(xlApp==null)
            if(xlApp==null)20
 {
            {21
 MessageBox.Show("无法启动Excel,可能您的机子未安装Excel");
                MessageBox.Show("无法启动Excel,可能您的机子未安装Excel");22
 return;
                return;23
 }
            }24
 Excel.Workbook workbook = xlApp.Workbooks.Add(true);
            Excel.Workbook workbook = xlApp.Workbooks.Add(true);25
 Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
            Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];   26
 Excel.Range range;
            Excel.Range range;27

28
 
       29
 string oldCaption=dataGrid1.CaptionText;
            string oldCaption=dataGrid1.CaptionText;30
 // 列索引,行索引,总列数,总行数
            // 列索引,行索引,总列数,总行数                   31
 int colIndex = 0;
            int colIndex = 0;32
 int RowIndex = 0;
            int RowIndex = 0;33
 int colCount = ts.GridColumnStyles.Count;
            int colCount = ts.GridColumnStyles.Count;34
 int RowCount=dt.Rows.Count;
            int RowCount=dt.Rows.Count;35
 
           36

37
 // *****************获取数据*********************
            // *****************获取数据*********************38
 dataGrid1.CaptionVisible = true;
            dataGrid1.CaptionVisible = true;39
 dataGrid1.CaptionText = "正在导出数据
            dataGrid1.CaptionText = "正在导出数据
 ";
";40
 // 创建缓存数据
            // 创建缓存数据41
 object[,] objData = new object[RowCount + 1, colCount];
            object[,] objData = new object[RowCount + 1, colCount];42
 // 获取列标题
            // 获取列标题43
 foreach(DataGridColumnStyle cs in ts.GridColumnStyles)
            foreach(DataGridColumnStyle cs in ts.GridColumnStyles)44
 {
            {45
 objData[RowIndex,colIndex++] = cs.HeaderText;
                objData[RowIndex,colIndex++] = cs.HeaderText;   46
 }
            }47
 
       48
 // 获取具体数据
            // 获取具体数据49
 for(RowIndex =1;RowIndex< RowCount;RowIndex++)
            for(RowIndex =1;RowIndex< RowCount;RowIndex++)50
 {
            {51
 for(colIndex=0;colIndex < colCount;colIndex++)
                for(colIndex=0;colIndex < colCount;colIndex++)52
 {
                {               53
 objData[RowIndex,colIndex] =dt.Rows[RowIndex-1][colIndex+1];
                    objData[RowIndex,colIndex] =dt.Rows[RowIndex-1][colIndex+1];54
 }
                }55
 
               56
 }
            }   57

58
 //********************* 写入Excel*******************
           //********************* 写入Excel*******************59
 
               60
 range = worksheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[RowCount,colCount]);
            range = worksheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[RowCount,colCount]);           61
 range.Value2= objData;
            range.Value2= objData;            62
 Application.DoEvents();
            Application.DoEvents();   63
 
           64
 //*******************设置输出格式******************************
            //*******************设置输出格式******************************65
 
             66
 //设置顶部説明
            //设置顶部説明   67
 range = worksheet.get_Range(xlApp.Cells[1,1],xlApp.Cells[1,colCount]);
            range = worksheet.get_Range(xlApp.Cells[1,1],xlApp.Cells[1,colCount]);68
 range.MergeCells = true;
            range.MergeCells = true;69
 range.RowHeight=38;
            range.RowHeight=38;70
 range.Font.Bold=true;
            range.Font.Bold=true;71
 range.Font.Size=14;
            range.Font.Size=14;72
 range.Font.ColorIndex=10;//字体颜色
            range.Font.ColorIndex=10;//字体颜色73
 xlApp.ActiveCell.FormulaR1C1 = "导入记录查询结果";
            xlApp.ActiveCell.FormulaR1C1 = "导入记录查询结果";74

75
 //特殊数字格式
            //特殊数字格式76
 range = worksheet.get_Range(xlApp.Cells[2,colCount],xlApp.Cells[RowCount,colCount]);
            range = worksheet.get_Range(xlApp.Cells[2,colCount],xlApp.Cells[RowCount,colCount]);77
 range.NumberFormat="yyyy-MM-dd hh:mm:ss";
            range.NumberFormat="yyyy-MM-dd hh:mm:ss";78

79
 xlApp.Cells.HorizontalAlignment=Excel.Constants.xlCenter;
            xlApp.Cells.HorizontalAlignment=Excel.Constants.xlCenter;   80
 range = worksheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[2,colCount]);
            range = worksheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[2,colCount]);81
 range.Interior.ColorIndex = 10;//背景色
            range.Interior.ColorIndex = 10;//背景色82
 range.Font.Bold = true;
            range.Font.Bold = true;83
 range.RowHeight=20;
            range.RowHeight=20;84
 ((Excel.Range)worksheet.Cells[2,1]).ColumnWidth=25;
            ((Excel.Range)worksheet.Cells[2,1]).ColumnWidth=25;85
 ((Excel.Range)worksheet.Cells[2,2]).ColumnWidth=13;
            ((Excel.Range)worksheet.Cells[2,2]).ColumnWidth=13;86
 ((Excel.Range)worksheet.Cells[2,3]).ColumnWidth=18;
            ((Excel.Range)worksheet.Cells[2,3]).ColumnWidth=18;87
 ((Excel.Range)worksheet.Cells[2,4]).ColumnWidth=15;
            ((Excel.Range)worksheet.Cells[2,4]).ColumnWidth=15;88
 ((Excel.Range)worksheet.Cells[2,5]).ColumnWidth=22;
            ((Excel.Range)worksheet.Cells[2,5]).ColumnWidth=22;89

90
 //***************************保存**********************
            //***************************保存**********************91
 dataGrid1.CaptionVisible = false;
            dataGrid1.CaptionVisible = false;92
 dataGrid1.CaptionText = oldCaption;
            dataGrid1.CaptionText = oldCaption; 93
 if(saveFileName!="")
            if(saveFileName!="")94
 {
            {95
 try
                try96
 {
                {97
 workbook.Saved =true;
                    workbook.Saved =true;98
 workbook.SaveCopyAs(saveFileName);
                    workbook.SaveCopyAs(saveFileName);99
 fileSaved=true;
                    fileSaved=true;100
 }
                }101
 catch(Exception ex)
                catch(Exception ex)102
 {
                {103
 fileSaved=false;
                    fileSaved=false;104
 MessageBox.Show("导出文件时出错,文件可能正被打开!\n"+ex.Message);
                    MessageBox.Show("导出文件时出错,文件可能正被打开!\n"+ex.Message);105
 }
                }106
 }
            }107
 else
            else108
 {
            {109
 fileSaved=false;
                fileSaved=false;110
 }
            }111
 xlApp.Quit();
            xlApp.Quit();112
 GC.Collect();//强行销毁
            GC.Collect();//强行销毁   113
 TimeSpan dateEnd=new TimeSpan(DateTime.Now.Ticks);
            TimeSpan dateEnd=new TimeSpan(DateTime.Now.Ticks);           114
 TimeSpan tspan=dateBegin.Subtract(dateEnd).Duration();
            TimeSpan tspan=dateBegin.Subtract(dateEnd).Duration();115
 MessageBox.Show(tspan.ToString());
            MessageBox.Show(tspan.ToString());116
 if(fileSaved && File.Exists(saveFileName))
            if(fileSaved && File.Exists(saveFileName))117
 System.Diagnostics.Process.Start(saveFileName);
                System.Diagnostics.Process.Start(saveFileName);