raviteja
raviteja

Reputation: 73

Export html content to excel in javascript

I am exporting Html content to Excel and download the Excel in JavaScript. It works fine but my requirement is to download Excel without Grid Lines and also I want to render the content as my html shows in browser. Is there any options or suggestions to render Html content in Excel without gridlines?

Below is my sample Html:

<div>
<div style="float:left">
NAME:some text
</div>
<div style="float:right">
Number:some number
</div>
</div>
<div>
<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
</table>
</div>

And my excel sheet: enter image description here

I don't want those grid cells and my content is not rendered as Html content.

Is there any way to prepare excel with Html data without gridlines in Javascript?

Upvotes: 3

Views: 17525

Answers (2)

Karthick Nagarajan
Karthick Nagarajan

Reputation: 1345

you just add this:

<meta http-equiv="content-type" content="text/plain; charset=UTF-8"/>

in the head of the document it will start working as expected:

<script type="text/javascript">
var tableToExcel = (function() {
  var uri = 'data:application/vnd.ms-excel;base64,'
    , 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]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>'
    , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
  return function(table, name) {
    if (!table.nodeType) table = document.getElementById(table)
    var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
    window.location.href = uri + base64(format(template, ctx))
  }
})()
</script>

Fiddle Here.

Upvotes: 6

Anurag Sinha
Anurag Sinha

Reputation: 1032

I think you can update excel files using Apache POI library. Refer https://poi.apache.org/ for more details.

Regarding making the lines disappear, refer http://apache-poi.1045710.n5.nabble.com/how-to-make-the-gridline-disappeared-by-API-td2297448.html

Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("sheet");
sheet.setDisplayGridlines(false);

More details at Remove all borders on a specific excel worksheet using Apache POI

Upvotes: 0

Related Questions