admin管理员组文章数量:1346047
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 ercial 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>
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 ercial 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:
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?
Share Improve this question edited Dec 23, 2016 at 8:25 Aycan Yaşıt 2,1204 gold badges35 silver badges42 bronze badges asked Dec 23, 2016 at 7:56 ravitejaraviteja 731 gold badge2 silver badges6 bronze badges2 Answers
Reset to default 6you 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-:office:office" xmlns:x="urn:schemas-microsoft-:office:excel" xmlns="http://www.w3/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.
I think you can update excel files using Apache POI library. Refer https://poi.apache/ for more details.
Regarding making the lines disappear, refer http://apache-poi.1045710.n5.nabble./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
本文标签: Export html content to excel in javascriptStack Overflow
版权声明:本文标题:Export html content to excel in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743823424a2545202.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论