ASP.NET 导出数据表格
功能:可以实现导出整个数据表格或整个页面
public bool ExportGv(string fileType, string fileName)
{
bool flag = false;
try
{
//定义文档类型、字符编码
Response.Clear();
Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
//2.定义导入文档的类型
HttpContext.Current.Response.ContentType = fileType;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=\"" +
System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
this.Gv.Page.EnableViewState = false;
//定义一个输入流
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
//将目标数据绑定到输入流输出,这里的this可以换成要导出的控件数据
this.RenderControl(hw);
Response.Output.Write(tw.ToString());
Response.Flush();
Response.End();
this.Gv.AllowPaging = false;
flag = true;
}
catch (Exception ex)
{
flag = false;
throw ex;
}
return flag;
}
有时候导出时出现缺少CSS样式的特性,自此补充
public bool exportAndPrint(string fileType, string fileName)
{
bool flag = false;
try
{
//定义文档类型、字符编码
Response.Clear();
Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
//2.定义导入文档的类型
HttpContext.Current.Response.ContentType = fileType;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=\"" +
System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
//定义一个输入流
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
//将目标数据绑定到输入流输出
this.RenderControl(hw);
string outStr = "<style> ";
using (System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath("~/CSS/APDetailInfo.css")))
{
outStr += sr.ReadToEnd();
}
outStr += " </style>";
outStr = outStr.Replace("/r/n", "");
outStr = tw.ToString().Replace("<link href=\"CSS/APDetailInfo.css\" rel=\"stylesheet\" />", "");
Response.Output.Write(outStr);
Response.Flush();
Response.End();
flag = true;
}
catch (Exception ex)
{
flag = false;
throw ex;
}
return flag;
}
protected void Export_Click(object sender, EventArgs e)
{
TYKY_OA.Web.Listnation<TYKY_OA.Model.ExtraInfo> pageList = (Listnation<Model.ExtraInfo>)Session["ADAFOTRlistnation"];
if (pageList != null)
{
//由于是导出整个页面,有些控件不想被导出,所以置为空
this.Gv.Columns[10].Visible = false;
//这里是整个DIV的隐藏
this.head.Visible = false;
this.boot.Visible = false;
this.Gv.DataSource = pageList.ObjList;
this.Gv.DataBind();
//ms-word/ms-txt/ms-html/ms-excel
if (!ExportGv("application/ms-excel", "ExportGv.xls"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "ExportFailed", "<script>alert('数据导入失败');</script>");
}
this.prev.Disabled = !pageList.FlagPrivous;
this.next.Disabled = !pageList.FlagNext;
this.pageIndex.Text = "当前第" + pageList.CurrentPage + "页" + "(共" + pageList.PageCount + "页)";
this.Gv.DataSource = pageList.GetList();
this.Gv.DataBind();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
// base.VerifyRenderingInServerForm(control);
}
ASP.NET 导出数据表格的更多相关文章
- ASP.NET导出数据到Excel 实例介绍
ASP.NET导出数据到Excel 该方法只是把asp.net页面保存成html页面只是把后缀改为xlc不过excel可以读取,接下连我看看还有别的方式能导出数据,并利用模版生成. 下面是代码 新建 ...
- [js]EasyUI导出数据表格(Export DataGrid)
包括 'datagrid-export.js' 文件 <script type="text/javascript" src="datagrid-export.js& ...
- 【转】asp.net导出数据到Excel的三种方法
来源:http://www.cnblogs.com/lishengpeng1982/archive/2008/04/03/1135490.html 原文出处:http://blog.csdn.net/ ...
- thinkphp5中使用excel导出数据表格(包涵图片)
首先使用composer require phpoffice/phpexcel下载安装phpexcel包. 将包放入extend下面. 不附加图片的导出 /** * 导出excel(不带图片) * @ ...
- phpexcel导出数据表格
1.下载phpexcel(李昌辉) 2.在页面引入phpexcel的类文件,并且造该类的对象 include("../chajian/phpexcel/Classes/PHPExcel.ph ...
- asp.net将数据导出到excel
本次应用datatable导出,若用gridview(假设gridview设为了分页显示)会出现只导出当前页的情况. protected void btnPrn_Click(object sender ...
- ASP如何将table导出EXCEL表格
网页导出excel表格非常常用,对于一些加载<table>的数据网页,经常会用到这种功能,下面和大家分享一下ASP如何导出EXCEL表格 工具/原料 ASP编辑器 方法/步骤 ...
- [转] Asp.Net 导出 Excel 数据的9种方案
湛刚 de BLOG 原文地址 Asp.Net 导出 Excel 数据的9种方案 简介 Excel 的强大之处在于它不仅仅只能打开Excel格式的文档,它还能打开CSV格式.Tab格式.website ...
- 导出数据到Excel表格
开发工具与关键技术:Visual Studio 和 ASP.NET.MVC,作者:陈鸿鹏撰写时间:2019年5月25日123下面是我们来学习的导出数据到Excel表格的总结首先在视图层写导出数据的点击 ...
随机推荐
- WPFTookit Chart 入门
如何使用WPFToolKit Chart private void button1_Click(object sender, EventArgs e) { var s = new Series(); ...
- linux Shell脚本编码格式
在windows下开发,写好的shell脚本,放到linux上执行,往往会因为编码格式的问题存在兼容问题: -bash: ./lbs-circle-server.sh: /bin/sh^M: bad ...
- MIT 6.824 : Spring 2015 lab3 训练笔记
摘要: 源代码参见我的github:https://github.com/YaoZengzeng/MIT-6.824 Lab3: Paxos-based Key/Value Service Intro ...
- transactionManager的type与dataSource的type
1. 在ibatis的配置文件中dataSource 节点有这么个配置<datasource type="SIMPLE"></datasource>,根据原 ...
- SpringMVC注入Spring的bean
一.Spring和SpringMVC两个IOC容器有什么关系呢? Spring的IOC容器包含了SpringMVC的IOC,即SpringMVC配置的bean可以调用Spring配置好的bean,反之 ...
- 第三章Struts2 Action中动态方法调用、通配符的使用
01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...
- 常用Git代码托管服务分享
Git Repository代码托管服务越来越流行,目前有很多商业公司和个人团队逐渐切换项目到 Git平台进行代码托管.本文分享一些常用的Git代码托管服务,其中一些提供私有项目保护服务,特别有利于远 ...
- Swift之 ? 和 !
Swift之 ? 和 ! 转载于:http://joeyio.com/ios/2014/06/04/swift---/ Swift语言使用var定义变量,但和别的语言不同,Swift里不会自动给变量赋 ...
- Javascript四舍五入(Math.round()与Math.pow())
代码 Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ ...
- BAD APPLE C++控制台程序
使用C++语言编写,如果想改成C语言,手动以GetProcAddress的方式来调用相关函数即可. #include <windows.h> #include <mmsystem.h ...