JAVA导出Excel到浏览器_多个浏览器下应用前端JS实现一键导出excel表

本文介绍了一种从网页中导出金庸武侠小说主要人物到Excel文件的方法。该方法针对不同的浏览器使用了不同的实现方式,例如对于IE浏览器利用了ActiveX对象来实现复制表格内容并粘贴到Excel中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1

2

3

4

5

导出表格

6

7 varTmr;8 functiongetExplorer() {9 varexplorer=window.navigator.userAgent ;10 //ie

11 if(explorer.indexOf("MSIE")>= 0) {12 return 'ie';13 }14 //firefox

15 else if(explorer.indexOf("Firefox")>= 0) {16 return 'Firefox';17 }18 //Chrome

19 else if(explorer.indexOf("Chrome")>= 0){20 return 'Chrome';21 }22 //Opera

23 else if(explorer.indexOf("Opera")>= 0){24 return 'Opera';25 }26 //Safari

27 else if(explorer.indexOf("Safari")>= 0){28 return 'Safari';29 }30 }31 functionmethod(tableid) {32 if(getExplorer()=='ie')33 {34 varcurTbl=document.getElementById(tableid);35 varoXL= newActiveXObject("Excel.Application");36 varoWB=oXL.Workbooks.Add();37 varxlsheet=oWB.Worksheets(1);38 varsel=document.body.createTextRange();39 sel.moveToElementText(curTbl);40 sel.select();41 sel.execCommand("Copy");42 xlsheet.Paste();43 oXL.Visible= true;44

45 try{46 varfname=oXL.Application.GetSaveAsFilename("Excel.xls","Excel Spreadsheets (*.xls), *.xls");47 }catch(e) {48 print("Nested catch caught" +e);49 }finally{50 oWB.SaveAs(fname);51 oWB.Close(savechanges= false);52 oXL.Quit();53 oXL= null;54 Tmr=window.setInterval("Cleanup();",1);55 }56 }57 else

58 {59 tableToExcel(tableid)60 }61 }62 functionCleanup() {63 window.clearInterval(Tmr);64 CollectGarbage();65 }66 vartableToExcel=(function() {67 varuri= 'data:application/vnd.ms-excel;base64,',68 template= '

',69 base64= function(s) {returnwindow.btoa(unescape(encodeURIComponent(s))) },70 format= function(s, c) {71 returns.replace(/{(w+)}/g,72 function(m, p) {returnc[p]; }) }73 return function(table, name) {74 if(!table.nodeType) table=document.getElementById(table)75 varctx={worksheet: name|| 'Worksheet', table: table.innerHTML}76 window.location.href=uri+base64(format(template, ctx))77 }78 })()79

80

81

82

83

84 导出Excel

85

86

87

88

89

90

91

92 金庸笔下人物

93

94

95

96

倚天屠龙记

97

天龙八部

98

鹿鼎记

99

神雕侠侣

100

射雕英雄传

101

102

103

104

张无忌

105

乔峰

106

韦小宝

107

杨过

108

郭靖

109

110

111

112

赵敏

113

虚竹

114

康熙

115

小龙女

116

黄蓉

117

118

119

120

周芷若

121

段誉

122

鳌拜

123

李莫愁

124

杨康

125

126

127

128

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值