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:
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
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:
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:
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:
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.
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.
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.
When i open my csv file in the spreadsheet control using
VB Code:
cd.Filter = "CSV File(*.CSV)|*.CSV;*.csv | All files (*.*)|*.*"
cd.FilterIndex = 0
cd.InitialDirectory = Application.ExecutablePath
cd.ShowDialog(Me)
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?