在c#windows应用程序中将datgridview导出为pdf? [英] Export datgridview to pdf in c# windows application?

查看:99
本文介绍了在c#windows应用程序中将datgridview导出为pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

生成pdf文件时,行和列的方式不正确。在删除列'id'来到id列'Fname'来等等。这是我的PDF文件http://s8.postimg.org/nit4hghbp/Untitled.jpg [ ^ ]

这是我的datagridview图片

http://s4.postimg.org/72aj4aar1/Untitled.jpg [ ^ ]



我希望前两列数据更新和删除pdf文件中打印的数据并打印剩余的列数据。

这是我的代码,请帮我解决此错误。

Row and column are not in correct way when pdf file generated .In delete column 'id' come and in id column " 'Fname' come" and so on.Here is my PDF file http://s8.postimg.org/nit4hghbp/Untitled.jpg[^]
Here is my datagridview image
http://s4.postimg.org/72aj4aar1/Untitled.jpg[^]

I want that first two columns data update and delete data print in pdf file and remaining column data printed.
Here is my code,Please help me to resolve this error.

private void button6_Click(object sender, EventArgs e)
        {
            //Creating iTextSharp Table from the DataTable data
            PdfPTable pdfTable = new PdfPTable(dataGridView1.ColumnCount);
            
            pdfTable.DefaultCell.Padding = 3;
            
            pdfTable.WidthPercentage = 70;
            pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
            pdfTable.DefaultCell.BorderWidth = 1;
            //Adding Header row
    foreach (DataGridViewColumn column in dataGridView1.Columns)
    {
        PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
        cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240);
        pdfTable.AddCell(cell);
    }
 
    //Adding DataRow
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        foreach (DataGridViewCell cell in row.Cells)
        {
            if (cell.Value == null)
            {
                
            }
            else
            {
                pdfTable.AddCell(cell.Value.ToString());
                
            }


        }
    }
    //Exporting to PDF
    string folderPath = "C:\\PDFs\\";
    if (!Directory.Exists(folderPath))
    {
        Directory.CreateDirectory(folderPath);
    }
    using (FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create))
    {




        Document pdfDoc = new Document(PageSize.A2,10f,10f,10f,0f);
        
        PdfWriter.GetInstance(pdfDoc, stream);
        pdfDoc.Open();
        pdfDoc.Add(pdfTable);
        pdfDoc.Close();
        stream.Close();
    }

        }
        } 
    }

推荐答案

问题是你有没有分配位图的记录,所以在这部分代码中:



The problem is that you have records where the bitmap is not assigned, so in this part of the code:

if (cell.Value == null)
{
     // if bitmap is null then the cell does not get output, this causes the cells to slide
}
else
{
    pdfTable.AddCell(cell.Value.ToString());

}





不确定正确的代码,但你应该做类似
$ b的事情$ b



Not sure on the correct code but you should do something like

if (cell.Value == null) && (cell.ValueType == string)


这篇关于在c#windows应用程序中将datgridview导出为pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