有两种方式导出数据,都可以实现Gridview导出excel,如果Gridview是分页的,解决了只能导出当前页的问题

第一种

例: 在A.aspx页面上 放入Gridview 在导出excel的button的单击事件里不做处理只调转到另外一个页面,这么做是为了导出所有数据不止是当前页的

例如

 protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("B.aspx?startTime=" + startTime + "&endTime=" + endTime + "&userID=" + userID);
}

在B页面的aspx页面里 拖入一个Gridview,把不需要的属性都可以不设置,这里的Gridview就是为了承载数据用的 注意这里是AllowPaging="false"

例如:

   <asp:GridView ID="gvUserInfo" runat="server" AutoGenerateColumns="False"
EmptyDataText="还没有任何信息" Width="100%" AllowPaging="false"
Font-Size="12px"   HeaderStyle-BackColor="#e9f3d6"
PagerStyle-BackColor="#e9f3d6" HeaderStyle-ForeColor="#5c9948">
<Columns>
<asp:BoundField HeaderText="用户名" DataField="userName" />
<asp:BoundField HeaderText="编号" DataField="userCode" />
<asp:BoundField HeaderText="年龄" DataField="userAge" />
</Columns>
</asp:GridView>

再在B的页面的.cs编写方法方法 如下

        private void Export()
{
//request接收传过来的参数
string startTime = Request["StartTime"]; ;
string endTime = Request["EndTime"];
string userID = Request["UserID"];
//业务逻辑处理
//IList<UserInfo> list = new List<UserInfo>();
//list = blldao.GetUserInfolist(userID);
//返回结果集
//gvUserInfo.DataSource = list;
//gvUserInfo.DataBind();
//下边就是导出的部分
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc    .xls    .txt   .htm
Response.AppendHeader("Content-Disposition", "attachment;filename=1.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档
Response.ContentType = "application/ms-excel";
}

此方法在Page_Load里运行

 protected void Page_Load(object sender, EventArgs e)
{
Export();
}

还有另外一种方法是不需要中间处理页面的

需要添加AppLibrary.dll 这个包在我的资源里边,方法如下 此方法在导出按钮里调用即可

        public void OutDataToExcel(System.Web.UI.WebControls.GridView gridview, IList<UserInfo> list)
{
HttpContext hc = HttpContext.Current;
ArrayList al = new ArrayList();
int count = gridview.Columns.Count;
for (int i = 0; i < count; i++)
{
string t = gridview.Columns[i].HeaderText;
al.Add(t);
}
AppLibrary.WriteExcel.XlsDocument doc = new AppLibrary.WriteExcel.XlsDocument();
doc.FileName = DateTime.Now.ToString("yyyyMMddHHMMss") + ".xls";
string SheetName = string.Empty;
SheetName = "数据查询";
AppLibrary.WriteExcel.Worksheet sheet = doc.Workbook.Worksheets.Add(SheetName);
AppLibrary.WriteExcel.Cells cells = sheet.Cells;
#region 第一个样式
AppLibrary.WriteExcel.XF XFstyle = doc.NewXF();//添加样式的
XFstyle.HorizontalAlignment = AppLibrary.WriteExcel.HorizontalAlignments.Centered;
XFstyle.Font.FontName = "宋体";//字体
XFstyle.UseMisc = true;
XFstyle.TextDirection = AppLibrary.WriteExcel.TextDirections.LeftToRight;//文本位置
XFstyle.Font.Bold = false;//加粗
#region 边框线的样式
XFstyle.BottomLineStyle = 1;
XFstyle.LeftLineStyle = 1;
XFstyle.TopLineStyle = 1;
XFstyle.RightLineStyle = 1;
#endregion
XFstyle.UseBorder = true;
XFstyle.PatternBackgroundColor = AppLibrary.WriteExcel.Colors.Blue;
XFstyle.PatternColor = AppLibrary.WriteExcel.Colors.White;
XFstyle.Pattern = 1;
#region 宽度
AppLibrary.WriteExcel.ColumnInfo colInfo = new AppLibrary.WriteExcel.ColumnInfo(doc, sheet);
colInfo.ColumnIndexStart = 0;
colInfo.ColumnIndexEnd = 4;
colInfo.Width = 10 * 256;
sheet.AddColumnInfo(colInfo);
AppLibrary.WriteExcel.ColumnInfo colInfo1 = new AppLibrary.WriteExcel.ColumnInfo(doc, sheet);
colInfo1.ColumnIndexStart = 5;
colInfo1.ColumnIndexEnd = 7;
colInfo1.Width = 15 * 256;
sheet.AddColumnInfo(colInfo1);
AppLibrary.WriteExcel.ColumnInfo colInfo2 = new AppLibrary.WriteExcel.ColumnInfo(doc, sheet);
colInfo2.ColumnIndexStart = 8;
colInfo2.ColumnIndexEnd = 9;
colInfo2.Width = 15 * 256;
sheet.AddColumnInfo(colInfo2);
AppLibrary.WriteExcel.ColumnInfo colInfo3 = new AppLibrary.WriteExcel.ColumnInfo(doc, sheet);
colInfo3.ColumnIndexStart = 12;
colInfo3.ColumnIndexEnd = 12;
colInfo3.Width = 15 * 256;
sheet.AddColumnInfo(colInfo3);
AppLibrary.WriteExcel.ColumnInfo colInfo4 = new AppLibrary.WriteExcel.ColumnInfo(doc, sheet);
colInfo4.ColumnIndexStart = 13;
colInfo4.ColumnIndexEnd = 13;
colInfo4.Width = 50 * 256;
sheet.AddColumnInfo(colInfo4);
AppLibrary.WriteExcel.ColumnInfo colInfo5 = new AppLibrary.WriteExcel.ColumnInfo(doc, sheet);
colInfo5.ColumnIndexStart = 14;
colInfo5.ColumnIndexEnd = 14;
colInfo5.Width = 15 * 256;
sheet.AddColumnInfo(colInfo5);
AppLibrary.WriteExcel.ColumnInfo colInfo6 = new AppLibrary.WriteExcel.ColumnInfo(doc, sheet);
colInfo6.ColumnIndexStart = 15;
colInfo6.ColumnIndexEnd = 15;
colInfo6.Width = 50 * 256;
sheet.AddColumnInfo(colInfo6);
#endregion
#endregion
#region 添加表头
int idx = 1;
for (int i = 0; i < al.Count; i++)
{
cells.Add(1, idx, al[i], XFstyle);
idx++;
}
#endregion
int f = 2;//从第二行开始填充数据
foreach (UserInfo m in list)
{
cells.Add(f, 1, m.userName, XFstyle);
cells.Add(f, 2, m.userCode, XFstyle);
cells.Add(f, 3, m.userAge, XFstyle);
cells.Add(f, 5, float.Parse(m.sHTMoney).ToString("f0"), XFstyle);
cells.Add(f, 6, m.PayResult == "000" ? "成功" : "失败", XFstyle);
cells.Add(f, 7, m.CreateTime.ToString(), XFstyle);
f++;
}
doc.Send();
hc.Response.Flush();
hc.Response.End();
}   

Gridview 导出excel,解决了只能导出当前页的问题相关推荐

  1. npoi的mvc怎么ajax导出,asp.net mvc利用NPOI导入导出Excel解决方法

    asp.net mvc利用NPOI导入导出Excel 导出Excel 2003没有问题,导出Excel2007老是出现无法访问已关闭的流,请帮忙解决,或是哪位有mvc导入导出excel的工具类能提供, ...

  2. asp.net中使用excel类导出Excel文件,并导出到web客户端中遇到的问题

    asp.net中使用excel类导出Excel文件,并导出到web客户端中遇到错误: 检索Com类工厂中CLSID为{000245-0000-0000-C000-000000000046}的组件失败, ...

  3. java利用poi导出excel功能-附带图片导出

    java利用poi导出excel功能-附带图片导出 写在前面 最近刚离职,闲来无事,于是把上两家公司都有碰到过的需求但都没有去研究实现:即导出带图片的excel报表.于是就折腾了一下这个功能,研究出来 ...

  4. html导出excel 内存不足,web导出excel那些坑

    背景介绍 昨天在一个前端的微信公众号看到一篇文章介绍导出cvs文件的,想想之前做导出excel的踩过的坑,心想记录一下,或许可以帮助别人吧! 需求很简单,在某个报表页面需要把table导出excel. ...

  5. java导出excel数字格式_POI 导出excel带小数点的数字格式显示不对解决方法

    最近看到了一个问题就是java导出excel中带小数点的数字显示不对, 比如我想在excel中第一行显示:  3,000.0 但是在excle中导出的格式总是不带小数点 3000(非文本格式),而且也 ...

  6. php导出excel出现乱码,php导出数据到excel出现乱码的解决办法

    代码如下: 代码示例: /** * 导出数据到excel 解决乱码问题 * Edit www.# */ function xlsBOF() { echo pack("ssssss" ...

  7. C# 导出Excel解决Cannot get a text value from a numeric cell或者Cannot get a numeric value from a text cell

    C#开发导出Excel中遇到如下问题中的一种: Cannot get a text value from a numeric cell Cannot get a numeric value from ...

  8. c#后台如何导出excel到本地_C#导出EXCEL方法总结

    下面介绍下我根据网上学习C#中导出EXCEL的几种方法: 一.asp.net导出Excel 1.将整个html全部输出到Excel 此方法会将html中所有的内容,如按钮.表格.图片等全部输出 Vie ...

  9. JavaPOI导出Excel合集——Java导出全内容

    最近接手了一个需求,开启了一段苦逼的加班之路,也收获了不少,对于Excel导出的功能也更加熟悉了,接下来我就介绍一下我遇到的问题跟解决的方式. 问题一: EasyExcel在Linux环境导出失败. ...

最新文章

  1. Vue3.js 全局组价案例入门
  2. csi python 摄像头 树莓派_树莓派之摄像头和人脸识别
  3. 三种获取Class类型的实例的方法
  4. nginx header参数丢失_某Nginx后门分析与重现
  5. 指针的理解 -- java程序员学C语言日记二
  6. android引导用户打开位置权限_想升级 App?先要个权限吧!!!
  7. VCL组件之TStrings
  8. Tomcat 异常关闭排查
  9. 提取pdf文件文本:pdfparser与xpdf具体操作
  10. php hr系统,专业hr管理系统
  11. 苹果无线网服务器改什么速度快,iPhone网速慢怎么办?苹果手机如何配置DNS?
  12. 优酷上的cocos2d-x的演示视频
  13. Python 自动化:根据模板批量生成含指定数据的 word 文档
  14. 活动二维码怎么制作?如何将活动内容做成二维码图片?
  15. python数据库-1
  16. 专访:平安科技首席架构师金新明和他的程序人生
  17. Spark 应用监控告警-Graphite_exporter
  18. 【linux】【docker】docker私服安装
  19. VirtualBox免费虚拟机使用简介、3G无线上网的Virtualbox实现
  20. OpenStack Days China:华云数据CTO郑军分享OpenStack创新实践

热门文章

  1. java 苹果内支付_一款集成支付宝、微信支付的Java源码,支持安卓、苹果,maven项目可以看看。申精...
  2. 电脑蓝牙耳机默认声音过高的解决办法
  3. SpringBoot | :定时任务的使用
  4. 五一,期待一场这样的旅行,提前预祝Csdner五一快乐
  5. 力扣(LeetCode)36. 有效的数独(2022.02.05)
  6. Python使用Excel表格的学生成绩管理系统
  7. python学习——高级教程
  8. 从拉萨开车到珠峰,需要准备多少钱?线路规划,费用清单
  9. 大数据课程A2——VPN客户端安装使用
  10. Java中一个接口可以继承多个接口吗?接口继承接口使用extends关键字