locked
I want to Import Data from windows application form to Excel,CSV files ? RRS feed

  • Question

  • I am using Datagridview and ListView Controls in my windows desktop application using vb.net.
    I want to Import data directly to Excell sheet and CSV file directly from form.

    Can anybody Give me Idea or Some Sample Code.

    Thanks in Advance.
     
    Thursday, January 8, 2009 2:14 PM

Answers

  • Ejaz,

    there are plenty of examples for this in the forums.  check the express forum also.  i do have examples for what you want on my website as well.  there are some projects on the projects page that deal with csv that might come in handy for you.  hope they help

    Jeff - www.srsoft.us    Database code generator and tutorial


    FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us
    Thursday, January 8, 2009 3:27 PM

All replies

  • Ejaz,

    there are plenty of examples for this in the forums.  check the express forum also.  i do have examples for what you want on my website as well.  there are some projects on the projects page that deal with csv that might come in handy for you.  hope they help

    Jeff - www.srsoft.us    Database code generator and tutorial


    FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us
    Thursday, January 8, 2009 3:27 PM
  • Thank you Jeff for your friendly help and sharing your beneficial website!

    Hi Ejaz,

    Welcome to MSDN forums!

    How to import data from DataGridView to Excel Spreadsheet with VB.NET?

    Please check the following two source-opened examples:

    1. Export data from DataGridView or DataSet to Excel Spreadsheet with VB.NET
    http://69.10.233.10/KB/vb/Senthil_S__Software_Eng_.aspx


    2. Export data from DataGridView to Excel Spreadsheet with VB.NET
    http://xldennis.wordpress.com/2008/02/12/export-data-from-datagridview-to-excel/


    Additionally, you can import data from Excel Spreadsheet into DataGridView like this:
    Firstly please Add Reference to COM component: Microsoft Excel Object Library to your project

    Imports System.Data.OleDb
    Imports Microsoft.Office.Interop.Excel
    Imports Microsoft.Office.Interop
    Public Class Form1
    ' e.g Import data from Sheet1 of C:\myData.XLS into DataGridView
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim DS As DataSet
    Dim MyCommand As OleDb.OleDbDataAdapter
    Dim MyConnection As OleDb.OleDbConnection
    MyConnection = New OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=C:\myData.XLS; Extended Properties=Excel 8.0;")
    MyCommand = New OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
    DS = New System.Data.DataSet()
    MyCommand.Fill(DS)
    DataGridView1.DataSource = DS.Tables(0).DefaultView
    MyConnection.Close()
    End Sub
    End Class

    Check this thread for detail:
    http://forums.msdn.microsoft.com/en-US/vbgeneral/thread/ce4bbe9c-7d13-44c5-a26c-4b84912a51f3/



    Best regards,
    Martin Xie
    Wednesday, January 14, 2009 10:11 AM
  • Hi,

    you can export DataTable to Excel with this Excel C# / VB.NET component. If you DataGridView / ListView are bound to DataTable this would be ideal solution for you.

    This component also supports various Excel formats, like XLS, XLSX, CSV, ODS.

    Here is a sample Excel VB.NET code:

    Dim bindingSource = DirectCast(Me.peopleDataGridView.DataSource, BindingSource)
    
    Dim dataTable = DirectCast(bindingSource.List, DataView).Table
    
    Dim ef = New ExcelFile()
    
    ' Insert data table in worksheet, starting from worksheet's first row and column and include column headers
    ef.Worksheets.Add(dataTable.TableName).InsertDataTable(dataTable, 0, 0, True)
    
    ef.SaveXls(Convert.ToString(dataTable.TableName) & ".xls")
    

    Monday, May 9, 2011 8:51 AM