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);
        }
时间: 2024-10-10 19:38:20

Winform DataGridView导出Excel的相关文章

C# Winform datagridview导出EXCEL

string saveFileName = ""; //bool fileSaved = false; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = "xls"; saveDialog.Filter = "Excel文件|*.xls"; saveDialog.ShowDialog(); saveFileName = saveDialog.File

Winform将DataGridView导出Excel,调用打印机打印DataGridView

DataGridView导出Excel (这个需要 Microsoft.Office.Interop.Excel.dll  在网上下载就可以) public static void ExportExcel(string fileName, DataGridView myDGV) { string saveFileName = ""; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = &qu

NPOI DataGridView导出EXCEL

NPOI 官方网站 http://npoi.codeplex.com/ 加载NPOI.DLL ,引用这两个命名空间 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; #region NPOI DataGridView 导出 EXCEL /// <summary> /// NPOI DataGridView 导出 EXCEL /// </summary> /// <param name="fileName"

从DataGridView导出Excel

从DataGridView导出Excel的两种情况,不多说,直接记录代码. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Data; namespace cnblogs { public static class LoadExcel { public sta

C#winform中datagridview导出Excel

只需要传入datagridview的name即可. 1 //导出Excel()方法 2 public void ToExcel(DataGridView dataGridView) 3 { 4 //实例化一个Excel.Application对象 5 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); 6 7 if (excel == null)

winform导入导出excel,后台动态添加控件

思路: 导入: 1,初始化一个OpenFileDialog类 (OpenFileDialog fileDialog = new OpenFileDialog();) 2, 获取用户选择文件的后缀名(string extension = Path.GetExtension(fileDialog.FileName).ToLower();),并设置允许后缀文件名: 3,NPOI转datetable,遍历tatetable转成实体类列表并入库: 导出: 1, 创建提示用户保存类,SaveFileDial

DataGridView导出Excel

把DataGridView中的信息导出到Excel是<机房收费系统>的功能之一,这里主要是VS与Office的交互,方法有n多种(从这里就可以看出这个功能难不到哪里去了),现在就介绍一下我使用的一种吧,首先需要添加并引用命名空间. 1.添加:右击窗体所在的项目名即U层的项目名--添加--引用--出现下面图片,搜索Excel,在结果中选中符合版本的Microsoft.Interop.Excel 2.引用命名空间:Imports Microsoft.Office.Interop 由于多次用到了导出

c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据

准备工作就是可以分页的DataGridView,和两个按钮,一个用来导出当前页数据到Excel,一个用来导出全部数据到Excel 没有使用SaveFileDialog,但却可以弹出保存对话框来 先做导出当前页数据到Excel的 DataGridView命名为dataGridView1 1 //按下导出按钮 2 private void button7_Click(object sender, EventArgs e) 3 { 4 print(dataGridView1); 5 } 6 publi

C# DataGridView导出Excel

using Microsoft.Office.Interop.Excel; using Excel=Microsoft.Office.Interop.Excel; //这里Excel和Interop报错,请参考我的另一篇http://blog.csdn.net/xtfnpgy/article/details/41076849 public bool ExportDataGridview(DataGridView gridView,string str_path) { if(gridView.Ro