使用 ITextSharp 从 Gridview 导出为 PDF
Export to PDF from Gridview with ITextSharp
我曾经使用 iTextSharp 库从 gridview 生成 PDF 文件。
这是我在 aspx 页面中的简单 GridView:
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="False" EmptyDataText="GV Empty."
DataKeyNames="ID" CssClass="mGrid" Width="500" HorizontalAlign="Center">
<Columns>
<asp:BoundField DataField="Day" HeaderText="Day" ReadOnly="true" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new" DataFormatString="{0:dd/MM/yyyy}" />
<asp:BoundField DataField="Code" HeaderText="Code" ReadOnly="true" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new" />
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="true" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new" />
<asp:BoundField DataField="User" HeaderText="User" ReadOnly="true" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new" />
<asp:BoundField DataField="Number" HeaderText="Number" ReadOnly="true" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new" />
<asp:BoundField DataField="Age" HeaderText="Age" ReadOnly="true" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new" />
<asp:BoundField DataField="Annotation" HeaderText="Annotation" ReadOnly="true" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new" />
</Columns>
</asp:GridView>
我有一个问题,因为在导出的 pdf 文件中我没有找到 GV 的最后一列 "Annotation"。
我没有错误,但是 "Annotation" 列的 header 和您的值没有被导出。
我下面的代码,怎么了?
有人知道我该怎么做吗?
提前致谢。
for (int colIndex = 0; colIndex < colCount; colIndex++)
{
table.SetWidths(new int[] { 20, 20, 20, 20, 20, 20 });
cellText = Server.HtmlDecode(gv.HeaderRow.Cells[colIndex].Text);
BaseFont bf = BaseFont.CreateFont(
BaseFont.HELVETICA,
BaseFont.CP1252,
BaseFont.EMBEDDED,
false);
iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.BOLD, BaseColor.WHITE);
cell = new PdfPCell(new Phrase(cellText.Replace("<br />", Environment.NewLine), font));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.FixedHeight = 45f;
cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#a52a2a"));
table.AddCell(cell);
}
for (int rowIndex = ; rowIndex < gvUsers.Rows.Count; rowIndex++)
{
if (gvUsers.Rows[rowIndex].RowType == DataControlRowType.DataRow)
{
for (int j = 0; j < gvUsers.Columns.Count - 1; j++)
{
cellText = Server.HtmlDecode(gvUsers.Rows[rowIndex].Cells[j].Text);
cell = new PdfPCell(new Phrase(cellText, FontFactory.GetFont("PrepareForExport", 8)));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.FixedHeight = 25f;
table.AddCell(cell);
}
}
}
编辑#1
Exception Details: iTextSharp.text.DocumentException: Wrong number of columns.
table.SetWidths(new int[] { 15, 15, 15, 15, 15, 15, 10 });
编辑#2
int colCount = gvUsers.Columns.Count - 1;
table = new PdfPTable(colCount);
table.HorizontalAlignment = 0;
table.WidthPercentage = 100;
int[] colWidths = new int[gvUsers.Columns.Count];
PdfPCell cell;
string cellText;
for (int colIndex = 0; colIndex < colCount; colIndex++)
{ ....
有问题:
table.SetWidths(new int[] { 20, 20, 20, 20, 20, 20 });
如您所见,您为 PdfPTable
设置了 6 列,但是您的 GridView
有 7 列。并且,所有 widths
的总和超过 100%
.
尝试:
table.SetWidths(new int[] { 15, 15, 15, 15, 15, 15, 10 });
或按您的意愿更改每个 column
的 width
。顺便提一句。 columns
width
在 single
中,因此您可以使用 width
值,例如 14.5
等
更新:(抱歉,它在vb中,但您可以转换为c#)
Dim ttbl As New PdfPTable(7)
ttbl.WidthPercentage = 100
Dim cp() As Integer = {15,15,15,15,15,15,10}
ttbl.SetWidths(cp)
7 cols
是为 table 定义的。你这样做吗?
顺便说一句。为什么你把 table.SetWidths(new int[] { 15, 15, 15, 15, 15, 15, 10 });
放在 for...next
块中?必须放在 for
.
之前和之后
更新#2:
vb
中有完整的代码并且工作正常:
Private Sub PrintTable()
Dim ft As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.EMBEDDED)
Dim mf As New iTextSharp.text.Font(ft, 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK)
Dim doc As Document = New Document(PageSize.A4, 70, 30, 40, 40)
Dim output As MemoryStream = New MemoryStream
Dim wr As PdfWriter = PdfWriter.GetInstance(doc, output)
doc.Open()
Dim tbl As New PdfPTable(7) 'set 7 columns in table
tbl.WidthPercentage = 100
Dim cp() As Integer = {15, 15, 15, 15, 15, 15, 10}
tbl.SetWidths(cp)
'write header
For x = 0 To gvUsers.Columns.Count - 1
Dim cell As New PdfPCell(New Phrase(gvUsers.Columns(x).HeaderText.ToString, mf))
cell.HorizontalAlignment = Element.ALIGN_CENTER
cell.VerticalAlignment = Element.ALIGN_MIDDLE
cell.FixedHeight = 45.0F
cell.BackgroundColor = New Color(System.Drawing.ColorTranslator.FromHtml("#a52a2a"))
tbl.AddCell(cell)
Next
mf = New iTextSharp.text.Font(ft, 8, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK)
'write content of table
For x = 0 To gvUsers.Rows.Count - 1
If gvUsers.Rows(x).RowType = DataControlRowType.DataRow Then
For y = 0 To gvUsers.Columns.Count - 1
Dim cellText = Server.HtmlDecode(gvUsers.Rows(x).Cells(y).Text)
Dim cell As New PdfPCell(New Phrase(cellText, mf))
cell.HorizontalAlignment = Element.ALIGN_CENTER
cell.VerticalAlignment = Element.ALIGN_MIDDLE
cell.FixedHeight = 25.0F
tbl.AddCell(cell)
Next
End If
Next
doc.Add(tbl)
doc.Close()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "attachment;filename=test.pdf")
Response.BinaryWrite(output.ToArray())
End Sub
我曾经使用 iTextSharp 库从 gridview 生成 PDF 文件。
这是我在 aspx 页面中的简单 GridView:
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="False" EmptyDataText="GV Empty."
DataKeyNames="ID" CssClass="mGrid" Width="500" HorizontalAlign="Center">
<Columns>
<asp:BoundField DataField="Day" HeaderText="Day" ReadOnly="true" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new" DataFormatString="{0:dd/MM/yyyy}" />
<asp:BoundField DataField="Code" HeaderText="Code" ReadOnly="true" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new" />
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="true" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new" />
<asp:BoundField DataField="User" HeaderText="User" ReadOnly="true" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new" />
<asp:BoundField DataField="Number" HeaderText="Number" ReadOnly="true" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new" />
<asp:BoundField DataField="Age" HeaderText="Age" ReadOnly="true" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new" />
<asp:BoundField DataField="Annotation" HeaderText="Annotation" ReadOnly="true" HtmlEncode="false" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new" />
</Columns>
</asp:GridView>
我有一个问题,因为在导出的 pdf 文件中我没有找到 GV 的最后一列 "Annotation"。
我没有错误,但是 "Annotation" 列的 header 和您的值没有被导出。
我下面的代码,怎么了?
有人知道我该怎么做吗?
提前致谢。
for (int colIndex = 0; colIndex < colCount; colIndex++)
{
table.SetWidths(new int[] { 20, 20, 20, 20, 20, 20 });
cellText = Server.HtmlDecode(gv.HeaderRow.Cells[colIndex].Text);
BaseFont bf = BaseFont.CreateFont(
BaseFont.HELVETICA,
BaseFont.CP1252,
BaseFont.EMBEDDED,
false);
iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.BOLD, BaseColor.WHITE);
cell = new PdfPCell(new Phrase(cellText.Replace("<br />", Environment.NewLine), font));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.FixedHeight = 45f;
cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#a52a2a"));
table.AddCell(cell);
}
for (int rowIndex = ; rowIndex < gvUsers.Rows.Count; rowIndex++)
{
if (gvUsers.Rows[rowIndex].RowType == DataControlRowType.DataRow)
{
for (int j = 0; j < gvUsers.Columns.Count - 1; j++)
{
cellText = Server.HtmlDecode(gvUsers.Rows[rowIndex].Cells[j].Text);
cell = new PdfPCell(new Phrase(cellText, FontFactory.GetFont("PrepareForExport", 8)));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.FixedHeight = 25f;
table.AddCell(cell);
}
}
}
编辑#1
Exception Details: iTextSharp.text.DocumentException: Wrong number of columns.
table.SetWidths(new int[] { 15, 15, 15, 15, 15, 15, 10 });
编辑#2
int colCount = gvUsers.Columns.Count - 1;
table = new PdfPTable(colCount);
table.HorizontalAlignment = 0;
table.WidthPercentage = 100;
int[] colWidths = new int[gvUsers.Columns.Count];
PdfPCell cell;
string cellText;
for (int colIndex = 0; colIndex < colCount; colIndex++)
{ ....
有问题:
table.SetWidths(new int[] { 20, 20, 20, 20, 20, 20 });
如您所见,您为 PdfPTable
设置了 6 列,但是您的 GridView
有 7 列。并且,所有 widths
的总和超过 100%
.
尝试:
table.SetWidths(new int[] { 15, 15, 15, 15, 15, 15, 10 });
或按您的意愿更改每个 column
的 width
。顺便提一句。 columns
width
在 single
中,因此您可以使用 width
值,例如 14.5
等
更新:(抱歉,它在vb中,但您可以转换为c#)
Dim ttbl As New PdfPTable(7)
ttbl.WidthPercentage = 100
Dim cp() As Integer = {15,15,15,15,15,15,10}
ttbl.SetWidths(cp)
7 cols
是为 table 定义的。你这样做吗?
顺便说一句。为什么你把 table.SetWidths(new int[] { 15, 15, 15, 15, 15, 15, 10 });
放在 for...next
块中?必须放在 for
.
更新#2:
vb
中有完整的代码并且工作正常:
Private Sub PrintTable()
Dim ft As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.EMBEDDED)
Dim mf As New iTextSharp.text.Font(ft, 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK)
Dim doc As Document = New Document(PageSize.A4, 70, 30, 40, 40)
Dim output As MemoryStream = New MemoryStream
Dim wr As PdfWriter = PdfWriter.GetInstance(doc, output)
doc.Open()
Dim tbl As New PdfPTable(7) 'set 7 columns in table
tbl.WidthPercentage = 100
Dim cp() As Integer = {15, 15, 15, 15, 15, 15, 10}
tbl.SetWidths(cp)
'write header
For x = 0 To gvUsers.Columns.Count - 1
Dim cell As New PdfPCell(New Phrase(gvUsers.Columns(x).HeaderText.ToString, mf))
cell.HorizontalAlignment = Element.ALIGN_CENTER
cell.VerticalAlignment = Element.ALIGN_MIDDLE
cell.FixedHeight = 45.0F
cell.BackgroundColor = New Color(System.Drawing.ColorTranslator.FromHtml("#a52a2a"))
tbl.AddCell(cell)
Next
mf = New iTextSharp.text.Font(ft, 8, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK)
'write content of table
For x = 0 To gvUsers.Rows.Count - 1
If gvUsers.Rows(x).RowType = DataControlRowType.DataRow Then
For y = 0 To gvUsers.Columns.Count - 1
Dim cellText = Server.HtmlDecode(gvUsers.Rows(x).Cells(y).Text)
Dim cell As New PdfPCell(New Phrase(cellText, mf))
cell.HorizontalAlignment = Element.ALIGN_CENTER
cell.VerticalAlignment = Element.ALIGN_MIDDLE
cell.FixedHeight = 25.0F
tbl.AddCell(cell)
Next
End If
Next
doc.Add(tbl)
doc.Close()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "attachment;filename=test.pdf")
Response.BinaryWrite(output.ToArray())
End Sub