Winform DataGridView导出Excel

 

     private void 导出excelToolStripMenuItem_Click(object sender, EventArgs e)

        {

            if (dgvData.RowCount > 0)

            {

                var saveFileDialog1 = new SaveFileDialog

                {

                    Filter = @"Excel文件(*.xls,*.xlsx)|*.xls;*.xlsx",

                    RestoreDirectory = true

                };



                if (saveFileDialog1.ShowDialog() == DialogResult.OK)

                {

                    ExportExcel(saveFileDialog1.FileName, dgvData, 2);

                }

            }

            else

            {

                MessageBox.Show(@"数据为空!!");

            }

        }



/// <summary>

        /// 导出Excel方法

        /// </summary>

        /// <param name="fileName">导出路径</param>

        /// <param name="gridView">数据源</param>

        /// <param name="num">从第几列开始导</param>

        private void ExportExcel(string fileName, DataGridView gridView, int num)

        {

            if (fileName.IndexOf(":") < 0) return; //被点了取消

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



            if (xlApp == null)

            {

                MessageBox.Show(@"无法创建Excel对象,可能您的机子未安装Excel");

                return;

            }

            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];//取得sheet1



            int a = 1, b = 1;

            for (var i = 0; i < gridView.RowCount; i++)

            {

                if (i == 0)

                {

                    for (var j = num; j < gridView.Columns.Count; j++)

                    {

                        worksheet.Cells[1, a] = gridView.Columns[j].HeaderText;

                        a++;

                    }

                }

                for (var j = num; j < gridView.Columns.Count; j++)

                {

                    worksheet.Cells[i + 2, b] = gridView.Rows[i].Cells[j].Value;

                    b++;

                }

                b = 1;

                System.Windows.Forms.Application.DoEvents();

            }



            worksheet.Columns.EntireColumn.AutoFit();//列宽自适应

            if (fileName != "")

            {

                try

                {

                    workbook.Saved = true;

                    workbook.SaveCopyAs(fileName);

                }

                catch (Exception ex)

                {

                    MessageBox.Show(@"文件导出时出错,文件可能正被打开!" + ex.Message);

                }

            }

            xlApp.Quit();

            GC.Collect();//强行销毁

            MessageBox.Show(@"文件导出成功!!", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

 

你可能感兴趣的:(datagridview)