Export Data from Listview to PDF in C#, VB.NET

2011-08-02 08:36:45 Written by  support iceblue
Rate this item
(1 Vote)

Because of safety, easy to transfer from one to another and also easy to use, PDf is now becoming No. 1 file format all over the world. Usually customers will need export data from database to PDF file format. Spire.DataExport for .NET is a .NET component suit, 100% pure managed by C# for data exporting from database such as ListView, datatable and SQL command into MS Excel,MS Word, HTML, XML, PDF, MS Access, DBF, SQL Script, SYLK, DIF, CSV ,MS Clipboard format.

Spire.DataExport for .Net provides a quick and easy solution to export data from Listview to PDF. With Spire.DataExport for .Net, users also have other rich options when export data from database.

Now Download Spire.DataExport (or Spire.Office) with .NET framework together and use the code below to export data from Listview to PDF with C#/VB.NET.

[C#]
protected void Button1_Click(object sender, System.EventArgs e)
	{

	    oleDbConnection1.Open();
            this.cellExport1.ActionAfterExport = DataExport.Common.ActionType.None;
	    this.cellExport1.DataFormats.CultureName = "zh-CN";
	    this.cellExport1.DataFormats.Currency = "?#,###,##0.00";
	    this.cellExport1.DataFormats.DateTime = "yyyy-M-d H:mm";
	    this.cellExport1.DataFormats.Float = "#,###,##0.00";
	    this.cellExport1.DataFormats.Integer = "#,###,##0";
	    this.cellExport1.DataFormats.Time = "H:mm";
	    this.cellExport1.SheetOptions.AggregateFormat.Font.Name = "Arial";
	    this.cellExport1.SheetOptions.CustomDataFormat.Font.Name = "Arial";
	    this.cellExport1.SheetOptions.DefaultFont.Name = "Arial";
	    this.cellExport1.SheetOptions.FooterFormat.Font.Name = "Arial";
	    this.cellExport1.SheetOptions.HeaderFormat.Font.Name = "Arial";
	    this.cellExport1.SheetOptions.HyperlinkFormat.Font.Color = Spire.DataExport.XLS.CellColor.Blue;
	    this.cellExport1.SheetOptions.HyperlinkFormat.Font.Name = "Arial";
	    this.cellExport1.SheetOptions.HyperlinkFormat.Font.Underline = Spire.DataExport.XLS.XlsFontUnderline.Single;
	    this.cellExport1.SheetOptions.NoteFormat.Alignment.Horizontal = Spire.DataExport.XLS.HorizontalAlignment.Left;
	    this.cellExport1.SheetOptions.NoteFormat.Alignment.Vertical = Spire.DataExport.XLS.VerticalAlignment.Top;
	    this.cellExport1.SheetOptions.NoteFormat.Font.Bold = true;
	    this.cellExport1.SheetOptions.NoteFormat.Font.Name = "Tahoma";
	    this.cellExport1.SheetOptions.NoteFormat.Font.Size = 8F;
	    this.cellExport1.SheetOptions.TitlesFormat.Font.Bold = true;
	    this.cellExport1.SheetOptions.TitlesFormat.Font.Name = "Arial";
            this.cellExport1.DataSource = DataExport.Common.ExportSource.ListView;
            this.cellExport1.ListView = this.listView1;

            Using(MemoryStream stream = new MemoryStream())
                    {
                           cellExport1.SaveToFile(stream);
                           this.oleDbConnection1.Close();
                           Workbook workbook = new Workbook(stream);
                           PdfConverter pdfConverter = new PdfConverter(workbook);

                           PdfDocument pdfDocument = new PdfDocument();
                           pdfDocument.PageSettings.Orientation = pdf.PdfPageOrientation.Landscape;
                           pdfDocument.PageSettings.Width = 970;
                           pdfDocument.PageSettings.Height = 850;

                           PdfConverterSettings settings = new PdfConverterSettings();
                           settings.TemplateDocument = pdfDocument;
                           pdfDocument = pdfConverter.Convert(settings);

                           pdfDocument.SaveToFile("test.pdf");

                    }
       }
[VB.NET]
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

			oleDbConnection1.Open()
			Me.cellExport1.ActionAfterExport = DataExport.Common.ActionType.None
			Me.cellExport1.DataFormats.CultureName = "zh-CN"
			Me.cellExport1.DataFormats.Currency = "?#,###,##0.00"
			Me.cellExport1.DataFormats.DateTime = "yyyy-M-d H:mm"
			Me.cellExport1.DataFormats.Float = "#,###,##0.00"
			Me.cellExport1.DataFormats.Integer = "#,###,##0"
			Me.cellExport1.DataFormats.Time = "H:mm"
			Me.cellExport1.SheetOptions.AggregateFormat.Font.Name = "Arial"
			Me.cellExport1.SheetOptions.CustomDataFormat.Font.Name = "Arial"
			Me.cellExport1.SheetOptions.DefaultFont.Name = "Arial"
			Me.cellExport1.SheetOptions.FooterFormat.Font.Name = "Arial"
			Me.cellExport1.SheetOptions.HeaderFormat.Font.Name = "Arial"
			Me.cellExport1.SheetOptions.HyperlinkFormat.Font.Color = Spire.DataExport.XLS.CellColor.Blue
			Me.cellExport1.SheetOptions.HyperlinkFormat.Font.Name = "Arial"
			Me.cellExport1.SheetOptions.HyperlinkFormat.Font.Underline = Spire.DataExport.XLS.XlsFontUnderline.Single
			Me.cellExport1.SheetOptions.NoteFormat.Alignment.Horizontal = Spire.DataExport.XLS.HorizontalAlignment.Left
			Me.cellExport1.SheetOptions.NoteFormat.Alignment.Vertical = Spire.DataExport.XLS.VerticalAlignment.Top
			Me.cellExport1.SheetOptions.NoteFormat.Font.Bold = True
			Me.cellExport1.SheetOptions.NoteFormat.Font.Name = "Tahoma"
			Me.cellExport1.SheetOptions.NoteFormat.Font.Size = 8F
			Me.cellExport1.SheetOptions.TitlesFormat.Font.Bold = True
			Me.cellExport1.SheetOptions.TitlesFormat.Font.Name = "Arial"
			Me.cellExport1.DataSource = DataExport.Common.ExportSource.ListView
			Me.cellExport1.ListView = Me.listView1
                            Dim stream As New MemoryStream()
                            cellExport1.SaveToFile(stream)
                            stream.Close()
                            Me.oleDbConnection1.Close()
                            Dim workbook As New Workbook(stream)		

                            Dim pdfConverter As New PdfConverter(workbook)

			Dim pdfDocument As New PdfDocument()
			pdfDocument.PageSettings.Orientation = pdf.PdfPageOrientation.Landscape
			pdfDocument.PageSettings.Width = 970
			pdfDocument.PageSettings.Height = 850

			Dim settings As New PdfConverterSettings()
			settings.TemplateDocument = pdfDocument
			pdfDocument = pdfConverter.Convert(settings)

			pdfDocument.SaveToFile("test.pdf")

End Sub

Additional Info

  • tutorial_title: Export Data to PDF
Last modified on Friday, 27 October 2023 01:39