This article will show you how to use Spire.DataExport to Export Datatable to PDF through DataGridView. Through DataGridView, users can preview data information and modify the information if they think it’s not correct before exporting data. With Spire.DataExport, this whole exporting process could be fast and easy.
Download Spire.DataExport (or Spire.Office) with .NET Framework together. Only 2 Simple steps you can finish the whole datatable to PDF exporting process.
Step 1: Load Data Information
Before exporting data from DataTable, we should load data information from data source. And select which information we need export. Through DataGridVew, we even can preview and modify data information. So, in this step, our job is to prepare data which is about to be exported out.
private void btnLoad_Click(object sender, EventArgs e) { using (OleDbConnection oleDbConnection = new OleDbConnection()) { oleDbConnection.ConnectionString = this.tbCS.Text; OleDbCommand oleDbCommand = new OleDbCommand(); oleDbCommand.CommandText = this.tbCT.Text; oleDbCommand.Connection = oleDbConnection; using (OleDbDataAdapter da = new OleDbDataAdapter(oleDbCommand)) { DataTable dt = new DataTable(); da.Fill(dt); dataGridView.DataSource = dt; } } }
Effect Screenshot
Step 2: Set Export into PDF
Spire.DataExport allows user to export data into most popular file formats including MS Excel, MS Word, HTML, PDF, XML, CSV, DBF, DIF, etc. Here we need set it as PDF format. Spire.DataExport will create a new PDF file and through DataGridView export data into PDF file. You also can rename the file as you like.
private void btnRun_Click(object sender, EventArgs e) { Spire.DataExport.PDF.PDFExport PDFExport = new Spire.DataExport.PDF.PDFExport(); PDFExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable; PDFExport.DataTable = this.dataGridView.DataSource as DataTable; PDFExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView; PDFExport.SaveToFile("20110223.pdf"); }
Effect Screenshot