title: Javascript - export image into excel
body: <p>Using this function, I can export image into excel(XLS). This code require physical location of the image. But instead of physical path, I want to pass dataUri of the image. e.g. data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA...</p>
  
  <p>Is it possible?
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function exportExcel() {
  
      var template = { excel: '&lt;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"&gt;&lt;head&gt;&lt;meta name=ProgId content=Excel.Sheet&gt; &lt;meta name=Generator content="Microsoft Excel 11"&gt;&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;&lt;!--[if gte mso 9]&gt;&lt;xml&gt;&lt;x:ExcelWorkbook&gt;&lt;x:ExcelWorksheets&gt;&lt;x:ExcelWorksheet&gt;&lt;x:Name&gt;{worksheet}&lt;/x:Name&gt;&lt;x:WorksheetOptions&gt;&lt;x:DisplayGridlines/&gt;&lt;/x:WorksheetOptions&gt;&lt;/x:ExcelWorksheet&gt;&lt;/x:ExcelWorksheets&gt;&lt;/x:ExcelWorkbook&gt;&lt;/xml&gt;&lt;![endif]--&gt;&lt;/head&gt;&lt;body&gt;&lt;table&gt;{table}&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;' };
  
      var format = function (s, c) {
          return s.replace(new RegExp("{(\\w+)}", "g"), function (m, p) {
              return c[p];
          });
      };
  
      var base64 = function (s) {
          return window.btoa(window.unescape(encodeURIComponent(s)));
      };
  
      var ctx = { worksheet: 'Worksheet', table: '&lt;img src="file://C:/Temp/chart.png" alt="" /&gt;' };
  
      var b64 = base64(format(template.excel, ctx));
  
      blob = b64toBlob(b64, 'application/vnd.ms-excel');
      var blobUrl = window.URL.createObjectURL(blob);
  
      var saveLink = document.createElement('a');
  
      saveLink.download = 'export.xls';
      saveLink.href = blobUrl;
      saveLink.click();
  }
  
  
  function b64toBlob(b64Data, contentType, sliceSize) {
      contentType = contentType || '';
      sliceSize = sliceSize || 512;
  
      var byteCharacters = window.atob(b64Data);
      var byteArrays = [];
  
      var offset;
      for (offset = 0; offset &lt; byteCharacters.length; offset += sliceSize) {
          var slice = byteCharacters.slice(offset, offset + sliceSize);
  
          var byteNumbers = new Array(slice.length);
          var i;
          for (i = 0; i &lt; slice.length; i = i + 1) {
              byteNumbers[i] = slice.charCodeAt(i);
          }
  
          var byteArray = new window.Uint8Array(byteNumbers);
  
          byteArrays.push(byteArray);
      }
  
      var blob = new window.Blob(byteArrays, {
          type: contentType
      });
      return blob;
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Attached screenshot of requirement.
  <a href="https://i.stack.imgur.com/KCsee.png" rel="nofollow noreferrer">enter image description here</a>
  Thanks!</p>