Technical Support, Instructions & Repair Service

Business and Productivity Software
Generic problem for all Business and Productivity Software

Export data in excel shld yoeet through VB

posted by sneha2008 on Jan 03, 2008


Hi,

I want to execute the output of the folowing query
SELECT IPADDRESS FROM TRANSFER_MANAGER

I want ipaddress to be dispalyed in the 3rd column of the excel sheet.

Could you please help me how can i write the code
I have the same problem
This Problem has been added to the Share Your Expertise Page under "My Work Queue".

Solution #1

posted on Jan 04, 2008
Not Rated (0)

Stonebraker

Rank:Wiz Wiz
Rating: 84%, 108 votes
When i first figured out how to pull data from SQL and put the results in an excel file i referenced these two articles....
Reading and writing excel file using VB.NET (http://www.codeproject.com/KB/vb/Work_with_Excel__VBNET_.aspx)
Get the Values From DataBase and Stored into excell Sheet (http://www.codeproject.com/KB/vb/Getvaluesfromdatabase.aspx)

This is the code i ended up using.... (check out those links to see how you need to import the ms office excel reference file with visual basic)

Const stcon As String = "Provider=SQLNCLI;server=xxxxx;database=xxxxx;uid=xxxxx;pwd=xxxxx;DataTypeCompatibility=80"
Dim stSQL As String = "select * from scs_rate_class_money where irate_book = 124 and snew_used = 'U' and sclass = '2' and splan = 'T4' and sopt_code = 'F1'"
Dim cnt As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim fld As ADODB.Field
'Open the connection.
cnt.Open(stcon)

'Open the recordset.
With rst
.CursorLocation = ADODB.CursorLocationEnum.adUseClient
.Open(stSQL, cnt, ADODB.CursorTypeEnum.adOpenForwardOnly, _
ADODB.LockTypeEnum.adLockReadOnly , _
ADODB.CommandTypeEnum.adCmdText)
.ActiveConnection = Nothing 'Disconnect the Recordset.
End With
'Close the connection
cnt.Close ()
Dim exp As Export = New Export()
Dim xlApp As New Microsoft.Office.Interop.Excel.Application
Dim xlWBook As Microsoft.Office.Interop.Excel.Workbook = xlApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet )
Dim xlWSheet As Microsoft.Office.Interop.Excel.Worksheet = CType(xlWBook.Worksheets(1), Microsoft.Office.Interop.Excel.Worksheet)
Dim xlRange As Microsoft.Office.Interop.Excel.Range = CType(xlWSheet, Microsoft.Office.Interop.Excel.Worksheet).Range("A2")
Dim xlCalc As Microsoft.Office.Interop.Excel.XlCalculation
Dim i As Short

'Turn off Excel's calculation.
With xlApp
xlCalc = .Calculation
.Calculation = Microsoft.Office.Interop.Excel.XlCalculation.xlCalculationManual
End With
'Write the fieldnames.
For Each fld In rst.Fields
xlRange.Offset(0, i).Value = fld.Name
i = i + 1
Next
'Populate the range.
xlRange.Offset(1, 0).CopyFromRecordset(rst)
'Close the recordset.
rst.Close()
'Make Excel available to the user.
With xlApp
.Visible = True
.UserControl = True
'Restore the calculation mode.
.Calculation = xlCalc
End With
'Release variables from memory.
fld = Nothing
rst = Nothing
cnt = Nothing
xlRange = Nothing
xlWSheet = Nothing
xlWBook = Nothing
xlApp = Nothing

Was this solution helpful? Show your Appreciation by rating it:

Thank You!

Was the solution helpful?
Show your appreciation by commenting on Export data in excel shld yoeet through VB:


I don't want to Accept this solution

Can you Help with these Business and Productivity Software problems?

Microsoft Office 2003 Basic Edition English...
I need a formula for an interest rate worksheet. Cell A1 =...

Pinnacle Studio Plus 10 Full Version for PC
I have successfully used Pinnacle Studio 10 Plus with my...

Screenplay Systems Writer's Dreamkit 4.0...
I bring up the menu and I get no action when I push the...

Handmark Software MobileLists (950142) for...
hi I have an sigmatel s4 china mobile I want to know...

Microsoft Office Professional 2007 Full...
I'm planning on getting a new Macbook, but I don't want to...

Loading problems.

Repair Service

Find Business and Pro. Repair Service Provider Near You:
Browse the Repair Service Directory
Repair Service Providers, Get Listed FREE

When the original poster rates a solution that was given to his own problem, that rating is locked!
X

Are you sure the solution content is Inappropriate?
   
Tech buddies can communicate directly to answer questions. Become a Tech Buddy and have direct access to your favorite expert for FREE!