반응형
Excel로 html 테이블을 내보내기 위한 Javascript
사용자가 '내보내기' 버튼을 클릭했을 때 내 페이지의 html 테이블을 Excel로 내보내야 합니다.스택 오버플로우에서 Firefox에서 작동하는 솔루션을 찾았습니다.
파이어폭스 브라우저에서 javascript에서 탁월하도록 동적 html 테이블을 내보냅니다.
여기서 흔히 사용하는 언어인 ,, which, which 등의 특수문자는 대응하고 있지 않기 때문에, 어떻게 하면 문제없이 수출할 수 있는지 아는 사람이 있습니까?
코드는 다음과 같습니다.
function tabletoExcel(table, name) {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/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]--></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]; }); };
if (!table.nodeType) table = document.getElementById(table);
var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML };
window.location.href = uri + base64(format(template, ctx));
}
추가할 경우:
<meta http-equiv="content-type" content="text/plain; charset=UTF-8"/>
문서의 선두에서 정상적으로 동작합니다.
<script type="text/javascript">
var tableToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/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>
UTF 8 변환 및 통화 기호 내보내기의 경우 다음을 사용하십시오.
var tableToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><?xml version="1.0" encoding="UTF-8" standalone="yes"?><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]--></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))
}
})()
ShieldUI의 Excel로 내보내기 기능은 이미 모든 특수 문자를 지원해야 합니다.
언급URL : https://stackoverflow.com/questions/17142427/javascript-to-export-html-table-to-excel
반응형
'sourcecode' 카테고리의 다른 글
Swift에서 열거값 이름을 얻는 방법은 무엇입니까? (0) | 2023.04.13 |
---|---|
Oracle 10g에서 테이블 열의 이름을 변경하는 방법 (0) | 2023.04.13 |
Excel 범위에 어레이 쓰기 (0) | 2023.04.13 |
탭 구분 파일을 CSV 형식으로 변환하려면 어떻게 해야 합니까? (0) | 2023.04.13 |
asp.net에서 사이트 전체에 https를 강제 적용하는 최선의 방법? (0) | 2023.04.08 |