就是按模板导出:http://download.csdn.net/detail/flag5418/1573880

解决方案 »

  1.   


     /*
                         * 获取保存EXCEL的路径
                         */
                        saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
                        saveFileDialog.FilterIndex = 0;
                        saveFileDialog.RestoreDirectory = true;
                        saveFileDialog.CreatePrompt = true;
                        saveFileDialog.Title = "导出文件保存路径";
                        if (saveFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            //strName储存保存EXCEL路径
                            string strName = saveFileDialog.FileName;
                            if (strName.Length != 0)
                            {
                                System.Reflection.Missing miss = System.Reflection.Missing.Value;
                                Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
                                excel.Application.Workbooks.Add(true); ;
                                //若是true,则在导出的时候会显示EXcel界面。
                                excel.Visible = false;
                                if (excel == null)
                                {
                                    MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                                Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
                                Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
                                Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
                                 //生成字段名称,逐条写,无效率
                                excel.Cells.Select();
                           
                                for (int i = 1; i < gridView.ColumnCount; i++)
                                {
                                    excel.Cells[2, i + 1] = gridView.Columns[i].HeaderText.ToString();
                                }
                                //以下为填充数据关键代码,逐条写,无效率
                                for (int i = 0; i < gridView.RowCount; i++)
                                {
                                    for (int j = 0; j < gridView.ColumnCount; j++)
                                    {
                                        Range contentRange = excel.Cells[i + 3, j + 1] as Range;//获取单元格
                                        if (gridView[j, i].Value == typeof(string))
                                        {
                                            excel.Cells[i + 3, j + 1] = "" + gridView[i, j].Value.ToString();
                                        }
                                        else
                                        {
                                            excel.Cells[i + 3, j + 1] = gridView[j, i].Value.ToString();
                                        }
                                    }                                 System.Windows.Forms.Application.DoEvents();                            }                             sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
                                book.Close(false, miss, miss);
                                books.Close();
                                excel.Quit();
                                System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
                                System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
                                System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
                                System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
                                GC.Collect();
                            }
                        }