---
title: "用js保存页面指定内容、指定文件类型"
author: "ablya"
date: "2009-09-17T18:05:05Z"
tags: []
slug: "js"
draft: false
lastmod: "2009-09-17T18:05:05Z"
id: 470613
url: "https://ablya.iteye.com/blog/470613"
---

用js保存页面指定内容
转自：  http://rorchina.iteye.com/blog/184425

```java

<script language=javascript>   

function Save(){   
    var txt = document.all.output.value;
    b = window.open();  
    b.document.open();  
    b.document.write(txt);  
    b.document.close();
    b.document.execCommand('saveas',true,'output.html');
    b.window.close();
}

</script>
 


<center>仅保存文本框中部分：</center>
<table width="90%" align="center" border="0" cellpadding="0" cellspacing="0"> 
    <tr> 
		<td>不保存</td> 
		<td><input type="button" value="Save" class="trans" onclick="Save()">

		<textarea rows="20" cols="50" id="output" class="txtedit"></textarea>
		</td> 
    </tr> 
</table>

```


保存HTML中的Table到Excel中

转自：http://www.bcbbs.net/news/Content28841.aspx

```java

<html> 
  <head> 
     <title>保存HTML中的Table到Excel中</title> 
  </head> 
  <body> 
     <h1>保存内容到Excel中</h1> 
     <table id='content'> 
         <tr><td>列1</td><td>列2</td></tr> 
         <tr><td>a1</td><td>a2</td></tr> 
         <tr><td>b1</td><td>b2</td></tr> 
         <tr><td>c1</td><td>c2</td></tr> 
         <tr><td>d1</td><td>d2</td></tr> 
         <tr><td>e1</td><td>e2</td></tr> 
         <tr><td>f1</td><td>f2</td></tr> 
     </table> 
      <a href="javascript:downloadfile('content')">保存文件</a> 
    <script language="javascript"> 
       function downloadfile(id)  
      {  
              window.document.write(document.getElementById(id).outerHTML);  
              window.document.execCommand("SaveAs",false,"C:\\download.xls");  
              history.go(-1);  
      }  
    </script> 
  </body> 
</html>  

```