下面小结下把gridview的数据导出到excel,world,pdf的几种方法
1 导出到word
Response.AddHeader("content-disposition", "attachment;filename=Export.doc");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
gv.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv);
frm.RenderControl(htmlWrite);
//GridView1.RenderControl(htw);
Response.Write(stringWrite.ToString());
Response.End();
2 导出到excel
string attachment = "attachment; filename=Export.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
gv.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv);
frm.RenderControl(htw);
//GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
3 导出到pdf
这个时候要用到开源的itext了
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
gv.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv);
frm.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
将gridview导出到excel,world,pdf的小结
原创mb5c80f4c73b73a 博主文章分类:Excel ©著作权
©著作权归作者所有:来自51CTO博客作者mb5c80f4c73b73a的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
接了个变态需求:生成 Excel + PDF 导出,用 Java 怎么实现?
接了个变态需求:生成 Excel + PDF 导出,用 Java 怎么实现?
List 表单 实体类 pdf springboot -
将gridview的内容导出到excel中去
asp.net 2.0中,gridview控件是个很不错的控件,如何将gridview中的数据导出到E
Excel ASP.net ASP UI Web -
gridview导出到excel
gridview导出到excel
导出到excel gridview -
gridview导出到excel2007
gridview导出到excel2007
excel gridview -
asp.net 将GridView导出到Excel
请看代码.........
html 文件名 导出excel JAVA -
docker tar 二次构建
一、.使用ieda 创建一个webapp 工程 如下图:二、加入spring mvc 等一下依赖包,配置等,如下:1. 工程结构如下:2. pom.xml 文件完整如 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.or
docker tar 二次构建 docker tomcat springmvc spring