728x90
반응형
■ Javascript Export HTML to Word Document with JavaScript
플러그인이나 라이브러리 없이 JavaScript만 사용하여 HTML을 Word 문서로 변환하는 법
■ 사용법
- HTML
<div id="source-html">
<h1>
<center>Artificial Intelligence</center>
</h1>
<h2>Overview</h2>
<p>
Artificial Intelligence(AI) is an emerging technology
demonstrating machine intelligence. The sub studies like <u><i>Neural
Networks</i>, <i>Robatics</i> or <i>Machine Learning</i></u> are
the parts of AI. This technology is expected to be a prime part
of the real world in all levels.
</p>
</div>
<div class="content-footer">
<button id="btn-export" onclick="exportHTML();">Export to
word doc</button>
</div>
- Javascript
<script>
function exportHTML(){
var header = "<html xmlns:o='urn:schemas-microsoft-com:office:office' "+
"xmlns:w='urn:schemas-microsoft-com:office:word' "+
"xmlns='http://www.w3.org/TR/REC-html40'>"+
"<head><meta charset='utf-8'><title>Export HTML to Word Document with JavaScript</title></head><body>";
var footer = "</body></html>";
var sourceHTML = header+document.getElementById("source-html").innerHTML+footer;
var source = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(sourceHTML);
var fileDownload = document.createElement("a");
document.body.appendChild(fileDownload);
fileDownload.href = source;
fileDownload.download = 'document.doc';
fileDownload.click();
document.body.removeChild(fileDownload);
}
</script>
728x90
반응형
'Javascript' 카테고리의 다른 글
[Javascript] alert 창 꾸미기 alertify.js 사용법 (0) | 2022.03.24 |
---|---|
[Javascript] 인증번호 타이머 (0) | 2022.03.22 |
[Javascript] textarea code highlighter effects (1) | 2022.03.14 |
[Javascript] textarea 영역에서 tab key 적용하기 탭키 적용 onkeyup (0) | 2022.03.08 |
[Javascript] sweetAlert (알림팝업창) 사용하기 (사용법, 사용예제) (0) | 2022.03.07 |