2012-08-27 77 views
1

我需要在我正在開發的系統中生成一些報告...如何使用Crystal Reports與C#/ ASP.NET生成pdf服務器端?

現在我有一個使用itext sharp的示例...但是,由於它的簡單性,我無法創建好看的報告。 ..

所以有人問我切換到Crystal Reports,但我不知道在哪裏,如何開始......

從我已經看到了,似乎用戶可以創建自己的報告教程使用CR工具,但我想要的是,報告生成自動採取一些數據,用戶提供...

這裏是我與iText的尖銳

public class GeneracionPDF 
{ 
    //I need a list for products and a list for services 
    List<Producto> listaProductos = new List<Producto>(); 
    List<Servicio> listaServicios = new List<Servicio>(); 

    private Producto objProducto = new Producto(); 
    private Servicio objServicio = new Servicio(); 
    private CarritoVenta objCV = new CarritoVenta(); 

    public void GenerarPdf(List<Articulo> lstArticulos, string strNombreReporte, string strEmpresa, string strSlogan, string strNombreVendedor, string strNombreCliente, string strRuta) 
    { 
     Organizar(lstArticulos); 

     #region Inicio 
     Document documento = new Document(PageSize.LETTER, 50, 50, 50, 50); 
     PdfWriter writerPdf = PdfWriter.GetInstance(documento, new FileStream(strRuta + strNombreReporte, FileMode.Create)); 
     documento.Open(); 
     #endregion 

     #region Titulo 
     Font font1 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 18, Font.BOLD, BaseColor.BLUE); 
     Phrase phrEmpresa = new Phrase(strEmpresa + "\n", font1); 

