473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How do I export sql data to an excel spreadsheet?

I have a search page in my program where users can search for member data. The results come up in the data grid view, but I also want users to be able to export the search results into an Excel spreadsheet. What is the best way to do this?
Oct 12 '07 #1
2 2193
iburyak
1,017 Expert 512MB
Here is my VB6 function where I can pass highlited part of a grid or whole thing to Excel.

Expand|Select|Wrap|Line Numbers
  1. Public Function SaveFlexGridToExcel(TheFlexgrid As MSFlexGrid, _
  2.                              TheRows As Integer, TheCols As Integer, _
  3.                              Optional GridStyle As Integer = 1, Optional WorkSheetName As String)
  4.  
  5. Dim objXL 'As New Excel.Application
  6. Dim wbXL 'As New Excel.Workbook
  7. Dim wsXL 'As New Excel.Worksheet
  8. Dim intRow As Integer ' counter
  9. Dim intCol As Integer ' counter
  10.  
  11. On Error GoTo ErrorHandler
  12.  
  13. Set objXL = CreateObject("Excel.Application.11")
  14.  
  15. If Not IsObject(objXL) Then
  16.     MsgBox "You need Microsoft Excel to use this function", _
  17.        vbExclamation, "Print to Excel"
  18.     Exit Function
  19. End If
  20.  
  21. ' open Excel
  22. objXL.Visible = True
  23. Set wbXL = objXL.Workbooks.Add
  24. Set wsXL = objXL.ActiveSheet
  25.  
  26. ' name the worksheet
  27.     If Not WorkSheetName = "" Then wsXL.Name = WorkSheetName
  28.  
  29. ' fill worksheet
  30. 'Skip first column
  31. For intRow = 1 To TheRows
  32.     For intCol = 2 To TheCols
  33.         With TheFlexgrid
  34.             wsXL.Cells(intRow, intCol - 1).Value = .TextMatrix(intRow - 1, intCol - 1) & " "
  35.         End With
  36.     Next
  37. Next
  38.  
  39. ' format the look
  40. For intCol = 1 To TheCols
  41.     wsXL.Columns(intCol).AutoFit
  42.     'wsXL.Columns(intCol).AutoFormat (1)
  43. Next
  44.  
  45. 'wsXL.Range("a1", Right(wsXL.Columns(TheCols).AddressLocal, 1) & TheRows).AutoFormat GridStyle
  46.  
  47. Set objXL = Nothing
  48. Screen.MousePointer = vbNormal
  49.  
  50. Exit Function
  51. ErrorHandler:
  52. MsgBox Err.Description, vbOKOnly + vbExclamation, " [SaveFlexGridToExcel]"
  53. End Function
Hope it helps.

Good Luck.
Oct 12 '07 #2
Here is my VB6 function where I can pass highlited part of a grid or whole thing to Excel.

Expand|Select|Wrap|Line Numbers
  1. Public Function SaveFlexGridToExcel(TheFlexgrid As MSFlexGrid, _
  2.                              TheRows As Integer, TheCols As Integer, _
  3.                              Optional GridStyle As Integer = 1, Optional WorkSheetName As String)
  4.  
  5. Dim objXL 'As New Excel.Application
  6. Dim wbXL 'As New Excel.Workbook
  7. Dim wsXL 'As New Excel.Worksheet
  8. Dim intRow As Integer ' counter
  9. Dim intCol As Integer ' counter
  10.  
  11. On Error GoTo ErrorHandler
  12.  
  13. Set objXL = CreateObject("Excel.Application.11")
  14.  
  15. If Not IsObject(objXL) Then
  16.     MsgBox "You need Microsoft Excel to use this function", _
  17.        vbExclamation, "Print to Excel"
  18.     Exit Function
  19. End If
  20.  
  21. ' open Excel
  22. objXL.Visible = True
  23. Set wbXL = objXL.Workbooks.Add
  24. Set wsXL = objXL.ActiveSheet
  25.  
  26. ' name the worksheet
  27.     If Not WorkSheetName = "" Then wsXL.Name = WorkSheetName
  28.  
  29. ' fill worksheet
  30. 'Skip first column
  31. For intRow = 1 To TheRows
  32.     For intCol = 2 To TheCols
  33.         With TheFlexgrid
  34.             wsXL.Cells(intRow, intCol - 1).Value = .TextMatrix(intRow - 1, intCol - 1) & " "
  35.         End With
  36.     Next
  37. Next
  38.  
  39. ' format the look
  40. For intCol = 1 To TheCols
  41.     wsXL.Columns(intCol).AutoFit
  42.     'wsXL.Columns(intCol).AutoFormat (1)
  43. Next
  44.  
  45. 'wsXL.Range("a1", Right(wsXL.Columns(TheCols).AddressLocal, 1) & TheRows).AutoFormat GridStyle
  46.  
  47. Set objXL = Nothing
  48. Screen.MousePointer = vbNormal
  49.  
  50. Exit Function
  51. ErrorHandler:
  52. MsgBox Err.Description, vbOKOnly + vbExclamation, " [SaveFlexGridToExcel]"
  53. End Function
Hope it helps.

Good Luck.



Do you have anything in C#??
Oct 15 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: cpeters5 | last post by:
Deaa group, I am using SQLServer 2000 in an XP Sp2. I would like to do the following: I have a program running on a database server that generates some data which are loaded to the database....
0
by: Jason | last post by:
Can anyone point me to some sample code or tutorial that shows how I can export data from a database to an Excel spreadsheet that is already created on the server? I have an Excel spreadsheet...
4
by: Gary Wright | last post by:
I have an Access 2K database split into front and back. Quite often the users want to do some data analysis that I have not created a report for so they want to export some subset of the data into...
2
by: OptikConnex | last post by:
I have data that I need to either put in a spreadsheet, or use in a listview to view it. I would rather use the listview but I don't understand how to put my data into it. Here is the code I have,...
3
by: Scott M. Lyon | last post by:
I'm trying to figure out a way to export data (actually the result of a Stored Procedure call from SQL Server) into a specified Excel spreadsheet format. Currently, I have the data read into a...
0
by: JLuv | last post by:
What i want to do is take the checked data from a datagrid, send it to a table, then show that table on an Excel Spreadsheet. Here is a little bit of my code... //adminProgress.aspx...
2
by: nofear | last post by:
I used to export my reports as snapshot but now I have to export them to Excel When I export my report to a Excel Spreadsheet the report header and footer are not included Only the data gets...
3
by: yovation | last post by:
Hi, I have a 3 table database. 1 parent 1 child 1 child of child I would like to get the data into excel as 1 sheet (similar to a grouped report).
0
by: =?Utf-8?B?RXJpayBLLiBTY2hpbmRlbGRlY2tlcg==?= | last post by:
Good Afternoon, I am trying to programmatically define our company logo in the header of a ..Net Export to Excel spreadsheet. I have tried to find some type of MetaData associated with...
0
by: Stephen Wallace | last post by:
Hi experts, i want to know what's the best way to export data from spreadsheet to datatable? maybe, any way that can export data from spreadsheet to datatable. Thanks for any answers.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.