问题描述
大家好,
我在asp.net页面使用c#有一个转发器。
我添加了一个export2pdf按钮。
代码:
Hi guys,
I have a repeater in asp.net page using c#.
where i have added a export2pdf button.
code:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=JobReport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Repeater1.DataSource = Department.GetDepartments();
Repeater1.DataBind();
Repeater1.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();
代码将转发器数据导出到pdf但没有桌边框和颜色。
i希望导出,因为它转发器,pdf,意思是:颜色,图像,边框......等等,我在转发器中有什么,我想导出它,就像一个图像。 />
任何人都可以帮助我,我该怎么做.......
谢谢
code is exporting the repeater data to the pdf but without table border and color.
i want to export as it repeater, to the pdf, means: color, images, borders ... etc, etc. what ever i have in the repeater, i want to export it, like an image.
Can anyone plz help me, how can i do this.......
Thanks
推荐答案
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div><img src="https://lh5.googleusercontent.com/_B28NJpJ61hA/TdgnS7lh7mI/AAAAAAAAAi4/oLTicIRgEIw/FinalLogo.png" /></div>
<div><b>Export Webpage with images to pdf using itextsharp dll</b></div><br />
<div>
<asp:GridView ID="gvDetails" AutoGenerateColumns="false" CellPadding="5" runat="server">
<Columns>
<asp:BoundField HeaderText="UserId" DataField="UserId" />
<asp:BoundField HeaderText="UserName" DataField="UserName" />
<asp:BoundField HeaderText="Education" DataField="Education" />
<asp:BoundField HeaderText="Location" DataField="Location" />
</Columns>
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
<asp:Button ID="btnPDF" runat="server" Text="Export to PDF" OnClick="btnPDF_Click"/>
</form>
</body>
</html>
使用System;
使用System.Web;
使用System.Web.UI;
使用System.Data;
使用System.IO;
使用iTextSharp.text;
使用iTextSharp.text.pdf;
使用iTextSharp.text。 html.simpleparser;
之后写下面的co de in code
protected void Page_Load(object sender,EventArgs e)
{
if(! IsPostBack)
{
BindGridview();
}
}
protected void BindGridview()
{
DataTable dt = new DataTable();
dt.Columns.Add(UserId,typeof(Int32) ));
dt.Columns.Add(UserName,typeof(string));
dt.Columns.Add(Education,typeof(string)) ;
dt.Columns.Add(Location,typeof(string));
DataRow dtrow = dt.NewRow(); //创建新行
dtrow [UserId] = 1; //将数据绑定到列
dtrow [UserName] =SureshDasari;
dtrow [Education] =B.Tech;
dtrow [Location] =Chennai;
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); //创建新行
dtrow [UserId] = 2; //将数据绑定到列
dtrow [UserName] =MadhavSai;
dtrow [Education] =MBA;
dtrow [Location] =Nagpur;
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); //创建新行
dtrow [UserId] = 3; //将数据绑定到列
dtrow [UserName] =MaheshDasari;
dtrow [Education] =B.Tech;
dtrow [Location] =Nuzividu;
dt.Rows.Add(dtrow);
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
/ *验证控件是否呈现* /
}
protected void btnPDF_Click(object sender,EventArgs e)
{
Response.ContentType =application / pdf;
Response.AddHeader(content-disposition,attachment; filename = UserDetails.pdf);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
文件pdfDoc =新文件(PageSize .A4,10f,10f,100f, 0.0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc,Response.OutputStream);
pdfDoc。打开();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
using System;
using System.Web;
using System.Web.UI;
using System.Data;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
After that write the following code in code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
protected void BindGridview()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(Int32));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Education", typeof(string));
dt.Columns.Add("Location", typeof(string));
DataRow dtrow = dt.NewRow(); // Create New Row
dtrow["UserId"] = 1; //Bind Data to Columns
dtrow["UserName"] = "SureshDasari";
dtrow["Education"] = "B.Tech";
dtrow["Location"] = "Chennai";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); // Create New Row
dtrow["UserId"] = 2; //Bind Data to Columns
dtrow["UserName"] = "MadhavSai";
dtrow["Education"] = "MBA";
dtrow["Location"] = "Nagpur";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); // Create New Row
dtrow["UserId"] = 3; //Bind Data to Columns
dtrow["UserName"] = "MaheshDasari";
dtrow["Education"] = "B.Tech";
dtrow["Location"] = "Nuzividu";
dt.Rows.Add(dtrow);
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void btnPDF_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
Response.Clear();
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
Response.AddHeader("content-disposition", "attachment;filename=" + "Report.xls");
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
((Repeater)Cache["Report"]).RenderControl(htw);
System.Text.StringBuilder sb1 = new System.Text.StringBuilder();
sb1 = sb1.Append("<table cellspacing="0" cellpadding="0" width="100%" align="center" border="1">" + sw.ToString() + "</table>");
sw = null;
htw = null;
Response.Write(sb1.ToString());
sb1.Remove(0, sb1.Length);
Response.Flush();
Response.End();
这篇关于在asp.net中导出具有表格边框的转发器数据c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!