码迷,mamicode.com
首页 > Windows程序 > 详细

Winform DataGridView导出Excel

时间:2015-07-13 17:50:02      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

 

     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);
        }

 

Winform DataGridView导出Excel

标签:

原文地址:http://www.cnblogs.com/xiaoqi742709106/p/4643123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!