943,699 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 2666
  • VB.NET RSS
Aug 2nd, 2009
0

Exporting data to excel sheet!

Expand Post »
Hi all,
I just need an idea (not code) how to export data from my databse to excel sheet..that too row wise..
I only need a starting idea... m using Visual studio 2008.
Similar Threads
Reputation Points: 22
Solved Threads: 9
Junior Poster
Piya27 is offline Offline
112 posts
since Jun 2009
Aug 2nd, 2009
0

Re: Exporting data to excel sheet!

no idea!!!
Reputation Points: 22
Solved Threads: 9
Junior Poster
Piya27 is offline Offline
112 posts
since Jun 2009
Aug 3rd, 2009
1

Re: Exporting data to excel sheet!

One way to export data to an (existing) Excel file is to use OleDb (namespace System.Data.OleDb). Check out Connection strings for Excel how to form a proper connection string to access an Excel file.

If you want to write a general Excel export, I suggest importing data from the data source to a DataView object and passing that to a routine which export DataView to XLS-file. A DataView object contains both data and field names.

And one thing to remember with Excel files is that the table names are Excel sheet names, for example "[sheet1$]". That caused me a bit headache before I realized it

HTH
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Aug 3rd, 2009
0

Re: Exporting data to excel sheet!

What database are you using? You can also use Excel automation to extract the data but I would use the OleDb approach outlined above.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Aug 4th, 2009
0

Re: Exporting data to excel sheet!

vb.net Syntax (Toggle Plain Text)
  1. Dim con As New SqlConnection(connection_string)
  2. Dim sqlquery As String = ""
  3. con.Open()
  4.  
  5.  
  6. Dim dataExport As String = ""
  7.  
  8. 'File.Create(fpath)
  9.  
  10. Dim cmd As New SqlCommand("Select * From company_profile Where serial_no = '" & TextBox1.Text & "'", con)
  11. Dim str As New StringBuilder
  12. Dim da As New SqlDataAdapter(cmd)
  13. Dim ds As New DataSet
  14. Dim dt As New DataTable
  15. 'Dim firstRecord As Boolean = True
  16. Dim cb As New SqlCommandBuilder(da)
  17.  
  18. da.Fill(ds)
  19. ds.Tables(0).Rows(0).Item(0) = TextBox1.Text
  20. ds.Tables(0).Rows(0).Item(1) = TextBox2.Text
  21. ds.Tables(0).Rows(0).Item(2) = TextBox6.Text
  22. ds.Tables(0).Rows(0).Item(3) = TextBox7.Text
  23. ds.Tables(0).Rows(0).Item(4) = DateTime.Now.ToString
  24. ds.Tables(0).Rows(0).Item(5) = DateTime.Now.ToString
  25.  
  26. '''''''''''Exporting data from GridView to CSV File'''''''''''''''''
  27. GridView1.DataSource = ds.Tables(0)
  28. GridView1.DataBind()
  29.  
  30. GridView1.AutoGenerateColumns = True
  31.  
  32. GridView1.Enabled = True
  33. GridView1.Visible = True
  34.  
  35. 'Extracting Column heading for Header Field
  36. For Each dc As DataGridColumn In GridView1.Columns
  37. dataExport = dataExport & dc.HeaderText
  38. Next
  39.  
  40. dataExport = Environment.NewLine.ToString()
  41.  
  42. For Each drow As GridViewRow In GridView1.Rows
  43.  
  44. For Each dcell As GridViewSelectEventArgs In GridView1.Rows
  45. If dcell Is Nothing Then
  46. dataExport = dataExport & drow.DataItem.ToString & ","
  47. End If
  48. Next
  49. Next
  50.  
  51. dataExport = dataExport & Environment.NewLine.ToString()
  52.  
  53. Dim tw As New System.IO.StreamWriter("E:\SBS_sample.csv")
  54. tw.Write(dataExport)
  55. tw.Close()
this is what i have done,but this is in csv format. i am reading my data from the GridView just for checking of it works, but cudnt get it working.. if any changes??
Last edited by Piya27; Aug 4th, 2009 at 2:40 am.
Reputation Points: 22
Solved Threads: 9
Junior Poster
Piya27 is offline Offline
112 posts
since Jun 2009
Aug 6th, 2009
0

Re: Exporting data to excel sheet!

