exportdata.html.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php js::import($this->app->getWebRoot() . 'js/sheetjs/xlsx.full.min.js');?>
  2. <?php js::import($this->app->getWebRoot() . 'js/filesaver/filesaver.js');?>
  3. <?php $this->app->loadLang('file');?>
  4. <?php js::set('untitled', $lang->file->untitled);?>
  5. <div class="modal fade" id='export'>
  6. <div class="modal-dialog" style='width: 500px'>
  7. <div class="modal-content">
  8. <div class="modal-header">
  9. <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">× </span></button>
  10. <h2 class="modal-title" style='font-weight: bold;'><?php echo $lang->export;?></h2>
  11. </div>
  12. <div class="modal-body">
  13. <div style="margin: 20px 50px 20px 30px;">
  14. <table class="table table-form" style="padding:30px">
  15. <tbody>
  16. <tr>
  17. <th class='w-120px'><?php echo $lang->setFileName;?></th>
  18. <td><?php echo html::input('fileName', '', "class='form-control' autofocus placeholder='{$lang->file->untitled}'");?></td>
  19. </tr>
  20. <tr>
  21. <th><?php echo $lang->pivot->exportType;?></th>
  22. <td><?php echo html::select('fileType', $config->pivot->fileType, '', 'class="form-control" style="width: 140px"');?></td>
  23. </tr>
  24. <?php if(isset($exportMode) and $exportMode == 'preview'):?>
  25. <tr>
  26. <th><?php echo $lang->pivot->exportRange;?></th>
  27. <td><?php echo html::select('range', $lang->pivot->rangeList, '', 'class="form-control" style="width: 140px"');?></td>
  28. </tr>
  29. <?php endif;?>
  30. <tr>
  31. <th></th>
  32. <td style='padding-left: 30px;'><button class='btn btn-primary' onclick='exportData()'><?php echo $lang->save;?></button></td>
  33. </tr>
  34. </tbody>
  35. </table>
  36. </div>
  37. </div>
  38. </div>
  39. </div>
  40. </div>
  41. <script>
  42. $(function()
  43. {
  44. /* Page is not initialized before clicking export will have bug. */
  45. $('.btn-export').removeClass('hidden');
  46. })
  47. /**
  48. * Export file.
  49. *
  50. * @param object $domObj
  51. * @access public
  52. * @return void
  53. */
  54. function exportFile($domObj)
  55. {
  56. if(typeof $domObj == 'undefined') return;
  57. var fileName = $('#fileName').val().trim() ? $('#fileName').val().trim() : untitled;
  58. var fileType = $('#fileType').val();
  59. var tableName = fileName + '.' + fileType;
  60. if(fileType == 'xlsx' || fileType == 'xls')
  61. {
  62. const new_sheet = XLSX.utils.table_to_book($domObj, {raw: true});
  63. XLSX.writeFile(new_sheet, tableName);
  64. }
  65. else if(fileType == 'html' || fileType == 'mht')
  66. {
  67. const htmlContent = $domObj.outerHTML;
  68. const $temp = $('<div>').html(htmlContent);
  69. $temp.find('*').removeAttr('style');
  70. $temp.find('*').removeAttr('class');
  71. $temp.find('*').removeAttr('data-flex');
  72. $temp.find('*').removeAttr('data-width');
  73. $temp.find('*').removeAttr('data-type');
  74. $temp.find('*').removeAttr('data-fixed-left-width');
  75. const cleanTableHTML = $temp.html();
  76. var head = '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
  77. var style = '<style>table, th, td {font-size: 12px; border: 1px solid gray; border-collapse: collapse;}table th, table td {padding: 5px;}</style>';
  78. var title = '<title>' + fileName + '</title></head>';
  79. var body = '<body>' + cleanTableHTML + '</body>';
  80. const finalHTML = head + style + title + body;
  81. if(fileType == 'html')
  82. {
  83. const blob = new Blob([finalHTML], { type: 'text/html;charset=utf-8' });
  84. saveAs(blob, tableName);
  85. }
  86. else if(fileType == 'mht')
  87. {
  88. const data = {html: finalHTML, fileName: fileName};
  89. $.post(createLink('file', 'ajaxExport2mht'), data, function(resp)
  90. {
  91. const blob = new Blob([resp], { type: "application/x-mimearchive" });
  92. saveAs(blob, tableName);
  93. });
  94. }
  95. }
  96. $('#export').modal('hide');
  97. }
  98. </script>