保存javascript生成的文档 [英] Save the document generated by javascript

查看:162
本文介绍了保存javascript生成的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Javascript可以操纵浏览器显示的文档,因此以下内容:

Javascript can manipulate the document the browser is displaying, so the following:

<script>
    document.write("<table><tr><td>Hola</td><td>Adios</td></tr></table>");
</script>

将使浏览器显示一个表,就像它是原始HTML文档一样:

Will make the browser display a table just like if it was the original HTML document:

<table>
    <tr>
        <td>Hola</td>
        <td>Adios</td>
    </tr>
</table>

我可以保存/提供此文档内容吗?

Is there a way I can save/serve this document content?

目前我们使用Ext-js生成一些很好的报告,我想做的是拥有它的text / html版本(我的意思是,某些东西)这不需要javascript)

Currently we have some nicely generated reports using Ext-js, what I would like to do is to have the "text/html" version of it ( I mean, something that doesn't require javascript )

所以在页面的末尾我会添加一个按钮:另存为blaba,文档应显示文本/纯文本版本。

So at the end of the page I would add a button : "Save as blaba" and the document should display the text/plain version.

我现在想的唯一方法是将内容写入javascript变量,如:

The only way I could think right now is, to write the content into a javascript variable like:

 var content = document.toString(); // or something magic like that.
 // post it to the server

然后将该值发布到服务器,并且服务器提供该值。

Then post that value to the server, and have the server present that value.

 <%=request.getParameter("content-text")%>

但看起来很棘手。

还有替代方案吗?

编辑

好的,我几乎得到了它。现在我只需要弹出一个新窗口,这样选项你想保存它吗

Ok, I almost got it. Now I just need the new window to pop up so the option "would you like to save it shows"

这是我到目前为止所拥有的

This is what I've got so far

<script>
    document.write("<div id='content'><table><tr><td>Hola</td><td>Adios</td></tr></table></div>");
    function saveAs(){
        var sMarkup =  document.getElementById('content').innerHTML; 
        var oNewDoc = document.open('application/vnd.ms-excel');        
        oNewDoc.write( sMarkup + "<hr>" );
        oNewDoc.close();
    }
</script>

<input type="button" value="Save as" onClick="saveAs()"/>

行:

    var oNewDoc = document.open('application/vnd.ms-excel');        

应该指定新的内容类型,但它会被忽略。

Should specify the new content type, but it is being ignored.

推荐答案

以下是以.xls格式打开表格内容的升级版本。

Here's the upgraded version to open the table contents in .xls format.

<head>
<script>

         document.write("<table id='targetTable'><tr><td>Hola</td><td>Adios</td></tr><tr><td>eins</td><td>zwei</td></table>"); 
        function saveAsXLS()
        {
            var xlObj = new ActiveXObject("Excel.Application");
            var xlBook = xlObj.Workbooks.Add();
            var xlSheet = xlBook.Worksheets(1);
            for (var y=0;y<targetTable.rows.length;y++) // targetTable=id of the table
            {
                for (var x=0;x<targetTable.rows(y).cells.length;x++)
                {
                    xlSheet.Cells(y+1,x+1)=targetTable.rows(y).cells(x).innerText;
                }
            }   
            xlObj.Visible=true;
            document.write("The table contents are opened in a new Excel sheet.");//Print on webpage 
        }
</script>
</head>
<body>  
<input type="button" value="Open table in Excel!" onclick="saveAsXLS()"/> 
</body>

此代码在IE6中测试并使用ActiveXObject控件。

This code is tested in IE6 and is using ActiveXObject control.


  • 我在这里使用的表格的顺序为2x2,各个内容分别映射到excel表格中。

  • 与.doc版本不同,这不会将文件保存在客户端系统中。它用表格内容打开应用程序,客户端必须保存它。

希望这有助于回答你的问题。 Lemme知道你是否面临任何问题。

Hope this helps in answering ur question. Lemme know if u face any issues.

和平。

这篇关于保存javascript生成的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