elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Únete al Grupo Steam elhacker.NET


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  Exportar un Datagridview a PDF con un encabezado personalizado
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Exportar un Datagridview a PDF con un encabezado personalizado  (Leído 10,627 veces)
joalbela

Desconectado Desconectado

Mensajes: 30



Ver Perfil
Exportar un Datagridview a PDF con un encabezado personalizado
« en: 11 Marzo 2011, 02:49 am »

Hola amigos tengo este codigo que genera un reporte en un datagridview y luego lo exporta a PDF, me gustaria que el encabezado donde dice los nombres de las columnas tuviera una letra en negrilla y que esta fila fuera un poco mas gruesa y con un color de fondo pero no se como hacerle les agredeceria mucho su ayuda.




Código
  1.            IfxDataAdapter dt = new IfxDataAdapter(" SELECT a.cdgo_prdcto as CODIGO,a.nmbre_prdcto DESCRIPCION, SUM(b.cntdad)PMDIOVTA ,a.csto_rpscion CR, " +
  2.                                                   " a.clfccion_prmnnte P, a.clfccion_tmpral T,a.cdgo_brra EAN" +
  3.                                                   " from prdcto a , prmdio_vnta b " +
  4.                                                   " where estdo = 'A' " +
  5.                                                   " and a.cdgo_prdcto = b.cdgo_prdcto " +
  6.                                                   " group by 1,2,4,5,6,7 " +                                                                                                                            
  7.                                                    " order by 2 ", conexion);
  8.            DataGridTableStyle ts1 = new DataGridTableStyle();
  9.  
  10.  
  11.  
  12.             DataSet dsdatos = new DataSet();
  13.             dt.Fill(dsdatos, "Datos");                        
  14.  
  15.            this.dataGridView1.DataMember = "Datos";
  16.            //this.dataGridView1.Columns(1).Width = 4400;
  17.            this.dataGridView1.DataSource = dsdatos;
  18.  
  19.  
  20.  
  21.  
  22.        }
  23.  
  24.        private void Form1_Load(object sender, EventArgs e)
  25.        {
  26.  
  27.        }
  28.  
  29.        private void btnExportar_Click(object sender, EventArgs e)
  30.        {
  31.  
  32.  
  33.            DateTime hora = DateTime.Now;
  34.            string fcha_ttal = Convert.ToDateTime(hora).Day + "/" + Convert.ToDateTime(hora).Month + "/" + Convert.ToDateTime(hora).Year;
  35.  
  36.  
  37.            Document doc = new Document(PageSize.A4, 9, 9, 10, 10);
  38.            string filename = "C:\\Temp\\Listado Total.pdf";
  39.            iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(@"C:\Mis documentos\Biblia_productos\Biblia_productos\Images\marion.JPG"); jpg.Alignment = iTextSharp.text.Image.ALIGN_LEFT;
  40.            Chunk encab = new Chunk(" LISTA DE PRODUCTOS CODIFICADOS EN " + fcha_ttal + "", FontFactory.GetFont("COURIER", 12));
  41.  
  42.  
  43.            try
  44.            {
  45.                FileStream file = new FileStream
  46.               (filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
  47.                PdfWriter.GetInstance(doc, file);
  48.                doc.Open();
  49.  
  50.  
  51.                doc.Add(new Paragraph(encab));
  52.                doc.Add(jpg);
  53.                GenerarDocumento(doc);
  54.  
  55.                Process.Start(filename);
  56.                doc.Close();
  57.            }
  58.  
  59.            catch (Exception ex)
  60.            {
  61.                MessageBox.Show(ex.Message);
  62.            }
  63.  
  64.        }
  65.  
  66.        //Función que genera el documento Pdf
  67.        public void GenerarDocumento(Document document)
  68.        {
  69.            PdfPTable datatable = new PdfPTable(dataGridView1.ColumnCount);
  70.            datatable.DefaultCell.Padding = 1;
  71.            float[] headerwidths = GetTamañoColumnas(dataGridView1);
  72.  
  73.            datatable.SetWidths(headerwidths);
  74.            datatable.WidthPercentage = 100;
  75.            datatable.DefaultCell.BorderWidth = 2;
  76.  
  77.            iTextSharp.text.Font fuente = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER);
  78.  
  79.            DataGridViewCellStyle style = this.dataGridView1.ColumnHeadersDefaultCellStyle;
  80.  
  81.            Phrase objP = new Phrase("A", fuente);
  82.  
  83.            datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
  84.            for (int i = 0; i < dataGridView1.ColumnCount; i++)
  85.            {
  86.  
  87.                objP = new Phrase(dataGridView1.Columns[i].HeaderText, fuente);
  88.                 datatable.HorizontalAlignment = Element.ALIGN_CENTER;
  89.  
  90.                datatable.AddCell(objP);
  91.  
  92.  
  93.            }
  94.            datatable.HeaderRows =1;
  95.  
  96.            datatable.DefaultCell.BorderWidth = 1;
  97.  
  98.  
  99.  
  100.  
  101.            for (int i = 0; i < dataGridView1.RowCount - 1; i++)
  102.            {
  103.                for (int j = 0; j < dataGridView1.ColumnCount; j++)
  104.                {
  105.                    objP = new Phrase(dataGridView1[j, i].Value.ToString(), fuente);
  106.  
  107.                    style.BackColor = Color.WhiteSmoke;
  108.                    style.ForeColor = Color.Gray;
  109.  
  110.                    datatable.AddCell(objP);
  111.  
  112.  
  113.  
  114.                }
  115.                datatable.CompleteRow();
  116.            }
  117.            document.Add(datatable);
  118.        }
  119.        public float[] GetTamañoColumnas(DataGridView dg)
  120.        {
  121.            float[] values = new float[dg.ColumnCount];
  122.            for (int i = 0; i < dg.ColumnCount; i++)
  123.            {
  124.                values[i] = (float)dg.Columns[i].Width;
  125.            }
  126.            return values;
  127.        }


En línea

Jorge MB
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Sniffer y encabezado HTTP
Nivel Web
Achernar 0 2,873 Último mensaje 19 Octubre 2007, 14:34 pm
por Achernar
Exportar DataGridView a Excel (CODIGO)
.NET (C#, VB.NET, ASP)
elmaro 5 23,481 Último mensaje 28 Septiembre 2011, 23:49 pm
por LROJASAYLLON
[Aporte] Exportar DataGridView a Excel, PDF y HTML « 1 2 3 4 »
.NET (C#, VB.NET, ASP)
oscarj24 31 36,367 Último mensaje 9 Julio 2012, 09:41 am
por P4nd3m0n1um
Quitar encabezado
PHP
carlosuc99 9 3,759 Último mensaje 6 Noviembre 2012, 22:58 pm
por ~ Yoya ~
Exportar datos de un DataGridView a un archivo Excel
Programación Visual Basic
okik 1 8,333 Último mensaje 30 Enero 2015, 19:42 pm
por okik
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines