xlswrite_demo.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>xlswrite demo</title>
  9. <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
  10. </head>
  11. <body>
  12. <script src="./static/layui/layui.all.js"></script>
  13. <button type="button" class="layui-btn" id="test1">
  14. <i class="layui-icon">&#xe67c;</i>导入excel
  15. </button>
  16. <button type="button" class="layui-btn" id="test2" onclick="exporData()">
  17. <i class="layui-icon">&#xe601;</i>导出excel
  18. </button>
  19. </body>
  20. <script>
  21. var layer = layui.layer, $ = layui.jquery, upload = layui.upload;
  22. var loading, fileLoading;
  23. var wsServer = 'ws://192.168.18.192:9502';
  24. var websocket = new WebSocket(wsServer);
  25. var fd = 0;//客户端socket id
  26. //文件上传 & 导入数据
  27. var uploadInst = upload.render({
  28. elem: '#test1' //绑定元素
  29. , accept: 'file'
  30. , method: 'post'
  31. , url: '/upload.php' //上传接口
  32. , before: function (obj) { //obj参数包含的信息,跟 choose回调完全一致,可参见上文。
  33. fileLoading = layer.load(); //上传loading
  34. }
  35. , done: function (res) {
  36. //上传完毕回调
  37. if (res.code == 1) {
  38. console.log(res.file);
  39. layer.close(fileLoading);
  40. layer.msg('文件上传完成,开始处理数据');
  41. importData(res.file);
  42. }
  43. }
  44. , error: function (e) {
  45. //请求异常回调
  46. console.log(res);
  47. }
  48. });
  49. //导出excel
  50. function exporData() {
  51. loading = layer.load(2, {
  52. shade: [0.1, '#fff'],
  53. content: '正在到导出,请耐心等待...',
  54. id: 'process',
  55. success: function (layero) {
  56. layero.find('.layui-layer-content').css({
  57. 'padding-top': '40px',//图标与样式会重合,这样设置可以错开
  58. 'width': '200px',//文字显示的宽度
  59. 'text-indent': '-4rem',
  60. });
  61. }
  62. });
  63. //请求后台excel文件生成
  64. $.get('/export_demo.php', {fd: fd}, function (res) {
  65. if (res.code == 1) {
  66. layer.close(loading);
  67. layer.msg(res.msg, {time: '1000'}, function () {
  68. //请求后台获取下载地址
  69. window.location.href = res.url;
  70. });
  71. }
  72. }, 'json');
  73. }
  74. //导入excel
  75. function importData(filename) {
  76. // 开启 loading 效果
  77. loading = layer.load(2, {
  78. shade: [0.1, '#fff'],
  79. id: 'process',
  80. content: '正在处理数据,请耐心等待...',
  81. success: function (layero) {
  82. layero.find('.layui-layer-content').css({
  83. 'padding-top': '40px',//图标与样式会重合,这样设置可以错开
  84. 'width': '200px',//文字显示的宽度
  85. 'text-indent': '-4rem',
  86. });
  87. }
  88. });
  89. $.get('/import_demo.php', {file: filename, fd: fd}, function (res) {
  90. console.log(res);
  91. if (res.code == 1) {
  92. layer.msg(res.msg, {}, function () {
  93. layer.closeAll();
  94. });
  95. }
  96. }, 'json');
  97. }
  98. websocket.onopen = function (evt) {
  99. console.log("Connected to WebSocket server.");
  100. };
  101. websocket.onclose = function (evt) {
  102. console.log("Disconnected");
  103. };
  104. websocket.onmessage = function (evt) {
  105. var data = JSON.parse(evt.data);
  106. if (data.status == 'onopen') {
  107. fd = data.fd;
  108. } else {
  109. $('#process').text('已处理' + data.process + '条数据...');
  110. // console.log(data.process);
  111. }
  112. // console.log('Retrieved data from server: ' + data);
  113. };
  114. websocket.onerror = function (evt, e) {
  115. console.log('Error occured: ' + evt.data);
  116. };
  117. </script>
  118. </html>