     Font font2 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12, Font.ITALIC, BaseColor.DARK_GRAY); 
     Phrase phrSlogan = new Phrase(strSlogan, font2); 

     Font font16I = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 16, Font.ITALIC, BaseColor.BLACK); 
     Phrase phrDescrip = new Phrase(("Cliente: " + strNombreCliente), font16I); 

     PdfContentByte cb = writerPdf.DirectContent; 
     documento.Add(phrEmpresa); 
     documento.Add(phrSlogan); 
     documento.Add(phrDescrip); 
     BaseFont bfTimes = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, false); 
     Font fontHelvetica16red = new Font(bfTimes, 12, Font.ITALIC, BaseColor.RED); 
     #endregion 

     #region Seccion Productos 
     if (listaProductos.Count > 0) 
     { 
      documento.Add(new Paragraph("\n Productos \n", fontHelvetica16red)); 
      PdfPTable tableProductos = null; 

      tableProductos = new PdfPTable(6); 
      float[] colWidths = { 100, 200, 400, 150, 150, 150 }; 
      tableProductos.SetWidths(colWidths); 
      Font LetraTituloTabla = FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.NORMAL); 
      Font LetraCeldaTabla = FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL); 

      PdfPCell celdaIdProducto = new PdfPCell(new Phrase("ID", LetraTituloTabla)); 
      PdfPCell celdaNombreProducto = new PdfPCell(new Phrase("Nombre", LetraTituloTabla)); 
      PdfPCell celdaDescripcionProducto = new PdfPCell(new Phrase("Descripción", LetraTituloTabla)); 
      PdfPCell celdaPrecioProducto = new PdfPCell(new Phrase("Precio", LetraTituloTabla)); 
      PdfPCell celdaCantidadProducto = new PdfPCell(new Phrase("Cantidad", LetraTituloTabla)); 
      PdfPCell celdaTotalProducto = new PdfPCell(new Phrase("Total", LetraTituloTabla)); 

      celdaIdProducto.HorizontalAlignment = 1; 
      celdaNombreProducto.HorizontalAlignment = 1; 
      celdaDescripcionProducto.HorizontalAlignment = 1; 
      celdaPrecioProducto.HorizontalAlignment = 1; 
      celdaCantidadProducto.HorizontalAlignment = 1; 
      celdaTotalProducto.HorizontalAlignment = 1; 

      tableProductos.AddCell(celdaIdProducto); 
      tableProductos.AddCell(celdaNombreProducto); 
      tableProductos.AddCell(celdaDescripcionProducto); 
      tableProductos.AddCell(celdaPrecioProducto); 
      tableProductos.AddCell(celdaCantidadProducto); 
      tableProductos.AddCell(celdaTotalProducto); 

      foreach (Producto prod in listaProductos) 
      { 
       celdaIdProducto = new PdfPCell(new Phrase(prod.IdProducto.ToString(), LetraCeldaTabla)); 
       celdaNombreProducto = new PdfPCell(new Phrase(prod.Nombre, LetraCeldaTabla)); 
       celdaDescripcionProducto = new PdfPCell(new Phrase(prod.Descripcion, LetraCeldaTabla)); 
       celdaPrecioProducto = new PdfPCell(new Phrase("$ " + prod.PrecioVenta, LetraCeldaTabla)); 
       celdaCantidadProducto = new PdfPCell(new Phrase(prod.Cantidad.ToString(), LetraCeldaTabla)); 
       celdaTotalProducto = new PdfPCell(new Phrase("$ " + prod.TotalCompra, LetraCeldaTabla)); 

       celdaIdProducto.HorizontalAlignment = 1; 
       celdaNombreProducto.HorizontalAlignment = 1; 
       celdaDescripcionProducto.HorizontalAlignment = 1; 
       celdaPrecioProducto.HorizontalAlignment = 1; 
       celdaCantidadProducto.HorizontalAlignment = 1; 
       celdaTotalProducto.HorizontalAlignment = 1; 

       tableProductos.AddCell(celdaIdProducto); 
       tableProductos.AddCell(celdaNombreProducto); 
       tableProductos.AddCell(celdaDescripcionProducto); 
       tableProductos.AddCell(celdaPrecioProducto); 
       tableProductos.AddCell(celdaCantidadProducto); 
       tableProductos.AddCell(celdaTotalProducto); 
      } 

      documento.Add(tableProductos); 
     } 
     #endregion 

     #region Seccion Servicios 
     if (listaServicios.Count > 0) 
     { 
      documento.Add(new Paragraph("\n Servicios \n", fontHelvetica16red)); 
      PdfPTable tableServicios = null; 

      tableServicios = new PdfPTable(6); 
      float[] colWidths = { 100, 200, 400, 150, 150, 150 }; 
      tableServicios.SetWidths(colWidths); 
      Font LetraTituloTabla = FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.NORMAL); 
      Font LetraCeldaTabla = FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL); 

      PdfPCell celdaIdServicio = new PdfPCell(new Phrase("ID", LetraTituloTabla)); 
      PdfPCell celdaNombreServicio = new PdfPCell(new Phrase("Nombre", LetraTituloTabla)); 
      PdfPCell celdaDescripcionServicio = new PdfPCell(new Phrase("Descripción", LetraTituloTabla)); 
      PdfPCell celdaPrecioServicio = new PdfPCell(new Phrase("Precio", LetraTituloTabla)); 
      PdfPCell celdaCantidadServicio = new PdfPCell(new Phrase("Cantidad", LetraTituloTabla)); 
      PdfPCell celdaTotalServicio = new PdfPCell(new Phrase("Total", LetraTituloTabla)); 

      celdaIdServicio.HorizontalAlignment = 1; 
      celdaNombreServicio.HorizontalAlignment = 1; 
      celdaDescripcionServicio.HorizontalAlignment = 1; 
      celdaPrecioServicio.HorizontalAlignment = 1; 
      celdaCantidadServicio.HorizontalAlignment = 1; 
      celdaTotalServicio.HorizontalAlignment = 1; 

      tableServicios.AddCell(celdaIdServicio); 
      tableServicios.AddCell(celdaNombreServicio); 
      tableServicios.AddCell(celdaDescripcionServicio); 
      tableServicios.AddCell(celdaPrecioServicio); 
      tableServicios.AddCell(celdaCantidadServicio); 
      tableServicios.AddCell(celdaTotalServicio); 

      foreach (Servicio ser in listaServicios) 
      { 
       celdaIdServicio = new PdfPCell(new Phrase(ser.IdServicio.ToString(), LetraCeldaTabla)); 
       celdaNombreServicio = new PdfPCell(new Phrase(ser.Nombre, LetraCeldaTabla)); 
       celdaDescripcionServicio = new PdfPCell(new Phrase(ser.Descripcion, LetraCeldaTabla)); 
       celdaPrecioServicio = new PdfPCell(new Phrase("$ " + ser.Precio, LetraCeldaTabla)); 
       celdaCantidadServicio = new PdfPCell(new Phrase(ser.Cantidad.ToString(), LetraCeldaTabla)); 
       celdaTotalServicio = new PdfPCell(new Phrase("$ " + ser.Total, LetraCeldaTabla)); 

       celdaIdServicio.HorizontalAlignment = 1; 
       celdaNombreServicio.HorizontalAlignment = 1; 
       celdaDescripcionServicio.HorizontalAlignment = 1; 
       celdaPrecioServicio.HorizontalAlignment = 1; 
       celdaCantidadServicio.HorizontalAlignment = 1; 
       celdaTotalServicio.HorizontalAlignment = 1; 

       tableServicios.AddCell(celdaIdServicio); 
       tableServicios.AddCell(celdaNombreServicio); 
       tableServicios.AddCell(celdaDescripcionServicio); 
       tableServicios.AddCell(celdaPrecioServicio); 
       tableServicios.AddCell(celdaCantidadServicio); 
       tableServicios.AddCell(celdaTotalServicio); 
      } 

      documento.Add(tableServicios); 
     } 
     #endregion 

     #region Finalizacion 
     //This method returns a decimal value, so no worries here 
     decimal decSubtotal = objCV.Subtotal(lstArticulos); 

     documento.Add(new Paragraph("\n Subtotal: $" + decSubtotal, fontHelvetica16red)); 
     documento.Add(new Paragraph("IVA: $" + (decSubtotal * 0.11m), fontHelvetica16red)); 
     documento.Add(new Paragraph("Total: $" + (decSubtotal * 1.11m), fontHelvetica16red)); 
     documento.Add(new Paragraph("\nNota: Si no requiere factura, omita el IVA", fontHelvetica16red)); 
     documento.Close(); 
     #endregion 
    } 

    //Method to separate products and services in different lists, since lstArticulos contains both products and services 
    private void Organizar(List<Articulo> lstArticulos) 
    { 
     listaProductos.Clear(); 
     listaServicios.Clear(); 

     foreach (Articulo art in lstArticulos) 
     { 
      if (art.Tipo == "Producto") 
      { 
       objProducto = new Producto(int.Parse(art.IdArticulo.Substring(8)), art.Nombre, art.Descripcion, art.Imagen, art.Precio, art.Cantidad); 
       listaProductos.Add(objProducto); 
      } 

      else if (art.Tipo == "Servicio") 
      { 
       objServicio = new Servicio(int.Parse(art.IdArticulo.Substring(8)), art.Nombre, art.Descripcion, art.Imagen, art.Precio, art.Cantidad); 
       listaServicios.Add(objServicio); 
      } 
     } 
    } 
} 

所以就像我創建服務器端PDF的像前面的例子中,我想這樣做,但用水晶報表做了一個樣本...

有沒有一種方法可以做類似的事情?任何教程? 哦,順便說一句...我如何在PDF上插入圖片?

感謝

回答

1

這裏,以動態地創建Crystal報表列

http://csharp.net-informations.com/crystal-reports/csharp-dynamic-crystal-reports.htm 

但教程會更好:

  1. 如果使用Crystal報告提出的模板,用戶將填補,因爲美麗的渲染是可能的只有水晶報告嚮導(並且不要讓報告太自由)
  2. 更喜歡使用SSRS(Sql server Reporting Service)它對報告的性能和管理更好(您有一個默認的Web服務器,並且您可以使用webservice來管理模板報告)
相關問題