【发布时间】:2014-04-22 11:02:49
【问题描述】:
我正在使用 obout 网格,并且我正在尝试将网格导出为 PDF,当我使用 sqldatasource 填充网格时导出工作完美,但是当我使用存储过程和后面代码中的函数填充网格时,我得到了这个错误
System.NullReferenceException:对象引用未设置为对象的实例
问题出在内部循环中
//How add the data from the Grid to pdf table
for (int i = 0; i < Grid1.Rows.Count; i++)
{
Hashtable dataItem = Grid1.Rows[i].ToHashtable();
foreach (Column col in Grid1.Columns)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(dataItem[col.DataField].ToString(), font8)));
PdfPCell.RunDirection = PdfWriter.RUN_DIRECTION_LTR; //change the text direction for the arabic
PdfTable.AddCell(PdfPCell);
}
}
当我跟踪错误时,我注意到当它到达最后一列时,它将继续读取另一个不存在的列 {Column},这就是我在这一行出现错误的原因
PdfPCell = new PdfPCell(new Phrase(new Chunk(dataItem[col.DataField].ToString(), font8)));
哪个是空的
完整代码
private void ExportGridToPDF()
{
// Stream which will be used to render the data
MemoryStream fileStream = new MemoryStream();
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
try
{
//Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
PdfWriter wri = PdfWriter.GetInstance(doc, fileStream);
doc.Open();//Open Document to write
//Font font8 = FontFactory.GetFont("TAHOMA", 7);
BaseFont bf = BaseFont.CreateFont("c:\\\\windows\\\\fonts\\\\tahoma.ttf", BaseFont.IDENTITY_H, true);
iTextSharp.text.Font font8 = new iTextSharp.text.Font(bf, 8, iTextSharp.text.Font.NORMAL);
//Write some content
Paragraph paragraph = new Paragraph("ASP.NET Grid - Export to PDF");
//Craete instance of the pdf table and set the number of column in that table
PdfPTable PdfTable = new PdfPTable(Grid1.Columns.Count);
PdfPCell PdfPCell = null;
//Add headers of the pdf table
foreach (Column col in Grid1.Columns)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(col.HeaderText, font8)));
PdfPCell.RunDirection = PdfWriter.RUN_DIRECTION_LTR; //change the text direction for the arabic
PdfTable.AddCell(PdfPCell);
}
//How add the data from the Grid to pdf table
for (int i = 0; i < Grid1.Rows.Count; i++)
{
Hashtable dataItem = Grid1.Rows[i].ToHashtable();
foreach (Column col in Grid1.Columns)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(dataItem[col.DataField].ToString(), font8)));
PdfPCell.RunDirection = PdfWriter.RUN_DIRECTION_LTR; //change the text direction for the arabic
PdfTable.AddCell(PdfPCell);
}
}
PdfTable.SpacingBefore = 15f;
doc.Add(paragraph);
doc.Add(PdfTable);
}
catch (DocumentException docEx)
{
//handle pdf document exception if any
MessageBox.Show(docEx.ToString());
}
catch (IOException ioEx)
{
// handle IO exception
MessageBox.Show(ioEx.ToString());
}
catch (Exception ex)
{
// ahndle other exception if occurs
MessageBox.Show(ex.ToString());
}
finally
{
//Close document and writer
doc.Close();
}
// Send the data and the appropriate headers to the browser
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=oboutGrid.pdf");
Response.ContentType = "application/pdf";
Response.BinaryWrite(fileStream.ToArray());
Response.End();
}
任何建议
【问题讨论】:
-
我现在没有加载 VS,所以我不能做太多,但你正在创建一个集合 (
dataItem),然后迭代另一个集合 (Grid1.Columns) 但引用第一个(dataItem[col.DataField])只是在自找麻烦。你不能把foreach换成dataItem吗? -
正如我之前提到的,我有 8 列和 Grid1.Columns 读取 9 列我怎样才能使它只读 8 以避免错误
-
你没有提到这些数字。我们不知道您的数据库也不知道您的存储过程。由于我们无法访问您的数据库,请比较
Grid1.Columns和dataItem的实际内容并在此处提供。我上面的评论说首先不要打扰Grid1.Columns,你试过了吗?
标签: c# asp.net .net grid pdf-generation