添加"导出到Excel"按钮到网页导出gridview的web应用中创先争优 [英] Add an "Export to Excel" button to a webpage to export gridview to excel in webapplication

查看:121
本文介绍了添加"导出到Excel"按钮到网页导出gridview的web应用中创先争优的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为诊所病人管理软件,我需要从ASP.net网格视图出口patiet列表,excel文件

我的问题是:

有没有出口的GridView到Excel的方法
我使用vb.net和Visual Web Developer 2010

我存储从高级搜索页面的数据源到一个会话,然后重定向到结果页面
这里是结果页面的code

 部分类Sresults
继承System.Web.UI.Page保护小组的Page_Load(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Me.Load
    GridView1.DataSource =会议(d源)
    GridView1.DataBind()结束小组保护小组Backbtn_Click(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Backbtn.Click
    会议(d源)=
    的Response.Redirect(searchme.aspx)结束小组保护小组Page_ preRenderComplete(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手我。preRenderComplete
    的Response.Write(GridView1.Rows.Count.ToString +记录)
结束小组
末级


解决方案

试试下面code点击按钮

  //获取数据表的DataGrid绑定到。
VAR的dataTable =(数据表)dataGrid.DataSource;//创建新ExcelFile。
变种EF =新ExcelFile();
//新的工作表添加到该文件。
VAR WS = ef.Worksheets.Add(dataTable.TableName);
//插入从数据表中的数据的工作表起始于细胞A1。
ws.InsertDataTable(dataTable中,A1,真);//流文件浏览器。
Response.Clear();
Response.ContentType =应用程序/ vnd.ms-EXCEL;
Response.AddHeader(内容处置,附件;文件名= Employee.xls);
ef.SaveXls(Response.OutputStream);
到Response.End();

i built a patient management software for a clinic and i need to export patiet list from ASP.net grid view to excel file

my question is:

Is there a way to export gridview to excel i am using vb.net and visual web developer 2010

i store datasource from advanced search page into a session and redirect to result page here is the code of result page

Partial Class Sresults
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    GridView1.DataSource = Session("dsource")
    GridView1.DataBind()

End Sub

Protected Sub Backbtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Backbtn.Click
    Session("dsource") = ""
    Response.Redirect("searchme.aspx")

End Sub

Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete
    Response.Write(GridView1.Rows.Count.ToString + " Records")
End Sub
End Class

解决方案

try below code on the button click

// Get DataTable that DataGrid is bound to.
var dataTable = (DataTable)dataGrid.DataSource;

// Create new ExcelFile.
var ef = new ExcelFile();
// Add new worksheet to the file.
var ws = ef.Worksheets.Add(dataTable.TableName);
// Insert the data from DataTable to the worksheet starting at cell "A1".
ws.InsertDataTable(dataTable, "A1", true);

// Stream file to browser.
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.xls");
ef.SaveXls(Response.OutputStream);
Response.End();

这篇关于添加"导出到Excel"按钮到网页导出gridview的web应用中创先争优的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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