vb.net Syntax (Toggle Plain Text)
  1. Protected Sub Button15_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button15.Click
  2.  
  3. Dim con As New SqlConnection(connection_string)
  4. 'Dim cmd As New SqlCommand("Select * From company_profile where serial_no = '" & TextBox1.Text & "'", con)
  5.  
  6.  
  7. Dim ad As New SqlDataAdapter("SELECT * FROM company_profile where serial_no = '1'", con)
  8. Dim ds As New DataSet()
  9.  
  10. ad.Fill(ds)
  11.  
  12. GridView1.DataSource = ds
  13. GridView1.DataBind()
  14.  
  15. Dim style As String = "<style> .text { mso-number-format:\@; } </script> "
  16.  
  17. Response.ClearContent()
  18. Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls")
  19. Response.ContentType = "application/excel"
  20.  
  21. Dim sw As New StringWriter()
  22. Dim htw As New HtmlTextWriter(sw)
  23.  
  24. GridView1.RenderControl(htw)
  25. ' Style is added dynamically
  26. Response.Write(style)
  27. Response.Write(sw.ToString())
  28. Response.[End]()
  29. End Sub
  30.  
  31. Private Sub DisableControls(ByVal gv As Control)
  32. Dim lb As New LinkButton()
  33. Dim l As New Literal()
  34.  
  35. Dim name As String = [String].Empty
  36.  
  37. For i As Integer = 0 To gv.Controls.Count - 1
  38. If gv.Controls(i).[GetType]() Is GetType(LinkButton) Then
  39. l.Text = TryCast(gv.Controls(i), LinkButton).Text
  40. gv.Controls.Remove(gv.Controls(i))
  41. gv.Controls.AddAt(i, l)
  42.  
  43. ElseIf gv.Controls(i).[GetType]() Is GetType(DropDownList) Then
  44. l.Text = TryCast(gv.Controls(i), DropDownList).SelectedItem.Text
  45. gv.Controls.Remove(gv.Controls(i))
  46. gv.Controls.AddAt(i, l)
  47. End If
  48.  
  49.  
  50. If gv.Controls(i).HasControls() Then
  51. DisableControls(gv.Controls(i))
  52. End If
  53.  
  54. Next
  55. End Sub
  56.  
  57. Protected Sub Button16_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button16.Click
  58. DisableControls(GridView1)
  59.  
  60. Response.ClearContent()
  61. Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls")
  62. Response.ContentType = "application/excel"
  63.  
  64. Dim sw As New StringWriter()
  65. Dim htw As New HtmlTextWriter(sw)
  66.  
  67. GridView1.RenderControl(htw)
  68. Response.Write(sw.ToString())
  69. Response.[End]()
  70. End Sub
  71.  
  72. Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
  73.  
  74. End Sub
  75.  
  76. Protected Sub gvUsers_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
  77.  
  78. If e.Row.RowType = DataControlRowType.DataRow Then
  79. e.Row.Cells(1).Attributes.Add("class", "text")
  80. End If
  81.  
  82. End Sub

This is what i am able to achieve in it. but now the error comes that could not connect to database.???
Last edited by Piya27; Aug 6th, 2009 at 3:29 am.
Reputation Points: 22
Solved Threads: 9
Junior Poster
Piya27 is offline Offline
112 posts
since Jun 2009
Aug 6th, 2009
0

Re: Exporting data to excel sheet!

any help??
Reputation Points: 22
Solved Threads: 9
Junior Poster
Piya27 is offline Offline
112 posts
since Jun 2009
Aug 7th, 2009
0

Re: Exporting data to excel sheet!

Click to Expand / Collapse  Quote originally posted by Piya27 ...
Hi all,
I just need an idea (not code) how to export data from my databse to excel sheet..that too row wise..
I only need a starting idea... m using Visual studio 2008.

Hi,
Here is my Idea,

Open the excel using the connection,
Query the EXCEL for the data u need,
store it in a dataset,

loop through the dataset for the rows of its table,
get the item details
and use it as the parameter for the database query or stored procedure

Regards
Karthick N
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Karthick N is offline Offline
1 posts
since Aug 2009
Aug 11th, 2009
0

Re: Exporting data to excel sheet!

I was able to export the data from database to excel using gridview but now the problem is how to append new values in the same existing excel sheet that i created earlier while exporting the data from gridview. I keep losing formats in the xcel sheet.

any help?
Reputation Points: 22
Solved Threads: 9
Junior Poster
Piya27 is offline Offline
112 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: My programs won't work on other computers other than mine?
Next Thread in VB.NET Forum Timeline: begginer coding





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC