| 首页 | 下载中心 | 图片中心 | 
您现在的位置: 海华网 >> 编程日志 >> ASP编程 >> 文章正文 用户登录 新用户注册
asp中的数据库导入Excel中的问题         
asp中的数据库导入Excel中的问题
作者:佚名  文章来源:本站原创  点击数:  更新时间:2005-8-30
    我现在已用如下代码将检索出来的的数据导入到Excel中,但由于从数据库中导出的数据字段比较宽,所以Excel中单元格显示不开,(Excel可能用的是它自身默认的单元格宽度),我怎么能控制每一列的宽度呢?(最好不用模板,当然如果一定要用那么请提供一段代码,要加分可以商量)

<%option explicit
response.expires=0
response.buffer=true%>
<!--#include file="INC/conn.inc"-->

<html>
<head>
<title>从数据库生成电子表格文档</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body bgcolor="#FFFFFF" text="#000000">

 
<%
'注意:本生成系统使用office自带的owc组件,服务器上必须安装office
'选出记录集
dim rs,sql
set rs=server.CreateObject("adodb.recordset")
sql="select studentname,lxtime,lxcountry,lxschoolname,lxschooladd,lxspecialty,lxyear,lxwww,lxeml,lxtel,lxother from studentdata"
rs.open sql,conn,1,3

dim rstCount,rstField
rstCount=rs.RecordCount'记录行数
rstField=rs.fields.count'记录列数


dim oExcel,sCellValue,Path
Set oExcel = Server.CreateObject("OWC.Spreadsheet")

dim Line,Column
Line=1

'写入列名
For Column=0 to rstField-1
    sCellValue=rs.fields(Column).name'列名
    select case  sCellValue
case "studentname"
sCellValue="姓名"
case "lxtime"
sCellValue="留学时间"
case "lxcountry"
sCellValue="留学国家"
case "lxschoolname"
sCellValue="学校名称"
case "lxschooladd"
sCellValue="学校地址"
case "lxspecialty"
sCellValue="留学专业"
case "lxyear"
sCellValue="留学年限"
case "lxwww"
sCellValue="学校网址"
case "lxeml"
sCellValue="学校电子信箱"
case "lxtel"
sCellValue="学校联系电话"
case "lxother"
sCellValue="其它"
end select
    sCellValue=sCellValue&""
    oExcel.Cells(Line,Column+1)= sCellValue '第一行的第几列,列是循环的,行不变。
Next

'写入记录,从第二行开始先行后列的写入表中
For Line=2 to rstcount+1
    For Column=0 to rstField-1
        sCellValue=rs.fields(Column)
        sCellValue=sCellValue&""
        oExcel.Cells(Line,Column+1)= sCellValue
    Next
rs.movenext
Next
'这行不知道干什么
For Column = 1 to rstField
    oExcel.Columns(Column).AutoFitColumns
Next

'设置Excel表里的字体
Column = 1
Do While Column <= rstField
    oExcel.Cells(1, Column).Font.Bold = True
    oExcel.Cells(1, Column).Font.Italic = False
    oExcel.Cells(1, Column).Font.Size = 16
    'oExcel.Cells(1, Column).Font.color = 'red'
    Column = Column + 1
Loop

'输出该表
Path=Server.MapPath("Caseinfo.xlt")
oExcel.ActiveSheet.Export Path, 0
%>

<br/>
<a href="Caseinfo.xlt" target=_blank>查看生成的电子表格</a>
<br/>

</body>
</html>
'设置Excel表里的字体
Column = 1 
Do While Column <= rstField 
    oExcel.Cells(1, Column).Font.Bold = True 
    oExcel.Cells(1, Column).Font.Italic = False
    oExcel.Cells(1, Column).Font.Size = 16 
    '设置字段的宽度
    oExcel.Cells(1,Column).ColumnWidth=200
    ~~~~~~~~~~~~~~~~~~~~~     
'oExcel.Cells(1, Column).Font.color = 'red'
    Column = Column + 1 
Loop
文章录入:Haihua    责任编辑:Haihua 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    专 题 栏 目
    最 新 热 门
    最 新 推 荐
    相 关 文 章
    没有相关文章
        网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)