1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| function vCSV(idTable, iDeb, iFin) { // iDeb commence à 0 ;)
var strCSV = '';
var strSeparateur = ';';
var i = 0;
var expRegMail = /<a href="mailto:(.*)">.*<\/a>/g; // Extraction des emails
var expReg = /<a.*>.*<\/a>/g; // Tout ce qui est dans des balises a
// Récupération de l'entête du tableau
$(idTable).find('thead').each(function(){
$(this).find('th').each(function(){
if (i >= iDeb && i <= iFin) strCSV += $.trim($(this).html()).replace(expReg, '')+strSeparateur;
i++;
});
strCSV += '\n';
});
// Récupération de chaque ligne
$(idTable).find('tbody').find('tr').each(function(){
i = 0;
$(this).find('td').each(function(){
// if (i >= iDeb && i <= iFin) strCSV += $.trim($(this).html()).replace(expReg, '')+strSeparateur;
if (i >= iDeb && i <= iFin) strCSV += $.trim($(this).html()).replace(expRegMail, '$1').replace(expReg, '')+strSeparateur;
i++;
});
strCSV += '\n';
});
// Création temporaire et artificielle d'une balise a pour utiliser l'attribut download afin de préciser le nom du fichier... si quelqu'un trouve mieux, je suis preneur!
// solution trouvé sur le net... mais je ne sais plus où
var tmpA = document.createElement("a");
tmpA.href = 'data:text/csv;charset:UTF-8,'+encodeURIComponent(strCSV);
tmpA.download = "tableau.csv";
document.body.appendChild(tmpA);
tmpA.click();
document.body.removeChild(tmpA);
} |