将网格导出为pdf

时间:2014-03-17 09:45:41

标签: c# asp.net .net grid pdf-generation

我正在使用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(); 
}

任何建议

1 个答案:

答案 0 :(得分:0)

我设法在寻求其他论坛的帮助后解决问题 通过更改读取网格标题的代码

PdfPTable PdfTable = new PdfPTable(Grid1.Columns.Count-1);

以及

//Add headers of the pdf table
    foreach (Column col in Grid1.Columns-1)
    {