使用iTextsharp将选定的行数据导出到C#中的pdf [英] Export selected row data to pdf in C# using iTextsharp

查看:75
本文介绍了使用iTextsharp将选定的行数据导出到C#中的pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview,我需要在C#中使用itextsharp将选定的行导出为pdf。我可以将datagridview的标题导出为pdf。



问题1 - 我需要将整个标题添加到pdf中的一列

问题2 - 我需要在相关的colomns面前显示选定的行数据。



我尝试了什么:



 iTextSharp.text.Font text =  new  iTextSharp.text.Font(bf, 11 ,iTextSharp.text.Font.NORMAL); 

凭证doc = new 文档(PageSize.A2.Rotate(), 1 1 1 1 ) ;
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream( Test.pdf,FileMode.Create));
doc.Open();


PdfPTable pdftable = new PdfPTable(dataGridView1.ColumnCount - 0 );

for int j = 0 ; j < dataGridView1.Columns.Count - 0 ; j ++)
{
PdfPCell cell = new PdfPCell( new 短语(dataGridView1.Columns [j] .HeaderText,text));
cell.BackgroundColor = BaseColor.LIGHT_GRAY;
pdftable.AddCell(cell);
}

pdftable.HeaderRows = 0 ;
for int i = 0 ; i < dataGridView1.SelectedRows.Count; i ++)
// for(int i = 0; i< dgvLoadAll.Rows.Count; i ++)
{
for int k = 0 ; k < ; dataGridView1.Columns.Count - 0 ; k ++)
{

if (dataGridView1 [k,i] .Value!= null
{
pdftable.AddCell( new 短语(dataGridView1.SelectedRows [i] .Cells [k] .Value.ToString(),text));
// pdftable.AddCell(new Phrase(dgvLoadAll [k,i] .Value.ToString(), text));

}
}
}

// float [] widths = new float [] {15f,50f,40f,40f,40f,40f,40f,40f,40f,40f,40f,40f,40f,40f,40f ,40f,40f};

// pdftable.SetWidths(widths) ;
doc.Add(pdftable);
doc.Close();
System.Diagnostics.Process.Start( Test.pdf);
}

解决方案

我在你的代码中注意到一件看起来很可疑的事情就是当你说

  if (dataGridView1 [k,i] .Value!=  null 

// 我认为它应该是

if (dataGridView1 [i,k] .Value!= null


I have a datagridview and I need to export only selected rows to pdf using itextsharp in C#. I am able to export datagridview's headers to pdf.

problem 1 - I need to get whole headers to one column in pdf
problem 2 - I need to appear selected row data in front of relevent colomns.

What I have tried:

iTextSharp.text.Font text = new iTextSharp.text.Font(bf, 11, iTextSharp.text.Font.NORMAL);

        Document doc = new Document(PageSize.A2.Rotate(), 1, 1, 1, 1);
        PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("Test.pdf", FileMode.Create));
        doc.Open();

        
        PdfPTable pdftable = new PdfPTable(dataGridView1.ColumnCount - 0);

        for (int j = 0; j < dataGridView1.Columns.Count - 0; j++)
        {
            PdfPCell cell = new PdfPCell(new Phrase(dataGridView1.Columns[j].HeaderText, text));
            cell.BackgroundColor = BaseColor.LIGHT_GRAY;
            pdftable.AddCell(cell);
        }

        pdftable.HeaderRows = 0;
        for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
        //for (int i = 0; i < dgvLoadAll.Rows.Count; i++)
        {
            for (int k = 0; k < dataGridView1.Columns.Count - 0; k++)
            {

                if (dataGridView1[k, i].Value != null)
                {
                    pdftable.AddCell(new Phrase(dataGridView1.SelectedRows[i].Cells[k].Value.ToString(), text));
                    //pdftable.AddCell(new Phrase(dgvLoadAll[k, i].Value.ToString(), text));

                }
            }
        }

        //float[] widths = new float[] { 15f, 50f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f, 40f };

        // pdftable.SetWidths(widths);
        doc.Add(pdftable);
        doc.Close(); 
        System.Diagnostics.Process.Start("Test.pdf");
    }

解决方案

One thing I notice in your code that looks suspect is when you say

if(dataGridView1[k, i].Value != null)

// I think it should be

if(dataGridView1[i,k].Value != null)


这篇关于使用iTextsharp将选定的行数据导出到C#中的pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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