| 订阅 | 在线投稿
分享
 
 
 

ASP程序中输出Excel文件实例一则

2008-08-25 23:34:33 编辑來源:互联网 国际版 评论
 
 
本文为【ASP程序中输出Excel文件实例一则】的汉字拼音对照版显示拼音
  zaiaspzhongliyongexceldeyigefangfashijiangexcelwenjianzuoweiyigeshujukujinxinglianjieranhoudecaozuoheduiaccessshujukucaozuoleisidanshizhegefangfabushizongnengyouyongdeyingweiexcelbushiguanxixingdeshujukuduiyuyigegudinggeshizhegegeshiliyoufuduodedanyuangehebingbiankuang线xiantiaoyangshihuanyoutuandanyuangezhijianhuanyougongshiguanxidengdengdewoxiangzuirongyilijiedejiushizaihoutaidakaiyigeyiyoushedinghaomobandewenjianranhouzaixuyaodedifangcharushujubaocunshuchu... ...

   zhelitidaodezhezhongfangfashizhijiejianliyigeexcelduixiangzaihoutaikeyigengfang便biandeduiexcelwendangjinxinggezhongcaozuo

   fuwuqiduanhuanbixuyaoshezhideyidianshiCOMzujiandecaozuoquanxianzaiminglingxingjianruDCOMCNFGzejinruCOMzujianpeizhijiemianxuanze MicrosoftExcelhoudianjishuxinganniujiangsangedanxuanxiangyilvxuanzezidingyibianjizhongjiangEveryonejiarusuoyouquanxianbaocunwanbihouchongxinqidongfuwuqi

   henchongyaobushezhidehuabiedediannaojiangwufawanchengexcelduixiangdejianli

   danshizhezhongfangfayeyouyigequehanzaiwoshijicaozuozhongruguofuwuqishangyiyoudakaideexcelwenjianzaizhixingtongyiwenjian,jiuhuichucuozanshihuangaobumingbaiweihekenenghuanyouyixiedifangmeishezhihaoba

   lingwai,shangmianyinyongdewenzhangzhongshilidaimageshibutaiwanzhengqizhonghenduohuanxinghekonggegeshibuzhunqueruguodaimawanzhengzhiyaofuzhidaimajiukeyiyunxingchenggongranhouzaimanmanjinxingyanjiuxiugaijiuhenrongyishangshoulexianbaxiugaihoudedaimaruxia(shanquleqizhonghuatubiaodebufen):

  <%

  On Error Resume Next

  strAddr=Server.MapPath(".")

  set objExcelApp=CreateObject("Excel.Application")

  objExcelApp.DisplayAlerts=false

  objExcelApp.Application.Visible=false

  objExcelApp.WorkBooks.Open(strAddr&"\Templet\Null.xls")

  set objExcelBook=objExcelApp.ActiveWorkBook

  set objExcelSheets=objExcelBook.Worksheets

  set objExcelSheet=objExcelBook.Sheets(1)

  objExcelSheet.Range("B2:k2").Value=Array("Week1","Week2","Week3","Week4","Week5","Week6","Week7")

  objExcelSheet.Range("B3:k3").Value=Array("67","87","5","9","7","45","45","54","54","10")

  objExcelSheet.Range("B4:k4").Value=Array("10","10","8","27","33","37","50","54","10","10")

  objExcelSheet.Range("B5:k5").Value=Array("23","3","86","64","60","18","5","1","36","80")

  objExcelSheet.Cells(3,1).Value="InternetExplorer"

  objExcelSheet.Cells(4,1).Value="Netscape"

  objExcelSheet.Cells(5,1).Value="Other"

  objExcelSheet.Range("b2:k5").Select

  SaveAs(strAddr&"\Temp\Excel.xls")

  objExcelApp.Quit

  set objExcelApp=Nothing

  %>

  <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">

  <HTML>

  <HEAD>

  <TITLE>NewDocument</TITLE>

  <METANAME="Generator"CONTENT=" MicrosoftFrontPage5.0">

  <METANAME="Author"CONTENT="">

  <METANAME="Keywords"CONTENT="">

  <METANAME="Description"CONTENT="">

  </HEAD>

  <BODY>

  </BODY>

  </HTML>

   caozuowanexcelwenjianxuyaojiangwenjianjinxingshuchushijinongyixiayongASPderedirectdaoexcelfangfayoushihoushizaiIEzhongzhijiedakaiyoushihoushitanchu"xiazai dakai baocun"chuangkou.ruguoxuyaozhijiezaiIEzhongdakaibufangyongFSOzairuexcelwenjianranhouzaiIEzhongshuchu

  <%

  Dim Fso,FileExt,strFilePath,Mime

  strFilePath = "f:\aspxuexi.doc"

  Set Fso=Server.CreateObject("Scripting.FileSystemObject")

  FileExt = Fso.GetExtensionName(strFilePath)

  Set fso=Nothing

  Select Case FileExt

  Case "doc"

  Mime="Application/msword"

  Case "xls"

  Mime="Application/msexcel"

  End Select

  Call OutPut(strFilePath,Mime)

  '######################################################

  Function OutPut(strFilePath,Mime)

  Response.ContentType = Mime

  Const adTypeBinary = 1

  Set objStream = Server.CreateObject("ADODB.Stream")

  objStream.Open

  objStream.Type = adTypeBinary

  objStream.LoadFromFile strFilePath

  Response.BinaryWrite objStream.Read

  objStream.Close

  Set objStream = Nothing

  End Function

  '######################################################

  %>

   zaishijiyingyongzhongdangfuwuqiduanzhengzaiyunxingexcelhuoACCESSchengxushikehuduantijiaojianliexcelapplicationduixiangwufachenggonglingyizhongqingkuangdangyigekehuduantijiaoqingqiushibingmeiyoujieshushilingyigekehuduantijiaodeqingqiuyewufachenggonghuoxuzhegewentiyoubiedejiejuefangandanshizhishaozheshibuwendingde

   zhenghaowomenbumenneibuwangshangyouxiangguanchenggongdeliziwachulaikanyuanlaishizaikehuduanjianliexcelapplicationdezheyangfuwuqiduanbuhuizaichongtuduiyukehuduanyinweishizaijuyuwangneiyunxingkehuIEanquanjibiekeyishedibingyuanxuyunxingxiangguanactiveX(jiushizaiIEgongjuxuanxiangzhongbageleishejianquandedong西xidousheweiyuanxujutidenayixiangmeiyouquzhuijiu)ji使shimeiyoushezhiIEyehuitanchujinggaochuangkoushifouyuanxuyunxingactiveX

   shixiandedaimaheshangpianrizhichabuduojiandanderuxia

  <script language="vbscript">

  set objExcelApp=CreateObject("Excel.Application")

  objExcelApp.DisplayAlerts=true

  objExcelApp.WorkBooks.Open("http://XXX.XXX.XXX/XXX.xls")

  'wanzhengwangludizhidexlswenjian,zhegewenjianyijingshezhihaogeshijidayinyangshi,baocunzaifuwuqishang

  set objExcelBook=objExcelApp.ActiveWorkBook

  set objExcelSheets=objExcelBook.Worksheets

  set objExcelSheet=objExcelBook.Sheets(1)

  '====cichuweiduiexceldanyuangejinxingtianxieshujudeyuju,ruguoshicongshujukutiqushuju,keyouhoutaichengxushengchengzhexieyuju,ASPzhongdeshilie:

  'ru:response.write "objExcelSheet.Range(""B2"").Value="""&rs("XXX")&""""

  'huo objExcelSheet.Range("B2").Value="<%=rs("XXX")%>"

  objExcelSheet.Range("B2:k2").Value=Array("Week1","Week2","Week3","Week4","Week5","Week6","Week7")

  objExcelSheet.Range("B3:k3").Value=Array("67","87","5","9","7","45","45","54","54","10")

  objExcelSheet.Range("B4:k4").Value=Array("10","10","8","27","33","37","50","54","10","10")

  objExcelSheet.Range("B5:k5").Value=Array("23","3","86","64","60","18","5","1","36","80")

  objExcelSheet.Cells(3,1).Value="InternetExplorer"

  objExcelSheet.Cells(4,1).Value="Netscape"

  objExcelSheet.Cells(5,1).Value="Other"

  '==================

  'objExcelApp.Quit

  'set objExcelApp=Nothing

  </script>

   shangshudaimazhong

   'objExcelApp.Quit

   'set objExcelApp=Nothing

   zuoweiquxiaozhixingyinweicichubuyaoguanbiexcel.applicaitionburanshujutianwanhouexcelhuiguanbidiaozheshizaikehuduandakaideexcelwenjianxuyaoyoukehujinxingxiugaihuodayindengcaozuotongshiobjexcelappduixianghuanyoudayinshezhijijinrudayinyulanjiemiandengfangfakeshenkaoxiangguanexcelxiangguanziliao原文
 
 
