【问题标题】:exporting datagrid to pdf in Windows Forms在 Windows 窗体中将数据网格导出为 pdf
【发布时间】:2015-01-15 14:26:55
【问题描述】:

我正在编写一个简单的应用程序,允许用户在 itextsharp 参考的帮助下将创建的数据网格(此处为它的 dataGridView1)导出到 pdf 文件。 我在互联网上找到了一个有用的示例代码,但我在配置它以导出用户在使用应用程序期间输入的数据时遇到了一些问题。用户在文本框中输入数据,将其保存到数据网格中。 结果 pdf 文件仅包含从文本框中获取的标题,没有单元格内容(用户使用文本框输入和保存的数据)。

这里是代码

        //Creating iTextSharp Table from the DataTable data
        PdfPTable pdfTable = new PdfPTable(dataGridView1.ColumnCount);
        pdfTable.DefaultCell.Padding = 3;
        pdfTable.WidthPercentage = 30;
        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);
        }            

        //Exporting to PDF
        string folderPath = "C:\\Users\\Marcus\\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();
        }
    }

【问题讨论】:

  • foreach 循环中添加另一个循环,该循环实际上遍历每一行。并将单元格值添加到 pdfTable。很明显new PdfPcell(new Phrase(column.HeaderText)); 只会抓取标题
  • 你能给我一个示例循环吗?
  • foreach(DataGridViewRow row in dataGridView1.rows) { foreach(DataGridViewCell cell in row.cells) { PdfPCell cell = new PdfPCell(new Phrase(cell.Value)); } }
  • 实际上只是在当前 foreach 循环之后添加该循环。不要嵌套它。
  • 你确定吗?它说“不能在此范围内声明名为'cell'的局部变量,因为它会给'cell'赋予不同的含义,它已经在'父或当前'范围中用于表示其他东西”和'iTextSharp。 text.pdf.PdfPCell' 不包含'Value' 的定义,并且找不到接受'iTextSharp.text.pdf.PdfPCell' 类型的第一个参数的扩展方法'Value'(您是否缺少 using 指令或程序集参考?)

标签: c# datagrid


【解决方案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);
}   


//Lets add code that actually gets the cell contents down below it:

//add the cell contents
foreach(DataGridViewRow row in dataGridView1.Rows) { 
    foreach(DataGridViewCell cell in row.Cells) { 
        //we need to call .Value to get the value of the cell, but since a Phrase takes in
        //a String, we need to convert the .Value to a String using .ToString()
        PdfPCell pdfCell = new PdfPCell(new Phrase(cell.Value.ToString()));  //this .Value property is that of a DataGridViewCell
        pdfTable.AddCell(pdfCell);
    } 
}

DataGridViewCell 应该有一个Value 属性。使用它来获取单元格的值。

【讨论】:

  • 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); } foreach (DataGridViewRow row in dataGridView1.Rows) { foreach (DataGridViewCell cell in row.Cells) { PdfPCell cell2 = new PdfPCell(new Phrase(row.Cells.ToString())); pdfTable.AddCell(cell2); } } 这是代码@chancea,导出仍然失败
  • 是的,您需要将row.Cells.ToString() 更改为cell.Value
  • Error 1 The best overloaded method match for 'iTextSharp.text.Phrase.Phrase(string)' has some invalid argumentsError 2 Argument 1: cannot convert from 'object' to 'string' 我想我会放弃这个谢谢你的耐心虽然@chancea
  • 应该抛出空引用异常吗? @chancea
  • 我添加了一个空检查if (cell.Value !=null) { MessageBox.Show(cell.Value.ToString()); },它说单元格的值为1。我有点迷路了。
猜你喜欢
  • 1970-01-01
  • 2014-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多