DaGridView导出Excel

首先添加引用Microsoft.Office.Interop.Excel;

        /// <summary>
        /// 将dataGridView导出Excel
        /// </summary>
        /// <param name="dataGridView">dataGridView</param>
        /// <returns>返回是否导出成功,成功返回True,失败返回False</returns>
        public bool ExportExcel(DataGridView dataGridView)
        {
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = "Excel files(*.xls)|*.xls";
            saveDialog.FilterIndex = 0;
            saveDialog.RestoreDirectory = true;
            saveDialog.CreatePrompt = true;

            bool fileSaved = false;

            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                if (dataGridView == null)
                {
                    return false;
                }

                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

                if (xlApp == null)
                {
                    return false;
                }

                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];

                for (int i = 0; i < dataGridView.Columns.Count; i++)
                {
                    worksheet.Cells[1, i + 1] = dataGridView.Columns[i].HeaderText;
                }

                for (int r = 0; r < dataGridView.Rows.Count; r++)
                {
                    for (int i = 0; i < dataGridView.Columns.Count; i++)
                    {
                        worksheet.Cells[r + 2, i + 1] = dataGridView.Rows[r].Cells[i].Value.ToString();
                    }
                }

                try
                {
                    workbook.Saved = true;
                    workbook.SaveCopyAs(saveDialog.FileName);
                    fileSaved = true;
                }
                catch (Exception)
                {
                    fileSaved = false;
                }

                if (worksheet != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
                    worksheet = null;
                }
                if (workbook != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
                    workbook = null;
                }
                if (workbooks != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
                    workbooks = null;
                }
                xlApp.Application.Workbooks.Close();
                xlApp.Quit();
                if (xlApp != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                    xlApp = null;
                }
                GC.Collect();
            }
            return fileSaved;
        }

posted @ 2012-11-02 10:27  Wico  阅读(258)  评论(5编辑  收藏  举报