我有一个spgridview,想要将其内容导出为pdf。我正在使用itextsharp。但是在rendercontrol调用时遇到错误。我已经尝试过以下线程中提到的解决方案。 Question我也尝试过使用
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
但它不起作用我无法在usercontrol中编写代码。 这是我的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" />
这是我的usercontrol.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的表单标签内。
答案 0 :(得分:0)
最后我得到了解决方案。 我一直在使用不同的方法。
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") + @"\fonts\ARIALUNI.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();
}
}