How To Export An HTML Table As A .xlsx File
Solution 1:
A great client-side tool for exporting html
tables to xlsx
, xls
, csv
, or txt
is TableExport by clarketm (me). It is a simple, easy-to-implement, full-featured library with a bunch of configurable properties and methods.
Install
$ npm install tableexport
Usage
TableExport(document.getElementsByTagName("table"));
// OR using jQuery
$("table").tableExport();
Documentation
Sample apps to get you started
- TableExport + RequireJS
- TableExport + Flask
- TableExport + Webpack 1
- TableExport + Angular 4 + Webpack 2
Check out the compendious docs or just head over to
TableExport
on Github for a full list of features.
Solution 2:
You can use this plug-in for exporting table to .xlsx
Solution 3:
Take a look at tableExport.jquery.plugin or tableexport.jquery.plugin
Code example
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML table Export</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="../lib/js-xlsx/xlsx.core.min.js"></script>
<script type="text/javascript" src="../lib/FileSaver/FileSaver.min.js"></script>
<script type="text/javascript" src="../lib/html2canvas/html2canvas.min.js"></script>
<script type="text/javascript" src="../tableExport.js"></script>
<script type="text/javaScript">
var sFileName = 'ngophi';
function ExportXLSX(){
$('#Event').tableExport({fileName: sFileName,
type: 'xlsx'
});
}
</script>
<style type="text/css">
body {
font-size: 12pt;
font-family: Calibri;
padding : 10px;
}
table {
border: 1px solid black;
}
th {
border: 1px solid black;
padding: 5px;
background-color:grey;
color: white;
}
td {
border: 1px solid black;
padding: 5px;
}
input {
font-size: 12pt;
font-family: Calibri;
}
</style>
</head>
<body>
<a href="#" onClick="ExportXLSX();">DownloadXLSX</a>
<br/>
<br/>
<div id="Event">
<table>
<tr>
<th>Column One</th>
<th>Column Two</th>
<th>Column Three</th>
</tr>
<tr>
<td>row1 Col1</td>
<td>row1 Col2</td>
<td>row1 Col3</td>
</tr>
<tr>
<td>row2 Col1</td>
<td>row2 Col2</td>
<td>row2 Col3</td>
</tr>
<tr>
<td>row3 Col1</td>
<td>row3 Col2</td>
<td><a href="http://www.jquery2dotnet.com/">http://www.jquery2dotnet.com/</a>
</td>
</tr>
</table>
</div>
</body>
</html>
Solution 4:
You won't be able to export it as XLSX without going back to the server. A XLSX file is a collection of XML files, zipped together. This means you do need to create multiple files. This is impossible to do with JS, client-side.
Instead, you should create a function retrieving the data from your HTML table and send that to you server. The server can then create the XLSX file for you (there are a bunch of libs available for that!) and send it back to the client for download.
If you expect to have a huge dataset, the XLSX creation on the server should be done as an async process, where you notify the user when it's done (instead of having the user waiting for the file to be created).
Let us know which language you use on your server, and we'll be able to recommend you some good libraries.
Post a Comment for "How To Export An HTML Table As A .xlsx File"