将datagridview的行导出到excel [英] Exporting rows of datagridview to excel

查看:68
本文介绍了将datagridview的行导出到excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了datagridview,并用数据集填充它,该数据集返回两列ID和Name,但是在gridview中,我只显示name列,而隐藏ID列.是否可以将行单独从datagridview导出到excel.例如,如果遵循.名称显示在每一行中,
A
B
C
D

我的Excel应该采用每行都必须显示为列的格式

A B C D

我尝试导出的代码

I am using a datagridview in my application and I have filled it with dataset which returns two columns ID and Name, but in gridview I''m displaying the name column alone hiding the ID column. Is it possible to export the rows alone from datagridview to excel. Eg If the follow. names appear as each row,
A
B
C
D

My excel should be in a format that each row must display as a column

A B C D

Code Which I have tried for export

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.Filter = "Excel files |*.xls|All files (*.*)|*.*";
                saveFileDialog1.FilterIndex = 2;
                saveFileDialog1.RestoreDirectory = true;
                saveFileDialog1.CreatePrompt = true;
                saveFileDialog1.Title = "Export Excel File To";
                //saveFileDialog1.ShowDialog();
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {

                    worksheet = workbook.Sheets["Sheet1"];
                    worksheet = workbook.ActiveSheet;
                    worksheet.Name = "Exported from Data Template";
                    for (int i = 0; i < dgDtParameters.Rows.Count - 1; i++)
                    {
                        for (int j = 0; j < dgDtParameters.Columns.Count; j++)
                        {
                            worksheet.Cells[i + 2, j + 1] = dgDtParameters.Rows[i].Cells[j].Value.ToString();
                        }
                    }
                    fileName = saveFileDialog1.FileName;
                    workbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    MessageBox.Show("Data template exported successfully","Data Template Export);
                }
                else
                    return;

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                app.Quit();
                workbook = null;
                app = null;
                GC.Collect();
            }

推荐答案

我从字面上看的只是关于这10分钟的一篇文章,我有类似的问题(略有不同),对看看你的想法:

无需从数据库自动将数据导出到Excel,Word,PDF [ http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm [^ ]

这对我真的很有用,而且资源很好
I was literally just reading an article about this 10 minutes ago I had similar issues(slightly different) have a read of this and see what you think:

Export Data to Excel, Word, PDF without Automation from DataBase[^]

also if you type it into google there are a host of sites that a really really good for db to excel and things like that try this one also:

http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm[^]

It was really useful to me and a good resource


这篇关于将datagridview的行导出到excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