代码之家  ›  专栏  ›  技术社区  ›  Jatin

基于sql结果生成多个pdf

  •  0
  • Jatin  · 技术社区  · 7 年前

    我正在生成一个带有sql查询结果的pdf。sql可能返回超过1个预订id。现在它所做的是,将所有预订id的结果放入一个pdf中。我想获取每个预订id并生成它的pdf。基本上,每个预订id和每个pdf压缩一个pdf(这是一个要求)。不确定iTextSharp是否能帮上忙。

    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.IO.Compression;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    
    namespace ConvertPdf
    {
    class Program
    {
    
         private static DataTable GetData(SqlCommand cmd)
        {
            DataTable dt = new DataTable();
            String strConnString = ConfigurationManager.AppSettings["dbConn"];
            SqlConnection con = new SqlConnection(strConnString);
            SqlDataAdapter sda = new SqlDataAdapter();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            try
            {
                con.Open();
                sda.SelectCommand = cmd;
                sda.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                sda.Dispose();
                con.Dispose();
            }
        }
    
    
    
        static void Main(string[] args)
        {
    
            //Get the data from database into datatable
            string strQuery = "select * from emp_booking where emp_booking_id in (select e.emp_booking_id from emp_booking e, emp_booking_file ef where ef.booking_date>=getdate() and e.emp_booking_id = ef.emp_booking_id)";
            SqlCommand cmd = new SqlCommand(strQuery);
            DataTable dt = GetData(cmd);
            Transpose(dt);
            int i=1;
    
            foreach(DataRow r in dt.Rows)
            {
            Document document = new Document();
    
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("M://Bookingsheet/Pdf/Bookingsheet" + i + ".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, 4f };
    
            table.SetWidths(widths);
    
            table.WidthPercentage = 100;
            int iCol = 0;
            string colname = "";
            PdfPCell cell = new PdfPCell(new Phrase("EmpSheet"));
    
            cell.Colspan = dt.Columns.Count;
    
           foreach (DataColumn c in dt.Columns)
            {
    
                table.AddCell(new Phrase(c.ColumnName, font5));
            }
    
    
                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));
                    table.AddCell(new Phrase(r[4].ToString(), font5));
                }
    
            document.Add(table);
            document.Close();
            ZipFile.CreateFromDirectory(@"D://BookingSheet/Pdf", @"M://BookingSheetUpload/Zip/Bookingsheet.zip");
            i++;
        }
    }
    }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   user8061994user8061994    7 年前

    虽然我不熟悉您的数据库模式或PDF所需的格式,但我相信重构您的 Main 方法将为每个预订id生成一个离散的PDF。

       static void Main(string[] args)
        {
            string strQuery = "select * from emp_booking where emp_booking_id in (select e.emp_booking_id from emp_booking e, emp_booking_file ef where ef.booking_date>=getdate() and e.emp_booking_id = ef.emp_booking_id)";
            SqlCommand cmd = new SqlCommand(strQuery);
            DataTable dt = GetData(cmd);
            Transpose(dt);
    
            foreach (DataRow r in dt)
            {
                Document document = new Document();
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("<Unique filename for each booking ID>", FileMode.Create));
                document.Open();
                iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5);
    
                PdfPTable table = new PdfPTable(dt.Columns.Count);
                float[] widths = new float[] { 4f, 4f, 4f, 4f, 4f };
                table.SetWidths(widths);
                table.WidthPercentage = 100;
    
                foreach (DataColumn c in dt.Columns)
                {
                    table.AddCell(new Phrase(c.ColumnName, font5));
                }
    
                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));
                table.AddCell(new Phrase(r[4].ToString(), font5));
    
                document.Add(table);
                document.Close();
                ZipFile.CreateFromDirectory(@"<parent directory of pdf>", @"M://BookingSheetUpload/Zip/<unique zip name>.zip");
            }
        }
    

    这将遍历每个 DataRow 由查询返回,并为每行生成一个PDF。请注意,每个PDF和.zip文件都应该指定一个唯一的名称。

    我也可能建议将这些代码中的一些移到其他类中,或者至少是类程序的静态方法中。再说一次,我对你的申请不熟悉,但看起来 主要 方法是在这里做很多繁重的工作。

    推荐文章