Avatar of frankie_jnr
frankie_jnr
 asked on

exporting data

Hi all,

I have a datagrid that displays some data for me what i need to do now is add a button to export the data to a excel file. I am using vb.net 2005

Thanks for the help

Frank
myadapter = New MySqlDataAdapter
        myData = New DataSet
        Me.DataGridView1.Columns.Clear()
        Searchvalue = ""
        Searchvalue = InputBox("Enter Date", "Search", "", 100, 100)
        Dim conn As New MySqlConnection
        Dim cmd As New MySqlCommand
        conn.ConnectionString = _
                        "server=10.0.0.10;" _
                        & "uid=xxxx;" _
                        & "pwd=xxxx;" _
                        & "database=dnote_db"
        Try
            conn.Open()
            cmd.CommandText = "SELECT `time_entered`,`ord_nr`,`supplier`,`depot`,`product`,`qty`,`user_entered`,`time_printed` FROM data WHERE `depot` LIKE '%wenkem%' AND `time_entered` LIKE '%" & Searchvalue & "%' AND `log` NOT LIKE 'Original Row nr :' OR `depot` LIKE '%wenkem%' AND `user_entered` LIKE '%" & Searchvalue & "%' OR `depot` LIKE '%wenkem%' AND `time_printed` LIKE '%" & Searchvalue & "%' order by `time_entered`"
            cmd.Connection = conn
            myadapter.SelectCommand = cmd
            myadapter.Fill(myData)
            Me.DataGridView1.DataSource = myData.Tables(0)
            Me.DataGridView1.AutoResizeColumns()
 
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Report could not be created", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

Open in new window

Visual Basic.NETVisual Basic Classic

Avatar of undefined
Last Comment
game-master

8/22/2022 - Mon
Omego2K

here is an article that provides a way to do it:

http://www.codeproject.com/KB/grid/ExportDatagridviewToExcel.aspx

Excel must be installed on the system using it though.
frankie_jnr

ASKER
if i can output the grid to a csv file it would be fine to
game-master

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
game-master



this link can give u lot of idea...

http://support.microsoft.com/kb/306022/


game-master
frankie_jnr

ASKER
hi there i have had a look but am not to sure how i can use it all the examles i cant see them using data from a datagrid and the microsoft site has many defferent ways to do this i am not sure what the best way would be

thanks for the help
frankie_jnr

ASKER
is there not an easyier way to just export to csv? i am in trouble a bit i need to have somthing this afternoon ;) sorry to keep nagging
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
game-master

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
frankie_jnr

ASKER
I have found a way that exports it and opens excel automatically. My only question is is there a way to auto size the column so that all data is displayed? I am attaching the code  
Dim wapp As Microsoft.Office.Interop.Excel.Application
 
        Dim wsheet As Microsoft.Office.Interop.Excel.Worksheet
 
        Dim wbook As Microsoft.Office.Interop.Excel.Workbook
 
        wapp = New Microsoft.Office.Interop.Excel.Application
 
        wapp.Visible = True
 
        wbook = wapp.Workbooks.Add()
 
        wsheet = wbook.ActiveSheet
 
        Dim iX As Integer
 
        Dim iY As Integer
 
        Dim iC As Integer
 
        For iC = 0 To DataGridView1.Columns.Count - 1
 
            wsheet.Cells(1, iC + 1).Value = DataGridView1.Columns(iC).HeaderText
 
            wsheet.Cells(1, iC + 1).font.bold = True
 
        Next
 
        wsheet.Rows(2).select()
 
        For iX = 0 To DataGridView1.Rows.Count - 2
 
            For iY = 0 To DataGridView1.Columns.Count - 1
 
                wsheet.Cells(iX + 2, iY + 1).value = DataGridView1(iY, iX).Value.ToString
 
            Next
 
        Next
 
        wapp.Visible = True
 
        wapp.UserControl = True

Open in new window

Nasir Razzaq

You can use the Range.Autofit method.
http://msdn.microsoft.com/de-de/library/microsoft.office.interop.excel.range.autofit%28office.11%29.aspx

wsheet.Range("A1", "H10").AutoFit()
frankie_jnr

ASKER
Thank You for the help
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Nasir Razzaq

Did my post help at all?
game-master



im glad i could help you...



game-master