一直用动易平台的ASP做新闻发布网站,直到现在才接触导出数据到Excel的问题,目的在于公司要统计各部门的投稿量,要做这么个东西,实现起来是挺简单的,但是第一次做,还是费了一些功夫的,特此记录一下
主要代码如下:
写查询字符串
rsAll为查询字符串
rsAll.open sqlAll,conn,1,3
Set xlApplication =server.CreateObject( "Excel.Application") '调用excel对象
xlApplication.Visible = false '无需打开excel
Set xlWorkBook=xlApplication.Workbooks.Add '添加工作簿
j=1
//定义一些表格样式
xlWorkBook.Worksheets(1).Columns(1).ColumnWidth=30
xlWorkBook.Worksheets(1).Columns(1).HorizontalAlignment=3
xlWorkBook.Worksheets(1).Columns(2).ColumnWidth=30
xlWorkBook.Worksheets(1).Columns(2).HorizontalAlignment=3
xlWorkBook.Worksheets(1).Columns(3).ColumnWidth=30
xlWorkBook.Worksheets(1).Columns(3).HorizontalAlignment=3
xlWorkBook.Worksheets(1).Range( "A2 ").font.bold=true '粗体
xlWorkBook.Worksheets(1).Range( "B2 ").font.bold=true '粗体
xlWorkBook.Worksheets(1).Range( "C2 ").font.bold=true '粗体
xlWorkBook.Worksheets(1).Range( "A1 ").HorizontalAlignment=3 '水平对齐
xlWorkBook.Worksheets(1).Range( "A1 ").VerticalAlignment=3 '垂直对齐
//写excel表头了
xlWorkBook.Worksheets(1).Cells(1,1).Value = "集团新闻中心各记者站积分统计"
xlWorkBook.Worksheets(1).Cells(2,1).Value = "记者站名称"
xlWorkBook.Worksheets(1).Cells(2,2).Value = "要闻文章数量"
xlWorkBook.Worksheets(1).Cells(2,3).Value = "要闻文章积分"
xlWorkBook.Worksheets(1).Cells(2,4).Value = "综合新闻文章数量"
xlWorkBook.Worksheets(1).Cells(2,5).Value = "综合新闻文章积分"
xlWorkBook.Worksheets(1).Cells(2,6).Value = "其它栏目文章数量"
xlWorkBook.Worksheets(1).Cells(2,7).Value = "其它栏目文章数量"
xlWorkBook.Worksheets(1).Cells(2,8).Value = "微信公众号数量"
xlWorkBook.Worksheets(1).Cells(2,9).Value = "微信公众号积分"
xlWorkBook.Worksheets(1).Cells(2,10).Value = "报刊杂志数量"
xlWorkBook.Worksheets(1).Cells(2,11).Value = "报刊杂志积分"
xlWorkBook.Worksheets(1).Cells(2,12).Value = "网络电视台数量"
xlWorkBook.Worksheets(1).Cells(2,13).Value = "网络电视台积分"
xlWorkBook.Worksheets(1).Cells(2,14).Value = "比赛获奖数量"
xlWorkBook.Worksheets(1).Cells(2,15).Value = "比赛获奖积分"
//循环输出下面的数据
DO WHILE NOT rsAll.EOF
xlWorkBook.Worksheets(1).Cells(2+j,1).Value = rsAll("CopyFrom")
xlWorkBook.Worksheets(1).Cells(2+j,2).Value = rsAll("YWNo")
xlWorkBook.Worksheets(1).Cells(2+j,3).Value = rsAll("YWFS")
xlWorkBook.Worksheets(1).Cells(2+j,4).Value = rsAll("ZHNo")
xlWorkBook.Worksheets(1).Cells(2+j,5).Value = rsAll("ZHFS")
xlWorkBook.Worksheets(1).Cells(2+j,6).Value = rsAll("QTNo")
xlWorkBook.Worksheets(1).Cells(2+j,7).Value = rsAll("QTFS")
xlWorkBook.Worksheets(1).Cells(2+j,8).Value = rsAll("weixinno")
xlWorkBook.Worksheets(1).Cells(2+j,9).Value = rsAll("weixinfs")
xlWorkBook.Worksheets(1).Cells(2+j,10).Value = rsAll("baozishu")
xlWorkBook.Worksheets(1).Cells(2+j,11).Value = rsAll("baozifenshu")
xlWorkBook.Worksheets(1).Cells(2+j,12).Value = rsAll("shipinshu")
xlWorkBook.Worksheets(1).Cells(2+j,13).Value = rsAll("shipinfs")
xlWorkBook.Worksheets(1).Cells(2+j,14).Value = rsAll("zuopinshu")
xlWorkBook.Worksheets(1).Cells(2+j,15).Value = rsAll("zuopinfenshu")
j=j+1
rsALL.movenext
LOOP
xlWorkBook.SaveAs tfile //保存文件
Set xlWorksheet = Nothing //释放工作表
xlApplication.Quit //释放对象
//生成链接,供用户下载
response.write("<center><table width='700' align='center'><tr><td>导出成功!点击下载:<a href='jftj.xlsx'>jftj.xlsx</a></td></tr></table></center>")
response.Write("<script><alert>Excel导出成功,请到列表下方点击下载!</alert></script>")
END IF
程序写完了,那么要从服务器上运行,还需要给服务器安装word组件,安装word的过程不再赘述,安装上word还要给IIS赋予使用word组件中excel组件的使用权限,方法是win+R,运行框里输入dcomcnfg,然后选择“组件服务”--》计算机--》我的电脑--》DCOM配置,找到Microsoft Excel Application,然后右键--》属性--》安全--》启动和激活权限--》自定义,然后选择编辑,然后添加Everyone,然后给everyone赋予权限,然后选择“标识”选项卡,改成交互式用户。
上述安装配置excel组件的原因是,1、如果不配置“启动和激活”权限,在server.CreateObject( "Excel.Application")时,服务器会报错,没有权限。2、如果不配置标识这个玩意儿,在执行“Set xlWorkBook=xlApplication.Workbooks.Add '添加工作簿 ”时,会报内存不足请关闭相关进行之类的错误。
本次记录完毕。