我有一个spgridview,想将其内容导出为pdf。我正在使用itextsharp进行相同的操作。但是在渲染控件调用时遇到错误。我已经尝试了以下线程中提到的解决方案。问题我也尝试过使用
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
但它不起作用,因为我正在用户控制中编写代码。这是我的 ascx 代码。
<SharePoint:SPGridView ID="domainGridview" AllowPaging="false" runat="server" AutoGenerateColumns="False"
ShowHeader="true" ShowHeaderWhenEmpty="true" AllowSorting="True" GridLines="None" EnabledEventValidation="false">
<AlternatingRowStyle CssClass="ms-alternating" />
<Columns>
<SharePoint:SPBoundField DataField = "SiteName" HeaderText = "Team Site Name" SortExpression = "SiteName"></SharePoint:SPBoundField>
<asp:BoundField DataField = "SiteUrl" HtmlEncode="false" HeaderText = "Team Site URL" SortExpression = "SiteUrl" ></asp:BoundField>
</Columns>
</SharePoint:SPGridView>
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
这是我的用户控制.ascx.cs代码
protected void btnExport_Click(object sender, EventArgs e)
{
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
UserControl myControl = (UserControl)LoadControl("DomainUserWebPartUserControl.ascx");
domainGridview.RenderControl(hw);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=DataTable.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
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();
}
这是我得到的例外
类型为"SPGridView"的控件"ctl00_m_g_70a6af81_243a_45cf_a88e_3760d013ca12_ctl00_domainGridview"必须放置在具有runat=server的表单标记中。
终于我得到了我的解决方案。我正在使用一种完全不同的方法。
protected void btnExport_Click(object sender, EventArgs e)
{
dtUIExport = GetData();
//Creating iTextSharp Table from the DataTable data
PdfPTable pdfTable = new PdfPTable(dtUIExport.Columns.Count - 1);
pdfTable.DefaultCell.BorderWidth = 2;
int[] widths = new int[dtUIExport.Columns.Count - 1];
//setting font
BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"fontsARIALUNI.TTF", BaseFont.IDENTITY_H, true);
//setting width
for (int x = 0; x < domainGridview.Columns.Count -1; x++)
{
widths[x] = (int)domainGridview.Columns[x].HeaderText.Length + 100;
}
pdfTable.SetWidths(widths);
//setting headers
for (int i = 0; i < dtUIExport.Columns.Count; i++)
{
if (i == 1)
{ continue; }
iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
font.Color = new BaseColor(domainGridview.HeaderStyle.ForeColor);
string cellText = Server.HtmlDecode(dtUIExport.Columns[i].ToString());
iTextSharp.text.pdf.PdfPCell cell = new PdfPCell(new Phrase(12, cellText, font));
pdfTable.AddCell(cell);
}
//copying the data to pdftable
for (int i = 0; i < dtUIExport.Rows.Count; i++)
{
for (int j = 0; j < dtUIExport.Columns.Count; j++)
{
if (j == 1)
{ continue; }
string cellText = Server.HtmlDecode(dtUIExport.Rows[i][j].ToString());
iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
font.Color = new BaseColor(domainGridview.RowStyle.ForeColor);
iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font));
pdfTable.AddCell(cell);
}
}
//creating the PDF DOC
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
pdfDoc.Add(pdfTable);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfTable);
Response.End();
}
}