簡體   English   中英

將網格導出為pdf

[英]Export grid to pdf

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

任何建議

我設法通過更改讀取網格標題的代碼從另一個論壇尋求幫助后解決了問題

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

並且

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM