bueno lo primero que haremos sera agregar una referencia a Microsoft.Office.Interop.Word para poder crear el documento. Para esto damos click derecho y vamos a Agregar Referencia:
nos aparecera una ventana asi:
agregamos la referencia y pulsamos Aceptar. Ahora para ver si la referencia esta agregada en nuestra ventana de Explorador de Soluciones pulsamos Mostrar todos los archivos y vemos si esta agregado.
ahora en nuestro formulario importamos las librerias que utilizaremos
Imports System.Data Imports System.Data.SqlClient Imports Microsoft.Office.Interop.Wordahora debajo de Public Class Form1 agregamos las siguientes variables globales:
Dim str = "Data Source=XSaint;Initial Catalog=Northwind;Integrated Security=true" Dim cn As New SqlConnection(str) Dim da As New SqlDataAdapter Dim cmd As New SqlCommand Dim dt As DataTableen el evento Load cargamos el datagrid con el codigo de abajo
With cmd .CommandType = CommandType.Text .CommandText = "select * from Customers" .Connection = cn End With da.SelectCommand = cmd dt = New DataTable da.Fill(dt) DataGridView1.DataSource = dten el evento Click del boton llamamos a la funcion que convertira el datagrid a word:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Call DataGridaAWord(DataGridView1) End Sube implementamos debajo de este procedimiento la funcion DataGridaWord con este codigo
Public Function DataGridaAWord(ByVal Grilla As DataGridView) Dim Word As Application Dim Doc As Document Dim Table As Table Dim Rng As Range Dim prf1 As Paragraph Dim prf2 As Paragraph Dim prf3 As Paragraph Word = CreateObject("Word.Application") Word.Visible = False Doc = Word.Documents.Add prf1 = Doc.Content.Paragraphs.Add prf1.Range.Text = "Pasar de SQL a Word by XSaint" prf1.Range.Font.Color = WdColor.wdColorBlue prf1.Range.Font.Bold = True prf1.Format.SpaceAfter = 24 prf1.Range.InsertParagraphAfter() prf2 = Doc.Content.Paragraphs.Add(Doc.Bookmarks.Item("\endofdoc").Range) prf2.Range.Text = "Descripcion" prf2.Range.Font.Color = WdColor.wdColorBlack prf2.Format.SpaceAfter = 6 prf2.Range.InsertParagraphAfter() prf3 = Doc.Content.Paragraphs.Add(Doc.Bookmarks.Item("\endofdoc").Range) prf3.Range.Text = "Aprendiendo mas" prf3.Range.Font.Bold = False prf3.Format.SpaceAfter = 24 prf3.Range.InsertParagraphAfter() Dim ncol As Integer = Grilla.ColumnCount Dim nrow As Integer = Grilla.RowCount ProgressBar1.Maximum = nrow Table = Doc.Tables.Add(Doc.Bookmarks.Item("\endofdoc").Range, Grilla.RowCount, Grilla.ColumnCount) For i As Integer = 1 To ncol Table.Cell(1, i).Range.Text = Grilla.Columns(i - 1).Name.ToString Next For fila As Integer = 0 To nrow - 2 For col As Integer = 0 To ncol - 1 If Grilla.Rows(fila).Cells(col).Value IsNot DBNull.Value Then Table.Cell(fila + 2, col + 1).Range.Text = Grilla.Rows(fila).Cells(col).Value End If Next ProgressBar1.Increment(1) Next Table.Rows.Item(1).Range.Font.Bold = True Table.Rows.Item(1).Range.Font.Italic = True Table.Borders.InsideLineStyle = WdLineStyle.wdLineStyleDot Table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDot Table.Borders.InsideColor = WdColor.wdColorBlue Rng = Doc.Bookmarks.Item("\endofdoc").Range Rng.InsertParagraphAfter() Rng.InsertAfter("XSaint") MessageBox.Show("Terminado") Word.Visible = True End Functionespero les haya servido el codigo =)
I love music, I love life, I love True Religion UK jeans too.
ResponderEliminarXq me sale que la variable ProgressBar1 no esta declarada
ResponderEliminar