C#导出为PDF并且只添加列,不包括按钮

时间:2015-12-06 19:58:39

标签: c# pdf datagridview itextsharp

我已经制作了密码管理器应用。 我试图只导出名称,用户名,密码和Chaged日期行。 但是现在,它还会导出列按钮。

任何提示?

使用此代码导出时:

  private void exportToPDFToolStripMenuItem_Click(object sender, EventArgs e)
    {
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Pdf File |*.pdf";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
            PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(sfd.FileName, FileMode.Create));
            doc.Open();//Open Document to write

            Paragraph paragraph = new Paragraph("data Exported From PassVault!\n\n");
            PdfPTable table = new PdfPTable(dgvPasswords.Columns.Count);

            //Add the headers
            for (int j = 0; j < dgvPasswords.Columns.Count; j++)
            {
                table.AddCell(new Phrase(dgvPasswords.Columns[j].HeaderText));

            }

            //Flag the first row as header
            table.HeaderRows = 1;

            //Add the actual rows from the DGV to the table
            for (int i = 0; i < dgvPasswords.Columns.Count; i++)
            {
                for (int k = 0; k < dgvPasswords.Rows.Count; k++)
                {
                    if (dgvPasswords[k, i].Value != null)
                    {
                        table.AddCell(new Phrase(dgvPasswords[k, i].Value.ToString()));

                    }
                }
            }

            doc.Add(paragraph);
            doc.Add(table);
            doc.Close();
          //  MessageBox.Show("Exported as Export-List.pdf to \n\n" + Application.StartupPath + " /Export-List.pdf", "PDF Exported", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    }

它还包括列复制和列新按钮.. 使它完美,但现在我不确定我错过了什么。

如何仅导出:DgvPassword中的名称,用户名,密码和更改日期?

1 个答案:

答案 0 :(得分:0)

由于使用了k

的错误上限,您试图访问dgvPasswords数组的边界之外
 for (int k = 0; k < dgvPasswords.Columns.Count; k++)
        {

应该阅读

  for (int k = 0; k < dgvPasswords.Rows.Count; k++)
        {