owc是office web compent的缩写,即microsoft的office web组件,他为在web中绘制图像提供了灵活的同时也是最基本的机制。在一个intranet环境中,如果能假设客户机上存在特定的浏览器和一些功能强大的软件(如ie5和office 2000),那么就有能力利用office web组件提供一个交互式图像研发环境。这种模式下,客户端工作站将在整个任务中分担非常大的比重。
<%option explicit
class excelgen
private objspreadsheet
private icoloffset
private irowoffset
sub class_initialize()
set objspreadsheet = server.createobject("owc.spreadsheet")
irowoffset = 2
icoloffset = 2
end sub
sub class_terminate()
set objspreadsheet = nothing clean up
end sub
public property let columnoffset(icoloff)
if icoloff > 0 then
icoloffset = icoloff
else
icoloffset = 2
end if
end property
public property let rowoffset(irowoff)
if irowoff > 0 then
irowoffset = irowoff
else
irowoffset = 2
end if
end property sub generateworksheet(objrs)
populates the excel worksheet based on a recordsets contents
start by displaying the titles
if objrs.eof then exit sub
dim objfield, icol, irow
icol = icoloffset
irow = irowoffset
for each objfield in objrs.fields
objspreadsheet.cells(irow, icol).value = objfield.name
objspreadsheet.columns(icol).autofitcolumns
设置excel表里的字体
objspreadsheet.cells(irow, icol).font.bold = true
objspreadsheet.cells(irow, icol).font.italic = false
objspreadsheet.cells(irow, icol).font.size = 10
objspreadsheet.cells(irow, icol).halignment = 2 居中
icol = icol + 1
next objfield
display all of the data
do while not objrs.eof
irow = irow + 1
icol = icoloffset
for each objfield in objrs.fields
if isnull(objfield.value) then
objspreadsheet.cells(irow, icol).value = ""
else
objspreadsheet.cells(irow, icol).value = objfield.value
objspreadsheet.columns(icol).autofitcolumns
objspreadsheet.cells(irow, icol).font.bold = false
objspreadsheet.cells(irow, icol).font.italic = false
objspreadsheet.cells(irow, icol).font.size = 10
end if
icol = icol + 1
next objfield
objrs.movenext
loop
end sub function saveworksheet(strfilename)
save the worksheet to a specified filename
on error resume next
call objspreadsheet.activesheet.export(strfilename, 0)
saveworksheet = (err.number = 0)
end function
end class
dim objrs
set objrs = server.createobject("adodb.recordset")
objrs.open "select * from xxxx", "provider=sqloledb.1;persist security
info=true;user id=xxxx;password=xxxx;initial catalog=xxxx;data source=xxxx;"
dim savename
savename = request.cookies("savename")("name")
dim objexcel
dim excelpath
excelpath = "excel\" & savename & ".xls"
set objexcel = new excelgen
objexcel.rowoffset = 1
objexcel.columnoffset = 1
objexcel.generateworksheet(objrs)
if objexcel.saveworksheet(server.mappath(excelpath)) then
response.write "<html><body bgcolor=gainsboro text=#000000>已保存为excel文件.
<a href=" & server.urlencode(excelpath) & ">下载</a>"
else
response.write "在保存过程中有错误!"
end if
set objexcel = nothing
objrs.close
set objrs = nothing
%>
<script language="javascript">
<!--
function automateexcel()
{
// start excel and get application object.
var oxl = new activexobject("excel.application");
// get a new workbook.
var owb = oxl.workbooks.add();
var osheet = owb.activesheet;
var table = document.all.data;
var hang = table.rows.length;
var lie = table.rows(0).cells.length;
// add table headers going cell by cell.
for (i=0;i<hang;i++)
{
for (j=0;j<lie;j++)
{
osheet.cells(i+1,j+1).value = table.rows(i).cells(j).innertext;
}
dim thearray(20,10000)
for i=0 to row-1
for j=0 to column-1
thearray(j+1,i+1) = table.rows(i).cells(j).innertext
next
next
objworddoc.application.activedocument.paragraphs.add.range.insertbefore("综合查询结果集") //显示表格标题
objworddoc.application.activedocument.paragraphs.add.range.insertbefore("")
set rngpara = objworddoc.application.activedocument.paragraphs(1).range
with rngpara
.bold = true //将标题设为粗体
.paragraphformat.alignment = 1 //将标题居中
.font.name = "隶书" //设定标题字体
.font.size = 18 //设定标题字体大小
end with
set rngcurrent = objworddoc.application.activedocument.paragraphs(3).range
set tabcurrent = objworddoc.application.activedocument.tables.add(rngcurrent,row,column)
for i = 1 to column
objworddoc.application.activedocument.tables(1).rows(1).cells(i).range.insertafter thearray(i,1)
objworddoc.application.activedocument.tables(1).rows(1).cells(i).range.paragraphformat.alignment=1
next
for i =1 to column
for j = 2 to row
objworddoc.application.activedocument.tables(1).rows(j).cells(i).range.insertafter thearray(i,j)
objworddoc.application.activedocument.tables(1).rows(j).cells(i).range.paragraphformat.alignment=1
next
next