C#Winform数据导出在Excel文档中显示

1,项目展示,将datagrid中的数据导出

2,点击导出数据 

 3,代码展示   导出数据按钮事件如下

 //导出
 private void tsbtnUpload_Click(object sender, EventArgs e)
 {
     List<GoldPlate> list = dgvGoldList.DataSource as List<GoldPlate>;
     string fileName = "金板预警统计";
     FormUtility.DataToExcel<GoldPlate>(list, dgvGoldList.Columns, fileName + ".xlsx", fileName, fileName, "导出金板预警明细数据");
 }

4,导出数据的具体方法如下

    public static void DataToExcel<T>(List<T> list, DataGridViewColumnCollection cols, string fileName, string sheetName, string msg, string title)
    {
        if (list != null && list.Count > 0)
        {
            string filePath = "";
            FolderBrowserDialog fbdChoose = new FolderBrowserDialog();
            if (fbdChoose.ShowDialog() == DialogResult.OK)
            {
                filePath = fbdChoose.SelectedPath;
            }
            if (string.IsNullOrEmpty(filePath))
            {
                MessageBox.Show("请选择导出的位置!");
                return;
            }
            if (filePath.LastIndexOf('/') != filePath.Length - 1)
                filePath += "/";
            Dictionary<string, string> colsName = new Dictionary<string, string>();
            foreach (DataGridViewColumn dc in cols)
            {
                colsName.Add(dc.Name, dc.HeaderText);
            }
            int count = ExcelHelper.ListToExcel<T>(list, filePath + fileName, sheetName, colsName);
            if (count > 0)
                MessageBox.Show(title, msg + "数据导出成功!");
            else
            {
                MessageBox.Show(msg + "数据导出失败!");
            }
        }
        else
        {
            MessageBox.Show("没有数据可导出!");
            return;
        }
    }

以上内容就是数据导出功能,下次更新DevExpress数据导出功能下图就是dev数据导出界面比较有动感。

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值