<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script>
//不支持IE6,7,8
function MakeWord() {
try {
if (!window.Blob) {
alert('不支持您的瀏覽器!');
return;
}
var html, link, blob, url,css;//定義變量
css = (
'<style>' +
'table{border-collapse:collapse;}td{border:1px gray solid;width:5em;padding:2px;}'+
'</style>'
);//定義導出樣式
html = window.div_test.innerHTML; //獲取頁面內容
blob = new Blob(['\ufeff', css+html], { type: 'application/msword' });//一個能夠存儲二進制文件的容器
url = URL.createObjectURL(blob);//獲取下載url
link = document.createElement('a');//建立連接
link.href = url;
link.download = '測試1';
document.body.appendChild(link);
if (navigator.msSaveOrOpenBlob) {
navigator.msSaveOrOpenBlob(blob, '測試1.doc'); // IE10-11
}
else {
link.click();
}
document.body.removeChild(link);
} catch (e) {
alert("導出失敗!");
window.close();
}
}
</script>
</head>
<body>
<button onclick=" MakeWord()">導出word</button>
<div id="div_test">
<table >
<tr>
<td>A1</td>
<td>B1</td>
<td>C1</td>
<td>E1</td>
</tr>
<tr>
<td>A2</td>
<td>B2</td>
<td>C2</td>
<td>E2</td>
</tr>
<tr>
<td>A3</td>
<td>B3</td>
<td>C3</td>
<td>E3</td>
</tr>
<tr>
<td>A4</td>
<td>B4</td>
<td>C4</td>
<td>E4</td>
</tr>
</table>
</div>
</body>
</html>css