exportExec.jsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import React ,{Component} from 'react';
  2. import {Button} from 'antd';
  3. class ExportExec extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = { }
  7. }
  8. //判断浏览器类型
  9. getExplorer() {
  10. var explorer = window.navigator.userAgent;
  11. //ie
  12. if(explorer.indexOf("MSIE") >= 0) {
  13. return 'ie';
  14. }
  15. //firefox
  16. else if(explorer.indexOf("Firefox") >= 0) {
  17. return 'Firefox';
  18. }
  19. //Chrome
  20. else if(explorer.indexOf("Chrome") >= 0) {
  21. return 'Chrome';
  22. }
  23. //Opera
  24. else if(explorer.indexOf("Opera") >= 0) {
  25. return 'Opera';
  26. }
  27. //Safari
  28. else if(explorer.indexOf("Safari") >= 0) {
  29. return 'Safari';
  30. }
  31. }
  32. //导出为exel表格
  33. tabExel() {
  34. this.props.callback(true);
  35. }
  36. tabaleClick(tabel){
  37. var table = tabel;
  38. var idTmr;
  39. //debugger;
  40. //整个表格拷贝到EXCEL中
  41. if(this.getExplorer() == 'ie') {
  42. var curTbl = document.getElementById(tableid);
  43. var oXL = new ActiveXObject("Excel.Application");
  44. //创建AX对象excel
  45. var oWB = oXL.Workbooks.Add();
  46. //获取workbook对象
  47. var xlsheet = oWB.Worksheets(1);
  48. //激活当前sheet
  49. var sel = document.body.createTextRange();
  50. sel.moveToElementText(curTbl);
  51. //把表格中的内容移到TextRange中
  52. sel.select();
  53. //全选TextRange中内容
  54. sel.execCommand("Copy");
  55. //复制TextRange中内容
  56. xlsheet.Paste();
  57. //粘贴到活动的EXCEL中
  58. oXL.Visible = true;
  59. //设置excel可见属性
  60. try {
  61. var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
  62. } catch(e) {
  63. print("Nested catch caught " + e);
  64. } finally {
  65. oWB.SaveAs(fname);
  66. oWB.Close(savechanges = false);
  67. //xls.visible = false;
  68. oXL.Quit();
  69. oXL = null;
  70. //结束excel进程,退出完成
  71. //window.setInterval("Cleanup();",1);
  72. idTmr = window.setInterval("Cleanup();", 1);
  73. }
  74. } else {
  75. this.tableToExcel(table,this.getExplorer());
  76. }
  77. function Cleanup() {
  78. window.clearInterval(idTmr);
  79. CollectGarbage();
  80. }
  81. this.props.callback(false);
  82. }
  83. tableToExcel(table,name) {
  84. var uri = 'data:application/vnd.ms-excel;base64,',
  85. template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
  86. base64 = function(s) {
  87. return window.btoa(unescape(encodeURIComponent(s)))
  88. },
  89. format = function(s, c) {
  90. return s.replace(/{(\w+)}/g,
  91. function(m, p) {
  92. return c[p];
  93. })
  94. }
  95. if(!table.nodeType) table = document.getElementById(table);
  96. var ctx = {
  97. worksheet: name || 'Worksheet',
  98. table: table.innerHTML
  99. };
  100. window.location.href = uri + base64(format(template, ctx))
  101. }
  102. componentWillReceiveProps(nextProps){
  103. if(nextProps.data){
  104. this.tabaleClick(nextProps.data)
  105. }
  106. }
  107. render() {
  108. return (
  109. <Button onClick={this.tabExel.bind(this)}>导出到表格</Button>
  110. );
  111. }
  112. }
  113. export default ExportExec;