C# iTextSharp错误对象引用未设置为对象的实例
我想使用iTextSharp库将数据从DataGridView导出到PDF文件 但当我单击导出Pdf时,会出现此错误 对象引用未设置为对象的实例 线路错误:C# iTextSharp错误对象引用未设置为对象的实例,c#,pdf,datagridview,itext,C#,Pdf,Datagridview,Itext,我想使用iTextSharp库将数据从DataGridView导出到PDF文件 但当我单击导出Pdf时,会出现此错误 对象引用未设置为对象的实例 线路错误: //fetch the header text cellText = Server.HtmlDecode(gvProducts.HeaderRow.Cells[colIndex].Text); 我有这个问题,只有变量rowCountProducts等于零 有人能帮我吗 提前谢谢 代码如下所示 if (rowCountProducts &g
//fetch the header text
cellText = Server.HtmlDecode(gvProducts.HeaderRow.Cells[colIndex].Text);
我有这个问题,只有变量rowCountProducts
等于零
有人能帮我吗
提前谢谢
代码如下所示
if (rowCountProducts >= 0)
{
//link button column is excluded from the list
int colCount = gvProducts.Columns.Count - 1;
//Create a table
table = new PdfPTable(colCount);
table.HorizontalAlignment = 1;
table.WidthPercentage = 100;
//create an array to store column widths
int[] colWidths = new int[gvProducts.Columns.Count];
PdfPCell cell;
string cellText;
//create the header row
for (int colIndex = 0; colIndex < colCount; colIndex++)
{
//set the column width
table.SetWidths(new int[] { 0, 15, 15, 20, 20, 15, 8, 12, 20, 20, 10, 12, 10, 20, 12, 10, 10, 12, 30, 8, 12, 12, 10 });
//fetch the header text
cellText = Server.HtmlDecode(gvProducts.HeaderRow.Cells[colIndex].Text);
//create a new cell with header 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 = 55f;
//set the background color for the header cell
cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#a52a2a"));
//add the cell to the table. we dont need to create a row and add cells to the row
//since we set the column count of the table to 4, it will automatically create row for
//every 4 cells
table.AddCell(cell);
}
//export rows from GridView to table
for (int rowIndex = 0; rowIndex < gvProducts.Rows.Count; rowIndex++)
{
if (gvProducts.Rows[rowIndex].RowType == DataControlRowType.DataRow)
{
for (int j = 0; j < gvProducts.Columns.Count - 1; j++)
{
//fetch the column value of the current row
cellText = Server.HtmlDecode(gvProducts.Rows[rowIndex].Cells[j].Text);
//create a new cell with column value
cell = new PdfPCell(new Phrase(cellText, FontFactory.GetFont("PrepareForExport", 8)));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.FixedHeight = 55f;
string cellText2 = Server.HtmlDecode(gvProducts.Rows[rowIndex].Cells[6].Text);
if (cellText2.ToString() == "B")
{
cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#f5f5dc"));
}
if (cellText2.ToString() == "L")
{
cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#dcf5f5"));
}
if (cellText2.ToString() == "T")
{
cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#ffe9de"));
}
//add the cell to the table
table.AddCell(cell);
}
}
}
}
if(rowCountProducts>=0)
{
//链接按钮列从列表中排除
int colCount=gvProducts.Columns.Count-1;
//创建一个表
表=新的PdfPTable(colCount);
表1.水平对齐=1;
表1.1:百分比=100;
//创建一个数组来存储列宽
int[]colWidths=newint[gvProducts.Columns.Count];
PDFP细胞;
字符串文本;
//创建标题行
对于(int colIndex=0;colIndex”,Environment.NewLine),字体));
cell.HorizontalAlignment=Element.ALIGN_CENTER;
cell.VerticalAlignment=Element.ALIGN_MIDDLE;
cell.FixedHeight=55f;
//设置标题单元格的背景色
cell.BackgroundColor=新的基色(System.Drawing.ColorTranslator.FromHtml(“#a52a2a”);
//将单元格添加到表中。我们不需要创建行并将单元格添加到行中
//由于我们将表的列计数设置为4,它将自动为创建行
//每4个单元格
表2.AddCell(cell);
}
//将行从GridView导出到表
对于(int-rowIndex=0;rowIndex
如果您的问题是
rowCountProducts
等于零,请尝试此操作
我希望这有帮助
//fetch the header text
//start
if (rowCountProducts > 0)
{
cellText = Server.HtmlDecode(gvProducts.HeaderRow.Cells[colIndex].Text);
}
else
{
cellText = "";
}
//end
您可以说
cellText=Server.HtmlDecode(gvProducts.HeaderRow.Cells[colIndex].Text)代码>是错误行。据我所知,这行中没有引用iTextSharp类。因此,您应该将自己的bug搜索和问题集中在其他细节上-将问题集中在iTextSharp上,这是一个很好的方法,可以避开那些对iTextSharp不太了解,但可能对Server.htmlCode(gvProducts.HeaderRow.Cells[colIndex].Text)中的概念非常了解的人。