星空网 > 软件开发 > ASP.net

winform 导出datagridview 到excel

 

winform 导出datagridview 到excelwinform 导出datagridview 到excelwinform 导出datagridview 到excel

 

数据不多可以用下面的方式方法,如果数据较大,不建议这样使用,可能会比较卡
如果电脑上没有Microsoft.Office.Interop.Excel.dll去找DLL下载站下载即可 
需要先导入这个dll的引用 

呈上代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using  System.Data.SqlClient;
namespace 导出到execl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        //绑定datagridview
        private void Bind()
        {
            string sql = string.Format("select id, no, name, age, gender, address from stuinfo");
            //getSet 方法返回的dataset所以要进行.Tables[0]
            this.dataGridView1.DataSource = SQLHelpercs.getSet(SQLHelpercs.ConString, sql).Tables[0]; ;
        }
 
        /// <summary>
        /// 导出excel 
        /// </summary>
        /// <param name="fileName">要保存excel的名称</param>
        /// <param name="dg">DataGridView 的 名称</param>
        public void OutExecl(string fileName, DataGridView dg)
        {
            if (dg.Rows.Count > 0)//判断datagridview是否有数据
            {
                string saveName = string.Empty;//声明一个保存名称
                SaveFileDialog sgfDialog=new SaveFileDialog();//创建一个保存对象
                sgfDialog.DefaultExt = "xls";//默认保存扩展名
                sgfDialog.Filter = "Excel文件|*.xls";//保存文件的类型
                sgfDialog.FileName = fileName;//保存的名称
                sgfDialog.ShowDialog();
                saveName = sgfDialog.FileName;
                if(saveName.IndexOf(":")<0)return;//点了取消
                Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();
                if (xlapp == null)
                {
                    MessageBox.Show("无法创建Execl!");
                    return;
                }
                Microsoft.Office.Interop.Excel.Workbooks wbs = xlapp.Workbooks;
                Microsoft.Office.Interop.Excel.Workbook workbook = wbs.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);  //添加工作薄
               Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1  
               //写入标题  
               for (int i = 0; i < dg.ColumnCount; i++)  
               {  
                   worksheet.Cells[1, i + 1] = dg.Columns[i].HeaderText;  
               }  
               //写入数值  
               for (int r = 0; r < dg.Rows.Count; r++)  
               {  
                   for (int i = 0; i < dg.ColumnCount; i++)  
                   {  
                       worksheet.Cells[r + 2, i + 1] = dg.Rows[r].Cells[i].Value;  
                   }  
                   System.Windows.Forms.Application.DoEvents();  
               }  
               worksheet.Columns.EntireColumn.AutoFit();//列宽自适应  
               
               if (saveName != "")  
               {  
                   try  
                   {  
                       workbook.Saved = true;  
                       workbook.SaveCopyAs(saveName);  
                       //fileSaved = true;  
                   }  
                   catch (Exception ex)  
                   {  
                       //fileSaved = false;  
                       MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);  
                   }  
               }  
               xlapp.Quit();  
               GC.Collect();//强行销毁   
               MessageBox.Show(fileName + "保存成功!", "提示", MessageBoxButtons.OK);  
           }  
           else  
           {  
               MessageBox.Show("报表为空,无表格需要导出","提示",MessageBoxButtons.OK);  
           } 

        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            Bind();
        }
 
        private void 导出到excelToolStripMenuItem_Click(object sender, EventArgs e)
        {
             OutExecl("cool.xls",this.dataGridView1);
        }
    }
}
 

 

 




原标题:winform 导出datagridview 到excel

关键词:GridView

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

利基产品介绍:https://www.ikjzd.com/w/1043
亚马逊APUK收款方式介绍:https://www.ikjzd.com/w/1044
宝视佳介绍:https://www.ikjzd.com/w/1045
浩方:https://www.ikjzd.com/w/1046
冠通分销平台介绍:https://www.ikjzd.com/w/1047
无货源亚马逊店群全面介绍:https://www.ikjzd.com/w/1048
深圳有没有比较好玩的景点 深圳有没有比较好玩的景点推荐一下:https://www.vstour.cn/a/366175.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流