Javascript 如何将html表格转换为csv字符串
我有一张桌子,里面放着ad、tfoot、tr、td和th。td或th具有colspan或rowspan属性,如何将其转换为csv字符串,而每个合并的单元格都不合并为多个字段 例如:Javascript 如何将html表格转换为csv字符串,javascript,jquery,Javascript,Jquery,我有一张桌子,里面放着ad、tfoot、tr、td和th。td或th具有colspan或rowspan属性,如何将其转换为csv字符串,而每个合并的单元格都不合并为多个字段 例如: <table> <thead> <tr> <th>1</th> <th>2</th> </tr> </thead> <tr> <td
<table>
<thead>
<tr>
<th>1</th>
<th>2</th>
</tr>
</thead>
<tr>
<td rowspan='2'>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
</tr>
<tfoot>
<tr>
<th colspan='2'>6 <span style='display:none'> 7
</span> 8</th>
</tr>
</tfoot>
</table>
除此之外还有其他插件吗?因为这一行在第一行而不是最后一行显示tfoot,所以也合并了显示为单个字段的单元格。如果不使用IE,可以使用table2CSV&JQuery进行合并
$(document).ready(function() {
$('table').each(function() {
var $table = $(this);
var $button = $("<button type='button'>");
$button.text("Export to spreadsheet");
$button.insertAfter($table);
$button.click(function() {
var csv = $table.table2CSV({delivery:'value'});
window.location.href = 'data:text/csv;charset=UTF-8,'
+ encodeURIComponent(csv);
});
});
})
$(文档).ready(函数(){
$('table')。每个(函数(){
var$表=$(此);
var$按钮=$(“”);
$button.text(“导出到电子表格”);
$button.insertAfter($table);
$按钮。单击(函数(){
var csv=$table.table2CSV({delivery:'value'});
window.location.href='data:text/csv;charset=UTF-8,'
+编码组件(csv);
});
});
})
我制作了一个100%免费的jQuery插件
您可以立即将其与jQuery集成
我们的目标是尝试以尽可能最好的方式完成这项工作
代码如下:
(function(window,undefined){
window.T2CSV=function(table){
if(! (table instanceof window.HTMLTableElement))
{
throw new window.TypeError('A <table> element is required, instead '+table+' was passed');
}
var tr,thead,csv,tfoot,cols,prop=(table.innerText===undefined?'textContent':'innerText'),
setVars=function(){
var elements=table.getElementsByTagName('tr');
if(elements.length<1)
{
throw new window.RangeError('At least 1 <tr> element is required, you have 0 on your <table>.');
}
tr=Array.prototype.slice.call(elements,1);
thead=elements[0];
cols=thead.children.length;
elements=null; //free memory
csv='';
},
render={
header:function(){
if(! (thead.children[0] instanceof window.HTMLTableCellElement))
{
throw new window.RangeError('At least 1 <tr> element with 1 <td> or <th> is required.');
}
for(var i=0,children=thead.children,l=children.length,csv=[];i<l;i++)
{
csv[csv.length]='"'+children[i][prop]+'"';
}
children=null; //free memory
return csv;
},
data:function(){
if(!tr.length)
{
return '';
}
for(var i=0,l=tr.length,csv=[],tfoot=false;i<l;i++)
{
if(!tfoot && tr[i].parentNode.tagName=='TFOOT')
{
tfoot=tr[i];
continue;
}
csv[csv.length]=render.row(tr[i]);
}
if(tfoot)
{
csv[csv.length]=render.row(tfoot);
}
return csv.join('\r\n');
},
row:function(tr){
var td=tr.getElementsByTagName('td');
if(!td.length)
{
td=tr.getElementsByTagName('th');
}
for(var i=0,tmp=[];i<cols;i++)
{
tmp[i]=td[i]?'"'+td[i][prop]+'"':'""';
}
return tmp+'';
}
};
setVars();
return {
toString:function(){
if(csv)
{
return csv;
}
return csv = [render.header(),render.data()].join('\r\n');
},
valueOf:function(){return this.toString();},
refresh:function(){
setVars();
}
}
}
})(function(){}.constructor('return this')());
它产生了以下结果:
"1","2"
"3","4"
"5",""
"6 7 8",""
代码可以很容易地定制
我正在考虑更新它,并为分离器和配置添加逃逸
请在此处查看操作:
更新(15/11/2014): 我做了一个改进版 现在它不再使用斜杠和双引号 我仍然没有添加对更多分隔符和文本引号的支持 以下是我的代码:
(function(window,undefined){
window.T2CSV=function(table){
if(!(table instanceof window.HTMLTableElement))
{
throw new window.TypeError('A <table> element is required, instead '+table+' was passed');
}
var tr,thead,cols,tfoot,csv={
header:'',
data:[],
footer:'',
string:''
},
prop=(table.innerText===undefined?'textContent':'innerText'),
setVars=function(){
var elements=table.getElementsByTagName('tr');
if(elements.length<1)
{
throw new window.RangeError('At least 1 <tr> element is required, you have 0 on your <table>.');
}
tr=Array.prototype.slice.call(elements,1);
thead=elements[0];
cols=thead.children.length;
elements=null; //free memory
csv={
header:'',
data:[],
footer:'',
string:''
};
},
addSlashes=function(data){
return data.replace(/([\\"])/g,'\\$1');
},
render={
header:function(){
if(! (thead.children[0] instanceof window.HTMLTableCellElement))
{
throw new window.RangeError('At least 1 <tr> element with 1 <td> or <th> is required.');
}
for(var i=0,children=thead.children,l=children.length,tmp=[];i<l;i++)
{
tmp[tmp.length]='"'+addSlashes(children[i][prop])+'"';
}
children=null; //free memory
return csv.header=tmp;
},
data:function(){
if(!tr.length)
{
return '';
}
for(var i=0,l=tr.length,tmp=[],tfoot=false;i<l;i++)
{
if(!tfoot && tr[i].parentNode.tagName=='TFOOT')
{
tfoot=tr[i];
continue;
}
csv.data[tmp.length]=tmp[tmp.length]=render.row(tr[i]);
}
if(tfoot)
{
csv.footer=tmp[tmp.length]=render.row(tfoot);
}
return tmp.join('\r\n');
},
row:function(tr){
var td=tr.getElementsByTagName('td');
if(!td.length)
{
td=tr.getElementsByTagName('th');
}
for(var i=0,tmp=[];i<cols;i++)
{
tmp[i]=td[i]?'"'+addSlashes(td[i][prop])+'"':'""';
}
return tmp+'';
}
};
setVars();
return {
toString:function(){
if(csv.string)
{
return csv.string;
}
return csv.string = [render.header(),render.data()].join('\r\n');
},
valueOf:function(){return this.toString();},
refresh:function(){
setVars();
},
getHeader:function(){
return csv.header;
},
getFooter:function(){
return csv.footer;
},
getRows:function(){
return csv.data;
},
getRow:function(row){
return csv.data[row>>0];
}
};
}
})(function(){}.constructor('return this')());
(函数(窗口,未定义){
window.T2CSV=函数(表){
if(!(window.HTMLTableElement的表实例))
{
抛出新窗口.TypeError('需要一个元素,而不是传递了'+table+');
}
变量tr、THAD、cols、tfoot、csv={
标题:“”,
数据:[],
页脚:“”,
字符串:“”
},
prop=(table.innerText==未定义的?'textContent':'innerText'),
setVars=函数(){
var elements=table.getElementsByTagName('tr');
if(elements.length)您可以随时手动操作。有些问题要求我们推荐或查找书籍、工具、软件库、教程或其他非现场资源,这些都是堆栈溢出的主题,因为它们往往会吸引自以为是的答案和垃圾邮件。相反,请描述问题以及迄今为止为解决问题所做的工作。在“结束问题”中理由。在这里探索是的,但是所有使用Table 2CSV的解决方案,在第一行@ ANKIT.JBIT中提到的bug都不适用于我在前2行中指定的。请参阅:我不认为合并的单元格是一个bug,因为谁说它不应该是一个单一的值(而你正以这种方式显示)。.由于您可能有一些选择,因此该表是如何填写的。
"1","2"
"3","4"
"5",""
"6 7 8",""
(function(window,undefined){
window.T2CSV=function(table){
if(!(table instanceof window.HTMLTableElement))
{
throw new window.TypeError('A <table> element is required, instead '+table+' was passed');
}
var tr,thead,cols,tfoot,csv={
header:'',
data:[],
footer:'',
string:''
},
prop=(table.innerText===undefined?'textContent':'innerText'),
setVars=function(){
var elements=table.getElementsByTagName('tr');
if(elements.length<1)
{
throw new window.RangeError('At least 1 <tr> element is required, you have 0 on your <table>.');
}
tr=Array.prototype.slice.call(elements,1);
thead=elements[0];
cols=thead.children.length;
elements=null; //free memory
csv={
header:'',
data:[],
footer:'',
string:''
};
},
addSlashes=function(data){
return data.replace(/([\\"])/g,'\\$1');
},
render={
header:function(){
if(! (thead.children[0] instanceof window.HTMLTableCellElement))
{
throw new window.RangeError('At least 1 <tr> element with 1 <td> or <th> is required.');
}
for(var i=0,children=thead.children,l=children.length,tmp=[];i<l;i++)
{
tmp[tmp.length]='"'+addSlashes(children[i][prop])+'"';
}
children=null; //free memory
return csv.header=tmp;
},
data:function(){
if(!tr.length)
{
return '';
}
for(var i=0,l=tr.length,tmp=[],tfoot=false;i<l;i++)
{
if(!tfoot && tr[i].parentNode.tagName=='TFOOT')
{
tfoot=tr[i];
continue;
}
csv.data[tmp.length]=tmp[tmp.length]=render.row(tr[i]);
}
if(tfoot)
{
csv.footer=tmp[tmp.length]=render.row(tfoot);
}
return tmp.join('\r\n');
},
row:function(tr){
var td=tr.getElementsByTagName('td');
if(!td.length)
{
td=tr.getElementsByTagName('th');
}
for(var i=0,tmp=[];i<cols;i++)
{
tmp[i]=td[i]?'"'+addSlashes(td[i][prop])+'"':'""';
}
return tmp+'';
}
};
setVars();
return {
toString:function(){
if(csv.string)
{
return csv.string;
}
return csv.string = [render.header(),render.data()].join('\r\n');
},
valueOf:function(){return this.toString();},
refresh:function(){
setVars();
},
getHeader:function(){
return csv.header;
},
getFooter:function(){
return csv.footer;
},
getRows:function(){
return csv.data;
},
getRow:function(row){
return csv.data[row>>0];
}
};
}
})(function(){}.constructor('return this')());