Business and Productivity Software
Problem for Business and Productivity Software
Generic problem for all Business and Productivity Sof...

Export data in excel shld yoeet through VB




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

Solution #1

posted on Jan 04, 2008
Not Rated)

Stonebraker

Rank: Guru 
Rating: 85%, 145 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 helpful?
Yes
No

Popular Solutions for Business and Productivity


Questions and Unsolved Problems for Business and Productivity


Answer
I'm evaluating MyMailList Deluxe. Is there a tutorial? I want to print a roster that I can design... (More)

Answer
I upgraded to Showbiz 2 from Arcsoft and now I get this error message: the procedure entry point... (More)

Answer
How to print label for certain customer address only from ACCPAC ?

Answer
Hello, I have the WD 500g MY Book with WD Smartware. The initial back-up worked fine but I am having... (More)


Didn't find what you were looking for?

Describe your problem:

Select a Category:







Ask our Experts

 

Solve Your Problem Now!
Chat Live with an Expert
Chat Now
Browse popular Problems
More Common Problems
Most Common Problems for:
For Business and Productivity...:

Top Business and Pro... Experts

Rank: Guru Guru  

Solutions: 1357
Member Since: July 2009

Experience: I have owned a computer networking sales and service company.

Ask Me
Find more Business and Pro... Experts

Top Computer & Laptop Repair

(877) 729-0812
We connect to your computer over the Internet to...
Remote Repair

(513) 874-3306
We fix and repair laptop motherboard no video, no...
LaptopOnCall Inc.


       
Solve Your Problem Now!
Chat Live with an Expert
Chat Now

X
Continue
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!
Insert Link
Insert Image
Insert You-Tube clip
Insert List
Insert List
Spell Check

What is this?



Select