使用保存文件对话框将Datagridview导出到excel [英] Datagridview export to excel using savefiledialog

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

问题描述

您好,有人可以帮助我将datagridview导出到excel吗?我想以特定格式在excel中显示,例如datagridview中的每一行都应在excel中显示为列.文件名不应在后面的代码中定义.用户应该能够在保存文件对话框中动态定义.谁能帮助我提供非常简单的代码段.我使用Microsoft.Office.Interop.Excel作为参考
在此先感谢

hi can anyone help me in exporting a datagridview to an excel. I want to display in excel in specific format, like each row in datagridview should be displayed as columns in excel. File name should not be defined in the code behind. The user should be able to dynamically defined in the save file dialog box. Can anyone help me in code snippet which is quite simple. I used Microsoft.Office.Interop.Excel as reference
Thanks in advance

推荐答案

这种方法原则上是错误的. 请勿将任何控件导出到任何文档;导出数据.

您应该有一个独特的数据层.数据层可以绑定到UI.如果不使用正式绑定,则想法是相同的,只是手动"实现:将数据填充到数据网格中,使用数据网格中的事件来使数据层无效/更新.这样,您就可以支持UI控件和数据之间的同步.

如果以此方式完成操作,则始终在数据层而不是UI控件上始终使用数据持久性,数据导出/导入,报告,打印和类似功能.

我建议您学习并分析以下建筑模式的适用性( http://en.wikipedia .org/wiki/Architectural_pattern_(computer_science) [^ ]):

MVVM —模型视图视图模型,
http://en.wikipedia.org/wiki/Model_View_ViewModel [> http://en.wikipedia.org/wiki/Model-view-controller [ ^ ])),

MVA —模型视图适配器,
http://en.wikipedia.org/wiki/Model–view–adapter [ ^ ],

MVP —模型视图呈现器,
> http://en.wikipedia.org/wiki/Model-view-presenter [ ^ ].
请注意这些架构的动机.如果您了解它,就可以提出更好的设计思路.

—SA
This approach is wrong in principle. Don''t export any controls to any documents; export data.

You should have a distinct data layer. Data layer can be bound to UI. If you don''t use formal binding, the idea is the same, only implemented "manually": you populate data to you data grid, use events in your data grid to invalidate/update the data layer. In this way, you support synchronization between UI controls and data.

If this is done this way, you always use data persistence, data export/import, reporting, printing and similar functionality on data layer and never on UI controls.

I suggest you learn and analyze applicability of the following architectural patterns (http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[^]):

MVVM — Model View View Model,
http://en.wikipedia.org/wiki/Model_View_ViewModel[^],

MVC — Model-View-Controller,
http://en.wikipedia.org/wiki/Model-view-controller[^]),

MVA — Model-View-Adapter,
http://en.wikipedia.org/wiki/Model–view–adapter[^],

MVP — Model-View-Presenter,
http://en.wikipedia.org/wiki/Model-view-presenter[^].
Pay attention for the motivation of those architectures. If you understand it, you would be able to create better design ideas.

—SA


请使用以下代码行导出到Excel,

字符串附件=附件;文件名= Test.xls";

Response.ClearContent();

Response.AddHeader("content-disposition",附件);

Response.ContentType ="application/ms-excel";

StringWriter sw = new StringWriter();

HtmlTextWriter htw =新的HtmlTextWriter(sw);

GridView1.RenderControl(htw);

Response.Write(sw.ToString());

Response.End();
Please use the following code lines to export to Excel,

string attachment = "attachment; filename=Test.xls";

Response.ClearContent();

Response.AddHeader("content-disposition", attachment);

Response.ContentType = "application/ms-excel";

StringWriter sw = new StringWriter();

HtmlTextWriter htw = new HtmlTextWriter(sw);

GridView1.RenderControl(htw);

Response.Write(sw.ToString());

Response.End();


这篇关于使用保存文件对话框将Datagridview导出到excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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