Javascript 通过TableExport.js将表格上载到excel时保持样式格式
我正在使用Javascript 通过TableExport.js将表格上载到excel时保持样式格式,javascript,export,export-to-excel,Javascript,Export,Export To Excel,我正在使用TableExport.js将我的html表格导出到Excel文件。我可以导出表,但在excel工作表中,css失败。帮我解决这个问题。我只使用inlinecss。我在下面附上了我的代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Table2csv Plugin</title> <link
TableExport.js
将我的html
表格导出到Excel
文件。我可以导出表,但在excel工作表中,css
失败。帮我解决这个问题。我只使用inline
css。我在下面附上了我的代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Table2csv Plugin</title>
<link rel="shortcut icon" href="favicon.ico">
<!-- <link href="css/table2csv.css" rel="stylesheet"> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.14.0/xlsx.core.min.js"></script>
<script src="FileSaver.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/TableExport/5.0.0/css/tableexport.css"></script>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/TableExport/5.0.0/js/tableexport.js"></script>
</head>
<body>
<table id="Population-list">
<caption><h2>Countries by population</h2></caption>
<thead>
<tr>
<th bgcolor="#00FF00">Rank</th>
<th bgcolor="#00FF00">Country</th>
<th bgcolor="#00FF00">Population</th>
<th bgcolor="#00FF00">% of world population</th>
<th bgcolor="#00FF00">Date</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td bgcolor="#00FF00">China</td>
<td>1,370,570,000</td>
<td>18.9%</td>
<td>June 24, 2015</td>
</tr>
<tr>
<td>2</td>
<td bgcolor="#00FF00">India</td>
<td>1,273,140,000</td>
<td>17.6%</td>
<td>June 24, 2015</td>
</tr>
.
.
.
.
.
.
<tr>
<td>8</td>
<td bgcolor="#00FF00">Bangladesh</td>
<td>126,880,000</td>
<td>2.19%</td>
<td>June 24, 2015</td>
</tr>
<tbody>
</table>
<script>
TableExport(document.getElementsByTagName("table"), {
headers: true, // (Boolean), display table headers (th or td elements) in the <thead>, (default: true)
footers: true, // (Boolean), display table footers (th or td elements) in the <tfoot>, (default: false)
formats: ['xlsx', 'csv', 'txt'], // (String[]), filetype(s) for the export, (default: ['xlsx', 'csv', 'txt'])
filename: 'id', // (id, String), filename for the downloaded file, (default: 'id')
bootstrap: false, // (Boolean), style buttons using bootstrap, (default: true)
exportButtons: true, // (Boolean), automatically generate the built-in export buttons for each of the specified formats (default: true)
position: 'bottom', // (top, bottom), position of the caption element relative to table, (default: 'bottom')
ignoreRows: null, // (Number, Number[]), row indices to exclude from the exported file(s) (default: null)
ignoreCols: null, // (Number, Number[]), column indices to exclude from the exported file(s) (default: null)
trimWhitespace: true // (Boolean), remove all leading/trailing newlines, spaces, and tabs from cell text in the exported file(s) (default: false)
});
</script>
</body>
</html>
表2CSV插件
按人口分列的国家
等级
国家
人口
%世界人口比例
日期
1.
中国
1,370,570,000
18.9%
2015年6月24日
2.
印度
1,273,140,000
17.6%
2015年6月24日
.
.
.
.
.
.
8.
孟加拉国
126,880,000
2.19%
2015年6月24日
TableExport(document.getElementsByTagName(“表格”){
headers:true、//(布尔值),在中显示表格标题(th或td元素)(默认值:true)
页脚:true、//(布尔值),在中显示表格页脚(th或td元素)(默认值:false)
格式:['xlsx',csv',txt',/(字符串[]),导出的文件类型(默认值:['xlsx',csv',txt'])
文件名:'id',/(id,String),下载文件的文件名,(默认值:'id')
引导:false,//(布尔值),使用引导设置样式按钮(默认值:true)
exportButtons:true、//(布尔值),自动为每个指定格式生成内置的导出按钮(默认值:true)
位置:'bottom',//(top,bottom),标题元素相对于表的位置(默认值:'bottom')
ignoreRows:null,//(Number,Number[]),要从导出文件中排除的行索引(默认值:null)
ignoreCols:null,//(Number,Number[]),要从导出文件中排除的列索引(默认值:null)
trimWhitespace:true//(布尔值),从导出文件中的单元格文本中删除所有前导/尾随的换行符、空格和制表符(默认值:false)
});
如果您指的是将CSS样式(例如颜色、边距)应用于导出的电子表格本身,很遗憾,这没有实现。这些样式仅适用于html,而不会传输到xlsx文档。如果你指的是html表格中的样式,请参考哦!我运气不好。你能给我一些建议来实现我的目标吗?在幕后,TableExport利用解析和编写xlsx文档,而这个库实际上(在一定程度上)支持单元格样式。如果尚未存在实现此功能的库,则需要直接使用或添加对TableExport本身的支持。好的,谢谢您的建议。