dlrandy / note-issues

2 stars 0 forks source link

table to excel #116

Open dlrandy opened 5 years ago

dlrandy commented 5 years ago
export const getRandomString = () => {
  return Math.random().toString(36).substring(2, 15);
}

function base64(s) {
  return window.btoa(unescape(encodeURIComponent(s)));
}

function format(s, c) {
  return s.replace(/{(\w+)}/g, (m, p) => c[p]);
}

export const tableToExcel = (table, filename = 'excel_data') => {
  let downloadLink;
  table = table.outerHTML;
  const uri = 'data:application/vnd.ms-excel;base64,';
  const template =
    '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-mic' +
    'rosoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta cha' +
    'rset="UTF-8"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:Exce' +
    'lWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/>' +
    '</x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></' +
    'xml><![endif]--></head><body>{table}</body></html>';

  const context = {
    worksheet: filename || 'Worksheet',
    table,
  };

  filename = filename + '.xls';

  if (navigator.msSaveOrOpenBlob) {
    const fileData = [
      `${'<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-mic' + 'rosoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta cha' + 'rset="UTF-8"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:Exce' + 'lWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/>' + '</x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></' + 'xml><![endif]--></head><body>'}${table}</body></html>`,
    ];
    const blobObject = new Blob(fileData);
    navigator.msSaveOrOpenBlob(blobObject, filename);
    return true;
  } else {
    downloadLink = document.createElement("a");
    document.body.appendChild(downloadLink);
    downloadLink.href = uri + base64(format(template, context));
    downloadLink.download = filename;
    downloadLink.click();
    document.body.removeChild(downloadLink);
    return true;
  }

}
dlrandy commented 5 years ago

export function onDownload(data, filename, mime) {
  var blob = new Blob([data], {type: mime || 'application/octet-stream'});
  if (typeof window.navigator.msSaveBlob !== 'undefined') {
      window.navigator.msSaveBlob(blob, filename);
  }
  else {
      var blobURL = window.URL.createObjectURL(blob);
      var tempLink = document.createElement('a');
      tempLink.style.display = 'none';
      tempLink.href = blobURL;
      tempLink.setAttribute('download', filename); 
      if (typeof tempLink.download === 'undefined') {
          tempLink.setAttribute('target', '_blank');
      }

      document.body.appendChild(tempLink);
      tempLink.click();
      document.body.removeChild(tempLink);
      window.URL.revokeObjectURL(blobURL);
  }
}

export function onDownloadIframe(url) {
  const iframe = document.createElement('iframe');
  iframe.setAttribute('id', 'download_file_iframe');
  const form = document.createElement('form');
  form.setAttribute('method', 'get');
  form.setAttribute('action', url);//can not carry the queru, or  you can use restfule or post method
  // form.setAttribute('target', '_blank');
  iframe.appendChild(form);
  document.body.appendChild(iframe);
  form.submit();
  document.body.removeChild(iframe);
}
dlrandy commented 5 years ago

reportInst.defaults.headers['Accept'] ='application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, /'; return reportInst.get('/users.xlsx', {//'/file/index.xlsx' responseType: 'blob', ...options }); }

dlrandy commented 5 years ago

https://gist.github.com/javilobo8/097c30a233786be52070986d8cdb1743

dlrandy commented 5 years ago
export function onDownloadIframe(id) {
  const url = '/cet3etwst/report/'+ id +'/users.xlsx';
  const iframe = document.createElement('iframe');
  iframe.setAttribute('id', 'download_file_iframe');
  const form = document.createElement('form');
  form.setAttribute('method', 'get');
  form.setAttribute('action', url);
  // form.setAttribute('target', '_blank');
  iframe.appendChild(form);
  document.body.appendChild(iframe);
  form.submit();
  document.body.removeChild(iframe);
}
dlrandy commented 4 years ago

function deleteCol(index, table) { var len = table.rows.length; for (var i = 0; i < len; i++) { table.rows[i].deleteCell(index); } } export const exportTableToExcel = (function() { const uri = "data:application/vnd.ms-excel;base64,", template = '

{table}
', base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))); }, format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }); }, downloadLink = document.createElement("a"); document.body.appendChild(downloadLink); return function(table, name, removeColumn) { if (!table.nodeType) table = document.querySelector(table);

if (removeColumn) {
  deleteCol(removeColumn, table);
}

var ctx = { worksheet: name || "Worksheet", table: table.innerHTML };
// window.location.href = uri + base64(format(template, ctx))

downloadLink.download = name;
downloadLink.href = uri + base64(format(template, ctx));
downloadLink.click();

}; })();

dlrandy commented 4 years ago

https://github.com/clarketm/TableExport/blob/master/src/stable/js/tableexport.js