Click here to Skip to main content
14,636,362 members
Rate this:
Please Sign up or sign in to vote.
Good day Folks!

Can I ask for your help? :) My question is how will I export the data from listview to excel and PDF directly without using the database or crystal report. Is there a possible way that I can do that?

Thank you! :)

Kindly check for the picture in the linkbelow

1. View image: listview to excel and pdf[^]

What I have tried:

I tried using crystal report and works fine but I need to export it directly without using third party program (crystal reports)
Posted
Updated 27-Aug-18 3:34am
Rate this:
Please Sign up or sign in to vote.

Solution 1

Going from a listview to a .csv file for Excel isnt hard - in fact, if you look here FileHelpers Library[^] you can probably use that for 1/2 the work - it looks like you'll still need to iterate your listview and build an a list of objects, where a object represents a row and the information within the columns (or perhaps, listview -> DataTable might be an option)

PDF - a couple of options - there's this PDF File Writer C# Class Library (Version 1.19.0 Enhancement: Document links)[^] or iTextSharp ... I cant recall what Uzi's library uses to format a PDF table, some time looking at his examples may help you optimise the data-structure for the export to csv step.

There's many ways to skin a cat
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

Public Sub export_me_to_excel(ByVal list As ListView)
    Try
        Dim objExcel As New Excel.Application
        Dim bkWorkBook As Workbook
        Dim shWorkSheet As Worksheet
        Dim chartRange As Excel.Range
        Dim i As Integer
        Dim j As Integer

        objExcel = New Excel.Application
        bkWorkBook = objExcel.Workbooks.Add
        shWorkSheet = CType(bkWorkBook.ActiveSheet, Worksheet)
        shWorkSheet.DisplayRightToLeft = True

        chartRange = shWorkSheet.Range("a1", "e2")
        chartRange.Merge()
        chartRange.FormulaR1C1 = xlval

        chartRange.HorizontalAlignment = 2
        chartRange.VerticalAlignment = 2

        For i = 0 To list.Columns.Count - 1
            shWorkSheet.Cells(5, i + 1) = list.Columns(i).Text
            shWorkSheet.Columns.AutoFit()
            shWorkSheet.Columns.HorizontalAlignment = Excel.Constants.xlCenter
            shWorkSheet.Columns.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)

        Next
        For i = 0 To list.Items.Count - 1
            For j = 0 To list.Items(i).SubItems.Count - 1
                shWorkSheet.Cells(i + 6, j + 1) = list.Items(i).SubItems(j).Text
                shWorkSheet.Columns.AutoFit()
                shWorkSheet.Columns.HorizontalAlignment = Excel.Constants.xlCenter
                shWorkSheet.Columns.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)

            Next
        Next

        objExcel.Visible = True
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub
   
Comments
Dave Kreskowiak 27-Aug-18 11:08am
   
So, you wrote a piece of code and decided to throw it at anything that mentions exporting to Excel from a ListView?

Unexplained answers are useless.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100