VB.NET

Moderators: seancampbell
Number of threads: 3879
Number of posts: 9845

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
exporting to excel Posted by azdonald on 14 Dec 2009 at 6:30 AM
i'd like to export to excel and this is the code i have. Problem is i'd like the user to select location and name of document by themselves..



Dim xlApp As excel.Application
Dim xlWorkBook As excel.Workbook
Dim xlWorkSheet As excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer

xlApp = New excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")

For i = 0 To DataGridView1.RowCount - 2
For j = 0 To DataGridView1.ColumnCount - 1
xlWorkSheet.Cells(i + 1, j + 1) = _
DataGridView1(j, i).Value.ToString()
Next
Next

xlWorkSheet.SaveAs("C:\vbexcelreport.xlsx")
xlWorkBook.Close()
xlApp.Quit()

releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)

MsgBox("You can find the file C:\vbexcelreport.xlsx")
Report
Re: exporting to excel Posted by seancampbell on 14 Dec 2009 at 7:16 AM
I am going to assume that code is working for you (I don't use built in libraries to make Excel documents, I use Excel 2003/2007's XML format)....

So if it is working, all you need is to know how to Select a file location for a user to save to... This is pretty easy with .Net:

                Dim SFD As SaveFileDialog
                SFD = New SaveFileDialog()
                SFD.InitialDirectory = Application.StartupPath
                SFD.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
                SFD.CheckFileExists = True

                If SFD.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    'The user clicked Open or Double Clicked the File
                    'Do what we need with the file now
                    Dim FileName As String = SFD.FileName
                    MessageBox.Show(FileName)
                End If


The last If statement is short hand for this:
Dim DR as DialogResult = SFD.ShowDialog()

If DR = Windows.Forms.DialogResult.OK Then '.....


hope this helps
Report
Re: exporting to excel Posted by azdonald on 29 Dec 2009 at 5:43 AM
thanks. but i just discovered that the header is not exported along.. i.e the column names are not exported.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.