3

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?

Aycan Yaşıt
  • 2,106
  • 4
  • 34
  • 40
raviteja
  • 73
  • 1
  • 2
  • 6

2 Answers2

6

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.

Karthick Nagarajan
  • 1,327
  • 2
  • 15
  • 27
  • Hey thanks for the reply, its working great than before, but cell A11 need to extend to cell C11 as the data in cell C is last. if any data occupy cell D then Cell A need to ocuppy till cell D. basically need to look like a document in excel is there any way to apply css internal or external? . – raviteja Dec 26 '16 at 16:22
0

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

Community
  • 1
  • 1
Anurag Sinha
  • 1,014
  • 10
  • 17
  • Hey thank for your reply, how to include Apache POI in my webApplication, is there any js file ? – raviteja Dec 26 '16 at 16:28
  • Nope, there is no JS file. It's a java based library. This needs to be done on the server side or via applet(which is almost dead) – Anurag Sinha Dec 26 '16 at 17:20