0

我想从 Excel 中的表格中获取数据,并使用宏自动填写多个可填写的 PDF 表单。

在此处输入图像描述

4

1 回答 1

2

这是一篇文章,解释了如何将 datagridview 转换为 PDF 和 excel 文件

http://www.codeproject.com/Articles/28269/Exporting-a-DataGridView-to-an-Excel-PDF-image-fil

这是我最近在我的一个项目中所做的:

public void ExportToPdf(DataTable ExDataTable) //Datatable 
  {
    //Here set page size as A4

    Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);

  try
   {
      PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
      pdfDoc.Open();

       //Set Font Properties for PDF File
       Font fnt = FontFactory.GetFont("Times New Roman", 12);
       DataTable dt = ExDataTable;

        if (dt != null)
        {

            PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
            PdfPCell PdfPCell = null;

            //Here we create PDF file tables

            for (int rows = 0; rows < dt.Rows.Count; rows++)
            {
                if (rows == 0)
                {
                    for (int column = 0; column < dt.Columns.Count; column++)
                    {
                        PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Columns[column].ColumnName.ToString(), fnt)));
                        PdfTable.AddCell(PdfPCell);
                    }
                }
                for (int column = 0; column < dt.Columns.Count; column++)
                {
                    PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), fnt)));
                    PdfTable.AddCell(PdfPCell);
                }
            }

            // Finally Add pdf table to the document 
            pdfDoc.Add(PdfTable);
        }

        pdfDoc.Close();

        Response.ContentType = "application/pdf";

        //Set default file Name as current datetime
        Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");

        System.Web.HttpContext.Current.Response.Write(pdfDoc);

        Response.Flush();
        Response.End();

    }
    catch (Exception ex)
    {
        Response.Write(ex.ToString());
    }
}  
于 2013-02-23T13:14:38.417 回答