如何以pdf格式导出datagridview数据

时间:2016-09-18 20:38:54

标签: c# pdf datagridview export

我想知道如何以pdf格式导出datagridview数据我正在谷歌搜索但我还是找不到所以请大家知道如果你知道它请告诉我你的代码解决方案请不要放任何任何文章的链接。

What I have tried:

public void ExportToPdf(DataTable dt)
               {
                   Document document = new Document();
                   PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("c://sample.pdf", FileMode.Create));
                   document.Open();
                   iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5);

                   PdfPTable table = new PdfPTable(dt.Columns.Count);
                   PdfPRow row = null;
                   float[] widths = new float[] { 4f, 4f, 4f, 4f };

                   table.SetWidths(widths);

                   table.WidthPercentage = 100;
                   int iCol = 0;
                   string colname = "";
                   PdfPCell cell = new PdfPCell(new Phrase("Products"));

                   cell.Colspan = dt.Columns.Count;

                   foreach (DataColumn c in dt.Columns)
                   {

                       table.AddCell(new Phrase(c.ColumnName, font5));
                   }

                   foreach (DataRow r in dt.Rows)
                   {
                       if (dt.Rows.Count > 0)
                       {
                           table.AddCell(new Phrase(r[0].ToString(), font5));
                           table.AddCell(new Phrase(r[1].ToString(), font5));
                           table.AddCell(new Phrase(r[2].ToString(), font5));
                           table.AddCell(new Phrase(r[3].ToString(), font5));
                       }
                   } document.Add(table);
                   document.Close();
               }

0 个答案:

没有答案