Exporting data to excel sheet!

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 105
Reputation: Piya27 is an unknown quantity at this point 
Solved Threads: 8
Piya27's Avatar
Piya27 Piya27 is offline Offline
Junior Poster

Exporting data to excel sheet!

 
0
  #1
Aug 2nd, 2009
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.
Regards,
Piya:)
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 105
Reputation: Piya27 is an unknown quantity at this point 
Solved Threads: 8
Piya27's Avatar
Piya27 Piya27 is offline Offline
Junior Poster

Re: Exporting data to excel sheet!

 
0
  #2
Aug 2nd, 2009
no idea!!!
Regards,
Piya:)
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Exporting data to excel sheet!

 
0
  #3
Aug 3rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Exporting data to excel sheet!

 
0
  #4
Aug 3rd, 2009
What database are you using? You can also use Excel automation to extract the data but I would use the OleDb approach outlined above.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 105
Reputation: Piya27 is an unknown quantity at this point 
Solved Threads: 8
Piya27's Avatar
Piya27 Piya27 is offline Offline
Junior Poster

Re: Exporting data to excel sheet!

 
0
  #5
Aug 4th, 2009
  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.
Regards,
Piya:)
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 105
Reputation: Piya27 is an unknown quantity at this point 
Solved Threads: 8
Piya27's Avatar
Piya27 Piya27 is offline Offline
Junior Poster

Re: Exporting data to excel sheet!

 
0
  #6
Aug 6th, 2009
  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.
Regards,
Piya:)
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 105
Reputation: Piya27 is an unknown quantity at this point 
Solved Threads: 8
Piya27's Avatar
Piya27 Piya27 is offline Offline
Junior Poster

Re: Exporting data to excel sheet!

 
0
  #7
Aug 6th, 2009
any help??
Regards,
Piya:)
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 1
Reputation: Karthick N is an unknown quantity at this point 
Solved Threads: 0
Karthick N Karthick N is offline Offline
Newbie Poster

Re: Exporting data to excel sheet!

 
0
  #8
Aug 7th, 2009
Originally Posted by Piya27 View 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.

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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 105
Reputation: Piya27 is an unknown quantity at this point 
Solved Threads: 8
Piya27's Avatar
Piya27 Piya27 is offline Offline
Junior Poster

Re: Exporting data to excel sheet!

 
0
  #9
Aug 11th, 2009
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?
Regards,
Piya:)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC