Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Download file ?????

Status
Not open for further replies.

vincentius

Programmer
Feb 11, 2002
17
0
0
ID
All,

Hi, after I use query in my database, I want to write it in a text file or .xls file or maybe .csv file. How to do it? And after that How to download it or attach to send it through e-mail?
A snippet code would be helpful.

Thanks,

Vincent.
 
To use Excel you must have it installed on the server. This code works for me, getting data from a database, placing it in an Excel Sheet, producing a chart and placing the chart in an ASP page. It is a lot of work but produces a superior chart on your web pages.

Set objExcel = CreateObject("Excel.Sheet")
Set cn = Server.CreateObject("ADODB.Connection")
cn.open "Name","Id", "Pw"
Retrieve = Request.QueryString("Database")

SqlString = "SELECT TO_NUMBER(TO_CHAR(TO_DATE....

Set RS = cn.Execute(SqlString)


MyCnt = 1
Do While not rs.eof

i = myCnt
objExcel.ActiveSheet.Cells(i + 1, 1 ) = "Wk No" & rs("WEEK_NO")
objExcel.ActiveSheet.Cells(i + 1, 2 ) = (int(rs("BUFF_CACHE")*10))/10
myCnt = myCnt + 1
rs.movenext
loop
objExcel.ActiveSheet.Range("A1:A1").Value = "Week Number"
objExcel.ActiveSheet.Range("b1:b1").Value = "Buffer Cache"

startstr = "a1:"
endstr = "B" & mycnt

objExcel.ActiveSheet.Range(startstr & endstr).Select
Set XlChartObj = objExcel.Charts.Add
XlChartObj.ChartType = 51
XlChartObj.HasTitle = True
XlChartObj.ChartTitle.Text = Retrieve & " Average Buffer Cache by Week"
XlChartObj.ApplyDataLabels
XlChartObj.Axes(1).TickLabels.Orientation = 90
XlChartObj.Axes(1).TickLabels.font.size = 8
XlChartObj.SetSourceData _
objExcel.Sheets("Sheet1").Range("A1:" & endstr),2
filename = server.mappath("/images")
if mid(filename,len(filename),1) <> &quot;\&quot; then
filename = filename & &quot;\&quot;
end if
filename = server.mappath(&quot;/images&quot;)
if mid(filename,len(filename),1) <> &quot;\&quot; then
filename = filename & &quot;\&quot;
end if
filename = filename & Retrieve & &quot;BuffCache.gif&quot;
objExcel.activechart.export filename, &quot;GIF&quot;

Set objExcel = Nothing
set rs = nothing

The chart is saved as a gif file which is included in a seperate page.

Hope this helps

Good Luck
 
B827,

Hi, Thanks for your help. But I still confuse with the command :

- objExcel.ActiveSheet.Cells(i + 1, 1 ) = &quot;Wk No&quot; & rs(&quot;WEEK_NO&quot;)
What is 'i + 1' means? does it indicate a column?
What about 1?

- objExcel.ActiveSheet.Range(&quot;A1:A1&quot;).Value = &quot;Week Number&quot;
If for this one &quot;A1:A1&quot; means column A row 1 in Excel, doesn't it?

And in that snippet code I didn't see any command to save the file in .xls format. Do you know how to save it?

Thanks,

Vincent.
 
Hi Vincent
the code is producing a graph of weekly stats, the data in the database just gives a week a number, 1,2,3..... For the axix of the chart I wanted Wk1,Wk2,Wk3.... which is what the concatenation is doing.
in this case it is putting the week number in one column and the value in the next so that the spread sheet looks like

COL A COL B
Wk1 27
Wk2 53
Wk3 44

for however many weeks there is data for.

I'll look into the saving problem. I just use an existing spreadsheet which I don't save. I'll post what I find later.
 
Vincent
this piece of code will create a spreadsheet aenter some data and save the result into the location selected in the code.

dim objExcel,I
Set objExcel = CreateObject(&quot;Excel.application&quot;)
objExcel.Workbooks.Add
For I = 1 to 12
objExcel.ActiveSheet.Cells(i + 1, 1 ) = &quot;Wk No&quot; & I
objExcel.ActiveSheet.Cells(i + 1, 2 ) = I^1.2
Next
objExcel.ActiveWorkbook.SaveAs &quot;D:\Inetpub\Iissamples\Book2.xls&quot;
Set objExcel = Nothing


Sandy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top