导出为PDF从使用的GridView iTextSharp的 [英] Export to PDF from Gridview with ITextSharp
问题描述
我用用iTextSharp的库来生成一个GridView的PDF文件。
这是我在aspx页面简单的GridView:
< ASP:GridView控件ID =gvUsers=服务器的AutoGenerateColumns =FALSEEmptyDataText =GV空
的DataKeyNames =ID的CssClass =MGRIDWIDTH =500HorizontalAlign =中心>
<柱体和GT;
< ASP:BoundField的数据字段=日的HeaderText =日只读=真HtmlEn code =假ItemStyle-HorizontalAlign =中心ItemStyle-的CssClass =ddl_Class_newDataFormatString ={0:DD / MM / YYYY}/>
< ASP:BoundField的数据字段=code的HeaderText =code只读=真HtmlEn code =假ItemStyle-HorizontalAlign =中心ItemStyle-的CssClass =ddl_Class_new/ >
< ASP:BoundField的数据字段=名称的HeaderText =名称只读=真HtmlEn code =假ItemStyle-HorizontalAlign =中心ItemStyle-的CssClass =ddl_Class_new/>
< ASP:BoundField的数据字段=用户的HeaderText =用户只读=真HtmlEn code =假ItemStyle-HorizontalAlign =中心ItemStyle-的CssClass =ddl_Class_new/>
< ASP:BoundField的数据字段=数字的HeaderText =号码只读=真HtmlEn code =假ItemStyle-HorizontalAlign =中心ItemStyle-的CssClass =ddl_Class_new/>
< ASP:BoundField的数据字段=时代的HeaderText =时代只读=真HtmlEn code =假ItemStyle-HorizontalAlign =中心ItemStyle-的CssClass =ddl_Class_new/>
< ASP:BoundField的数据字段=说文解字注的HeaderText =说文解字注只读=真HtmlEn code =假ItemStyle-HorizontalAlign =中心ItemStyle-的CssClass =ddl_Class_new/>
< /专栏>
< / ASP:GridView的>
我有一个问题,因为在导出的PDF文件,我没有找到关于GV说文解字注的最后一列。
我没有错误,但列说文解字注和你的价值,他们不导出的头。
我的code以下,有什么不对?
任何人都知道我该怎么做呢?
感谢您提前。
的for(int colIndex = 0; colIndex< colCount; colIndex ++)
{
table.SetWidths(新INT [] {20,20,20,20,20,20});
CELLTEXT = Server.HtmlDe code(gv.HeaderRow.Cells [colIndex]。文本);
BASEFONT BF = BaseFont.CreateFont(
BaseFont.HELVETICA,
BaseFont.CP1252,
BaseFont.EMBEDDED,
假); iTextSharp.text.Font字体=新iTextSharp.text.Font(BF,10,iTextSharp.text.Font.BOLD,BaseColor.WHITE);
电池=新PdfPCell(新乐句(cellText.Replace(< BR />中,Environment.NewLine),字体));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.FixedHeight = 45F;
cell.BackgroundColor =新BaseColor(System.Drawing.ColorTranslator.FromHtml(#a52a2a));
table.AddCell(细胞);
}
对于(INT的rowIndex =; rowIndex位置< gvUsers.Rows.Count; rowIndex位置++)
{
如果(gvUsers.Rows [rowIndex位置] .RowType == DataControlRowType.DataRow)
{
对于(INT J = 0; J< gvUsers.Columns.Count - 1; J ++)
{
CELLTEXT = Server.HtmlDe code(gvUsers.Rows [rowIndex位置] .Cells [J]。文本);
电池=新PdfPCell(新乐句(CELLTEXT,FontFactory.GetFont(prepareForExport,8)));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.FixedHeight = 25F;
table.AddCell(细胞);
}
}
}
编辑#1
异常详细信息:iTextSharp.text.DocumentException:列数目错误。table.SetWidths(新INT [] {15,15,15,15,15,15,10});
编辑#2
INT colCount = gvUsers.Columns.Count - 1;
表=新PdfPTable(colCount);
table.HorizontalAlignment = 0;
table.WidthPercentage = 100;
INT [] = colWidths新INT [gvUsers.Columns.Count]
PdfPCell细胞;
串CELLTEXT; 对(INT colIndex = 0; colIndex&下; colCount; colIndex ++)
{....
有问题是:
table.SetWidths(新INT [] {20,20,20,20,20,20});
喜欢,你可以看到,您可以设置6列 PdfPTable
,但您的 GridView控件
有7列。而且,所有的宽度
的总和更然后 100%
。
尝试:
table.SetWidths(新INT [] {15,15,15,15,15,15,10});
或更改宽度
每个列
如何你想。顺便说一句。 列
宽度
在单
,所以你可以使用宽度
值如 14.5
等。
更新:(不好意思,这是在VB中,但可以转换为C#)
昏暗ttbl作为新PdfPTable(7)
ttbl.WidthPercentage = 100
昏暗的CP()作为整数= {} 15,15,15,15,15,15,10
ttbl.SetWidths(CP)
7 COLS
是为表中定义。做呢?
BTW。为什么你把 table.SetWidths(新INT [] {15,15,15,15,15,15,10});
在的。 ..next
阻止?必须把前,进出的,为
。
更新#2:
有在完整code动
和精细工作:
私人小组PrintTable()
昏暗英尺作为BASEFONT = BaseFont.CreateFont(BaseFont.TIMES_ROMAN,BaseFont.CP1250,BaseFont.EMBEDDED)
昏暗的MF作为新iTextSharp.text.Font(英尺,12,iTextSharp.text.Font.NORMAL,iTextSharp.text.Color.BLACK)
昏暗的DOC作为文件=新建文档(PageSize.A4,70,30,40,40)
昏暗的输出作为MemoryStream的=新的MemoryStream
昏暗的WR作为PdfWriter = PdfWriter.GetInstance(文件,输出)
doc.Open()
昏暗的TBL作为新PdfPTable(7)在表中设置7列
tbl.WidthPercentage = 100
暗淡的cp()为整数= {15,15,15,15,15,15,10}
tbl.SetWidths(CP)
写报头
对于x = 0到gvUsers.Columns.Count - 1
昏暗的电池作为新PdfPCell(新乐句(gvUsers.Columns(X).HeaderText.ToString,MF))
cell.HorizontalAlignment = Element.ALIGN_CENTER
cell.VerticalAlignment = Element.ALIGN_MIDDLE
cell.FixedHeight = 45.0F
cell.BackgroundColor =新颜色(System.Drawing.ColorTranslator.FromHtml(#a52a2a))
tbl.AddCell(手机)
下一个
MF =新iTextSharp.text.Font(英尺,8,iTextSharp.text.Font.NORMAL,iTextSharp.text.Color.BLACK)
写表的内容
对于x = 0到gvUsers.Rows.Count - 1
如果gvUsers.Rows(X)= .RowType然后DataControlRowType.DataRow
对于Y = 0要gvUsers.Columns.Count - 1
昏暗CELLTEXT = Server.HtmlDe code(gvUsers.Rows(X).Cells(Y)。文本)
昏暗的电池作为新PdfPCell(新乐句(CELLTEXT,MF))
cell.HorizontalAlignment = Element.ALIGN_CENTER
cell.VerticalAlignment = Element.ALIGN_MIDDLE
cell.FixedHeight = 25.0F
tbl.AddCell(手机)
下一个
万一
下一个
doc.Add(TBL)
doc.Close()
Response.ContentType =应用程序/ PDF
Response.AddHeader(内容处置,附件;文件名=的test.pdf)
Response.BinaryWrite(output.ToArray())
结束小组
I use to generate a PDF file from a gridview using iTextSharp library.
This is my simple GridView in aspx page:
<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>
I have one problem because in exported pdf file I don't find the last column on GV "Annotation".
I don't have error but the header of column "Annotation" and your value they are not exported.
My code below, what's wrong?
Anybody know how can I do that?
Thank you in advance.
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);
}
}
}
Edit # 1
Exception Details: iTextSharp.text.DocumentException: Wrong number of columns.
table.SetWidths(new int[] { 15, 15, 15, 15, 15, 15, 10 });
Edit # 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++)
{ ....
There is problem :
table.SetWidths(new int[] { 20, 20, 20, 20, 20, 20 });
Like You can see, You set 6 columns for PdfPTable
, but Your GridView
have 7 columns. And, sum of all widths
are more then 100%
.
Try :
table.SetWidths(new int[] { 15, 15, 15, 15, 15, 15, 10 });
Or change width
of each column
how You wish. btw. columns
width
are in single
, so You can use width
values like 14.5
etc.
Update : (sorry, it's in vb, but You can convert to 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
are defined for table. Do You do that?
btw. Why You put table.SetWidths(new int[] { 15, 15, 15, 15, 15, 15, 10 });
in for...next
block? Must put before, and out of, for
.
Update #2 :
There is complete code in vb
and work fine :
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
这篇关于导出为PDF从使用的GridView iTextSharp的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!