Export Data To PDF vb.net
We will start making project data export into .pdf format, so just open our visual studio applications, i'll using 2017versions of visual studio, sure you can use more versions of visual studio.Create Project Export PDF
Create new project and rename it with "VB-Net-Export", and on the form1.vb we will design with simple design look like this display :After create design on Form1.vb, we will add References (iTextSharp.dll) into our project, iTextSharp.dll is a .net pdf library and we will import iTextSharp.dll namespaces into form1.vb. you can download iTextSharp.dll . Download and unzip the .dll file and import into references our project :
After all have done, we will leave the form1.vb for a while, we will create a new connections with MySQL Database using ODBC class, so just create a new module and rename it with "ModuleConnection.Vb".
Source Code Module Connection (ModuleConnection.VB)
Imports System.Data.Odbc ' import namespaces ODBCModule ModuleConnectionsPublic koneksi As OdbcConnection ' declaration our connetcions to public classSub OpenCOnnection()Try' create our connection to database using ODBC driverkoneksi = New OdbcConnection("DSN=k13new;MultipleActiveResultSets=True")If koneksi.State = ConnectionState.Closed Then'open our connectionkoneksi.Open()End IfCatch ex As Exception'if connection is filedMsgBox("Connection filed!")End TryEnd SubEnd Module
Source Code Fillter Data (Form1.vb)
Imports System.Data.Odbc ' import namespaces ODBC classImports iTextSharp.text ' import namespaces .net pdf libraryImports iTextSharp.text.pdfImports System.IOPublic Class Form1Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Loadopenconnection() ' open our connectionDim da As OdbcDataAdapter ' declaration data adapterDim dt As DataTable ' declaration data tableda = New OdbcDataAdapter("SELECT idsiswa,nama,nis,tempatlahir,alamat FROM biodata", connection)dt = New DataTableda.Fill(dt)DataGridView1.DataSource = dt ' bind data table into datagridviewDataGridView1.Refresh()connection.Close() ' close our connectionda.Dispose()'configuration fo save file dialogSaveFileDialog1.FileName = ""SaveFileDialog1.Filter = "PDF (*.pdf)|*.pdf"TextBox1.Text = "" ' for titleTextBox2.Text = "" ' for file locationsEnd SubPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickSaveFileDialog1.FileName = ""If SaveFileDialog1.ShowDialog = DialogResult.OK Then' declaration textbox2 to save file dialog nameTextBox2.Text = SaveFileDialog1.FileNameEnd IfEnd SubPrivate Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click' you must import itextsharp namespace into our form' download links is available in the descriptionsDim Paragraph As New Paragraph ' declaration for new paragraphDim PdfFile As New Document(PageSize.A4, 40, 40, 40, 20) ' set pdf page sizePdfFile.AddTitle(TextBox1.Text) ' set our pdf titleDim Write As PdfWriter = PdfWriter.GetInstance(PdfFile, New FileStream(TextBox2.Text, FileMode.Create))PdfFile.Open()' declaration font typeDim pTitle As New Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 14, iTextSharp.text.Font.BOLD, BaseColor.BLACK)Dim pTable As New Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)' insert title into pdf fileParagraph = New Paragraph(New Chunk(TextBox1.Text, pTitle))Paragraph.Alignment = Element.ALIGN_CENTERParagraph.SpacingAfter = 5.0F' set and add page with current settingsPdfFile.Add(Paragraph)' create data into tableDim PdfTable As New PdfPTable(DataGridView1.Columns.Count)' setting width of tablePdfTable.TotalWidth = 500.0FPdfTable.LockedWidth = TrueDim widths(0 To DataGridView1.Columns.Count - 1) As SingleFor i As Integer = 0 To DataGridView1.Columns.Count - 1widths(i) = 1.0FNextPdfTable.SetWidths(widths)PdfTable.HorizontalAlignment = 0PdfTable.SpacingBefore = 5.0F' declaration pdf cellsDim pdfcell As PdfPCell = New PdfPCell' create pdf headerFor i As Integer = 0 To DataGridView1.Columns.Count - 1pdfcell = New PdfPCell(New Phrase(New Chunk(DataGridView1.Columns(i).HeaderText, pTable)))' alignment header tablepdfcell.HorizontalAlignment = PdfPCell.ALIGN_LEFT' add cells into pdf tablePdfTable.AddCell(pdfcell)Next' add data into pdf tableFor i As Integer = 0 To DataGridView1.Rows.Count - 2For j As Integer = 0 To DataGridView1.Columns.Count - 1pdfcell = New PdfPCell(New Phrase(DataGridView1(j, i).Value.ToString(), pTable))PdfTable.HorizontalAlignment = PdfPCell.ALIGN_LEFTPdfTable.AddCell(pdfcell)NextNext' add pdf table into pdf documentPdfFile.Add(PdfTable)PdfFile.Close() ' close all sessions' show message if hasben exportedMessageBox.Show("PDF format success exported !", "Informations", MessageBoxButtons.OK, MessageBoxIcon.Information)End SubPrivate Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.ClickMe.Close()End SubPrivate Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClickedProcess.Start("www.hc-kr.com")End SubEnd Class
Suuuupppppppeerr!!!
RépondreSupprimer