在asp中利用excel的一个方法是将excel文件作为一个数据库进行链接,然后的操作和对access数据库操作类似。但是这个方法不是总能有用的,应为excel不是关系型的数据库。对于一个固定格式,这个格式里有复杂的单元格合并,边框线条样式,还有图案,单元格之间还有公式关系等等的,我想最容易理解的就是在后台打开一个已有设定好模板的文件,然后在需要的地方插入数据,保存,输出... ... 这里提到的这种方法是直接建立一个excel对象,在后台可以更方便的对excel文档进行各种操作。 服务器端还必须要设置的一点是COM组件的操作权限。在命令行键入“DCOMCNFG”,则进入COM组件配置界面,选择 MicrosoftExcel后点击属性按钮,将三个单选项一律选择自定义,编辑中将Everyone加入所有权限。保存完毕后重新启动服务器。 很重要,不设置的话,别的电脑将无法完成excel对象的建立。 但是这种方法也有一个缺憾,在我实际操作中,如果服务器上已有打开的excel文件,再执行同一文件,就会出错,暂时还搞不明白为何,可能还有一些地方没设置好吧。 另外,上面引用的文章中实例代码格式不太完整,其中很多换行和空格格式不准确,如果代码完整,只要复制代码,就可以运行成功,然后再慢慢进行研究修改,就很容易上手了,现把修改后的代码如下(删去了其中画图表的部分): <% On Error Resume Next strAddr=Server.MapPath(".") set objExcelApp=CreateObject("Excel.Application") objExcelApp.DisplayAlerts=false objExcelApp.Application.Visible=false objExcelApp.WorkBooks.Open(strAddr&"\Templet\Null.xls") set objExcelBook=objExcelApp.ActiveWorkBook set objExcelSheets=objExcelBook.Worksheets set objExcelSheet=objExcelBook.Sheets(1) objExcelSheet.Range("B2:k2").Value=Array("Week1","Week2","Week3","Week4","Week5","Week6","Week7") objExcelSheet.Range("B3:k3").Value=Array("67","87","5","9","7","45","45","54","54","10") objExcelSheet.Range("B4:k4").Value=Array("10","10","8","27","33","37","50","54","10","10") objExcelSheet.Range("B5:k5").Value=Array("23","3","86","64","60","18","5","1","36","80") objExcelSheet.Cells(3,1).Value="InternetExplorer" objExcelSheet.Cells(4,1).Value="Netscape" objExcelSheet.Cells(5,1).Value="Other" objExcelSheet.Range("b2:k5").Select SaveAs(strAddr&"\Temp\Excel.xls") objExcelApp.Quit set objExcelApp=Nothing %> <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN"> <HTML> <HEAD> <TITLE>NewDocument</TITLE> <METANAME="Generator"CONTENT=" MicrosoftFrontPage5.0"> <METANAME="Author"CONTENT=""> <METANAME="Keywords"CONTENT=""> <METANAME="Description"CONTENT=""> </HEAD> <BODY> </BODY> </HTML> 操作完excel文件需要将文件进行输出,实际弄一下,用ASP的redirect到excel方法,有时候是在IE中直接打开,有时候是弹出"下载 打开 保存"窗口.如果需要直接在IE中打开,不放用FSO载入excel文件,然后在IE中输出。 <% Dim Fso,FileExt,strFilePath,Mime strFilePath = "f:\aspxuexi.doc" Set Fso=Server.CreateObject("Scripting.FileSystemObject") FileExt = Fso.GetExtensionName(strFilePath) Set fso=Nothing Select Case FileExt Case "doc" Mime="Application/msword" Case "xls" Mime="Application/msexcel" End Select Call OutPut(strFilePath,Mime) '###################################################### Function OutPut(strFilePath,Mime) Response.ContentType = Mime Const adTypeBinary = 1 Set objStream = Server.CreateObject("ADODB.Stream") objStream.Open objStream.Type = adTypeBinary objStream.LoadFromFile strFilePath Response.BinaryWrite objStream.Read objStream.Close Set objStream = Nothing End Function '###################################################### %> 在实际应用中。当服务器端正在运行excel或ACCESS程序时,客户端提交建立excel。application对象无法成功。另一种情况,当一个客户端提交请求时,并没有结束时,另一个客户端提交的请求也无法成功!或许这个问题有别的解决方案,但是至少这是不稳定的。 正好我们部门内部网上有相关成功的例子,挖出来看,原来是在客户端建立excel。application的。这样,服务器端不会再冲突。对于客户端,因为是在局域网内运行,客户IE安全级别可以设低,并允许运行相关activeX(就是在IE工具选项中把各类涉及安全的东西都设为允许,具体的哪一项没有去追究),即使没有设置,IE也会弹出警告窗口:“是否允许运行activeX?” 实现的代码和上篇日志差不多,简单的如下: <script language="vbscript"> set objExcelApp=CreateObject("Excel.Application") objExcelApp.DisplayAlerts=true objExcelApp.WorkBooks.Open("[url=http://XXX.XXX.XXX/XXX.xls]http://XXX.XXX.XXX/XXX.xls[/url]") '完整网路地址的xls文件,这个文件已经设置好格式及打印样式,保存在服务器上 set objExcelBook=objExcelApp.ActiveWorkBook set objExcelSheets=objExcelBook.Worksheets set objExcelSheet=objExcelBook.Sheets(1) '====此处为对excel单元格进行填写数据的语句,如果是从数据库提取数据,可由后台程序生成这些语句,ASP中的示列: '如:response.write "objExcelSheet.Range(""B2"").Value="""&rs("XXX")&"""" '或 objExcelSheet.Range("B2").Value="<%=rs("XXX")%>" objExcelSheet.Range("B2:k2").Value=Array("Week1","Week2","Week3","Week4","Week5","Week6","Week7") objExcelSheet.Range("B3:k3").Value=Array("67","87","5","9","7","45","45","54","54","10") objExcelSheet.Range("B4:k4").Value=Array("10","10","8","27","33","37","50","54","10","10") objExcelSheet.Range("B5:k5").Value=Array("23","3","86","64","60","18","5","1","36","80") objExcelSheet.Cells(3,1).Value="InternetExplorer" objExcelSheet.Cells(4,1).Value="Netscape" objExcelSheet.Cells(5,1).Value="Other" '================== 'objExcelApp.Quit 'set objExcelApp=Nothing </script> 上述代码中 'objExcelApp.Quit 'set objExcelApp=Nothing 作为取消执行,因为此处不要关闭excel.applicaition,不然数据填完后,excel会关闭掉。这时在客户端打开的excel文件需要由客户进行修改或打印等操作。同时objexcelapp对象还有打印设置及进入打印预览界面等方法,可参考相关excel相关资料。
󰈣󰈤
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

网络安全治理:国家安全保障的主要方向是打击犯罪,而不是处置和惩罚受害者

来源:中国青年报 新的攻击方法不断涌现,黑客几乎永远占据网络攻击的上风,我们不可能通过技术手段杜绝网络攻击。国家安全保障的主要方向是打击犯罪,而不是处置和惩罚...

 
 
 
>>返回首页<<
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 
 热帖排行
 
清纯得让人窒息(7)
清纯得让人窒息(6)
天泽海韵宣传片
赣州文庙
 
 
王朝网络微信公众号
微信扫码关注本站公众号wangchaonetcn
 
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
©2005- 王朝网络 版权所有