将网格导出为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)
    {   

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在React Kendo网格中将选定的行导出为PDF

来自分类Dev

Kendo将多个图表导出为pdf

来自分类Dev

将Attlassian Confluence Wiki导出为PDF

来自分类Dev

UserControl将GridView导出为PDF

来自分类Dev

将div的内容导出为PDF

来自分类Dev

使用POI将DOC导出为PDF

来自分类Dev

从Python将SSRS报告导出为PDF

来自分类Dev

将div的内容导出为PDF

来自分类Dev

将文本从网站导出为pdf

来自分类Dev

将Attlassian Confluence Wiki导出为PDF

来自分类Dev

将treetable primefaces导出为pdf

来自分类Dev

将 xtable 导出为 pdf 文件

来自分类Dev

将 html 导出为 pdf 横向模式

来自分类Dev

将多页的 html 导出为 pdf

来自分类Dev

如何使用“导出网格为”将网格正确导出为.obj?-无法获取带有纹理的文件

来自分类Dev

将Excel导出为PDF 0显示为(?)

来自分类Dev

导出为pdf SSRS

来自分类Dev

如何将JIRA报告导出为PDF?

来自分类Dev

将具有大量列的html表导出为PDF

来自分类Dev

将报告导出为PDF时,更改字体

来自分类Dev

wicked_pdf将html链接导出为纯文本

来自分类Dev

Excel VBA无法将PageSetup正确导出为PDF

来自分类Dev

如何使用iText将越南文字导出为PDF

来自分类Dev

无法使用iText将越南字符导出为PDF

来自分类Dev

不使用iText将内容导出为pdf

来自分类Dev

使用JSPdf生成将整个html页面导出为pdf

来自分类Dev

在横向模式下将.rdlc报告导出为pdf

来自分类Dev

将多页html导出为单个pdf

来自分类Dev

将Google Map Heatmap图层导出为PDF