Results 1 to 9 of 9

Thread: export csv to excel template

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    16

    export csv to excel template

    Hi,

    I have One Form with and Import Button and Excel Spreadsheet in it. When user click on import button it will ask for the csv file then will export the csv file into my excel template and display it in the spreadsheet control already in my form.

    Can you provide me a code on how to do this.

    Another problem I had was that my vb.net is saying that excel.application in
    VB Code:
    1. Dim xcl as Excel.application
    declaration is not declared when infact I had already added a reference to Microsoft Excel 11.0 object library and i also added a reference to Microsoft Office 11.0 object library.

    Please help mo on this.

    Using VB 2003/1.1 and Office 2003

    THanks,
    Ian
    Last edited by h1dden; Mar 23rd, 2006 at 09:25 PM. Reason: change title

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    16

    Re: export csv to excel template

    anybody who could help me on this?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: export csv to excel template

    Read the CSV using ADO.NET. It works the very same way as reading from Access. See www.connectionstrings.com for details of how to tailor your connection string and SQL code. Alternatively you could read the data in a loop using a StreamReader. Once you have the data it's up to you how you get it into your spreadsheet as we don't know anything about the structure.

    As for your second problem, adding a reference to a library gives you access to all the types it contains. You still need to specify those types by name though. You either need to qualify your types with the namespace they are members of or else import that namespace, either project-wide in the properties or else for the current file at the top of the code. You would qualify the class name like this:
    VB Code:
    1. Dim xcl As Microsoft.Office.Interop.Excel.Application
    Doing that every time gets very laborious though, so it is a good idea to import the namespace. With Office apps it is regular to do so with an alias:
    VB Code:
    1. Imports Excel = Microsoft.Office.Interop.Excel
    Once you've added that at the top of your code "Excel" becomes an alias for "Microsoft.Office.Interop.Excel". The alternative would be to just import the Interop namespace:
    VB Code:
    1. Imports Microsoft.Office.Interop
    Your code would then look the same but using "Excel" in your code would have a subtly different meaning. The net result is the same though so whichever you like.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: export csv to excel template

    You will only get the .Interop class if you have installed the .NET Primary Interop Assemblies. Checkout the link in my signature for more info and installation procedures.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: export csv to excel template

    Just noticed that your running 2003 so I wrote a quick example for you.
    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3. 'Add a reference to MS Excel xx.0 Object Library
    4. Imports Microsoft.Office.Interop
    5.  
    6. Public Class Form1
    7.  
    8.     Inherits System.Windows.Forms.Form
    9.  
    10.  
    11.     Private moApp As Excel.Application
    12.  
    13.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    14.         Try
    15.             '<INITIALIZE EXCEL>
    16.             moApp = DirectCast(GetObject(, "Excel.Application"), Excel.Application)
    17.         Catch ex As Exception
    18.             '<CREATE EXCEL SINCE NOT RUNNING>
    19.             If TypeName(moApp) = "Nothing" Then
    20.                 moApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
    21.             Else
    22.                 MessageBox.Show(ex.Message, "VB/Office Guru™ Excel Demo™ .NET", _
    23.                 MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    24.             End If
    25.         End Try
    26.     End Sub
    27.  
    28.     Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    29.         If Not moApp Is Nothing Then
    30.             moApp.Quit()
    31.         End If
    32.         moApp = Nothing
    33.         System.Runtime.InteropServices.Marshal.ReleaseComObject(moApp)
    34.     End Sub
    35.  
    36.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    37.         Dim oWB As Excel.Workbook = moApp.Workbooks.Add
    38.         Dim oSht As Excel.Worksheet = DirectCast(oWB.Sheets("Sheet1"), Excel.Worksheet)
    39.         oSht.Cells(1, 1) = "2"
    40.         oSht.Cells(1, 2) = "2"
    41.         oSht.Cells(1, 3) = "=SUM(A1, B1)"
    42.         moApp.Visible = True
    43.         MessageBox.Show("Before Calculate()")
    44.         oSht.Cells(1, 1) = "3"
    45.         oSht.Calculate()
    46.         MessageBox.Show("After Calculate()")
    47.         oWB.Close(True, "C:\Test.xls")
    48.         oSht = Nothing
    49.         oWB = Nothing
    50.     End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    16

    Re: export csv to excel template

    Hi,

    THanks for the reply. I am attaching my VB.net form for clarity.

    Here is what i want to do.

    1. At form load the excel spreadsheet in my form will load a blank template which contains formatting(cells that are protected).
    2. Click on Import button, the program will ask for the csv file and then populate the content of it to the excel template.
    3. Save it as CSV file, but there will be an option for the user to save/export only modified rows.

    Hope you could help.

    Thanks,
    Ian
    Attached Images Attached Images  

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    16

    Re: export csv to excel template

    I have also attached my csv file.

    When i open my csv file in the spreadsheet control using
    VB Code:
    1. cd.Filter = "CSV File(*.CSV)|*.CSV;*.csv | All files (*.*)|*.*"
    2.         cd.FilterIndex = 0
    3.         cd.InitialDirectory = Application.ExecutablePath
    4.         cd.ShowDialog(Me)
    5.  
    6.         AxSpreadsheet1.CSVURL = cd.FileName
    the values like 000,1101110010005 and the likes where not displayed exactly as the csv file. 000 is replaced by 0 and 1101110010005 is replaced by 1.10111E+12. I want all values to just appear exactly as it is in the csv file. anybody can help me on this?

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: export csv to excel template

    Format the excel columns as text instead of numbers.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    16

    Re: export csv to excel template

    any idea about my other questions?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width