<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>风雪之隅 &#187; excel</title>
	<atom:link href="http://www.laruence.com/tag/excel/feed" rel="self" type="application/rss+xml" />
	<link>http://www.laruence.com</link>
	<description>PHP语言, PHP扩展, Zend引擎相关的研究,技术,新闻分享 - 左手代码 右手诗</description>
	<lastBuildDate>Wed, 08 Feb 2012 05:12:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>使用JS做文档处理</title>
		<link>http://www.laruence.com/2009/05/18/809.html</link>
		<comments>http://www.laruence.com/2009/05/18/809.html#comments</comments>
		<pubDate>Mon, 18 May 2009 02:48:46 +0000</pubDate>
		<dc:creator>雪候鸟</dc:creator>
				<category><![CDATA[Js/CSS]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[fso]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[利用Javascript编写ActiveX控件]]></category>

		<guid isPermaLink="false">http://www.laruence.com/?p=809</guid>
		<description><![CDATA[  最近应友人邀请, 要帮忙写个文档处理脚本,  考虑到如果使用PHP或者Perl需要在友人的机器上,搭建一个运行环境,比较复杂, 使用起来也不友好, 本着以人为本的信念, 决定采用hta实现.
  
   本文就使用js来实现文档处理, 和使用js编写ActiveX做一个简单介绍
  ....]]></description>
			<content:encoded><![CDATA[<div class="copyright" >
<ul  style="padding-left:1em;font-size:85%;padding-left:1em;font-size:85%;">
<li>作者: <a href="http://www.laruence.com" >Laruence</a>(<a href="http://www.twitter.com/laruence"  target="meme"  title="Twitter" ><img src="/images/ico-twitter.png" /></a> <a href="http://t.sina.com/laruence"  target="meme"  title="新浪微博" ><img src="/images/ico-sina.png" /></a> <a href="http://fusion.google.com/add?feedurl=http://www.laruence.com/feed"  target="meme"  title="Google阅读器" ><img src="/images/ico-google.png" /></a> <a href="mailto:laruence@yahoo.com.cn"  target="meme"  title="邮件" ><img src="/images/ico-mail.png" /></a>)</li>
<li>本文地址: <a href="http://www.laruence.com/2009/05/18/809.html"  title="Permanet Link to 使用JS做文档处理" >http://www.laruence.com/2009/05/18/809.html</a></li>
</li>
<li>转载请注明出处 </li>
</ul></div>
<p>    最近应友人邀请, 要帮忙写个文档处理脚本,  考虑到如果使用PHP或者Perl需要在友人的机器上,搭建一个运行环境,比较复杂, 使用起来也不友好, 本着以人为本的信念, 决定采用hta实现.</p>
<p>    其实, 在Windows平台上, js可以调用很多Windows提供的ActivexObject, 在这个意义上来说, 用js写HTA时, js已经不再是传统意义上的Web客户端脚本了, 它已经有些类似于VB了.</p>
<p>    本文就使用js来实现文档处理, 和使用js编写ActiveX做一个简单介绍.</p>
<p>    言归正传, 友人的需求是: 把一个文件夹下的所有文件内容都读出来, 并且每个文件做为一个Excel的Sheet, 写入一个固定Excel中.</p>
<pre name="code"  class="sh_shell"  linenum="off"   style="background: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:Monacobackground: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:MonacoConsolasConsolasCourierCouriermonospace;monospace;">
    难点 :
　　　　1. 读取文件
              我们知道， js本身并没有读写文件的API。
          2. 写入Excel
              写入Excel，并且有多个Sheet是个难点。
</pre>
<pre name="code"  class="sh_shell"  linenum="off"   style="background: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:Monacobackground: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:MonacoConsolasConsolasCourierCouriermonospace;monospace;">
    解决方案：
          1. 采用FileSystemObject ActiveX Object 来实现文件读取，文件夹遍历
          2. 采用Excel Application ActiveX Object 来实现写Excel
</pre>
<p>    采用FSO遍历文件夹：</p>
<pre name="code"  class="sh_javascript"  linenum="off"   style="background: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:Monacobackground: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:MonacoConsolasConsolasCourierCouriermonospace;monospace;">
                var path = 'C:\\folder\\';
		var fso  = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;); //创建一个FSO实例
		var dir = fso.GetFolder(path);
		var fc = new Enumerator(dir.files); //构建可枚举实例
                var file = {};
		for(; !fc.atEnd(); fc.moveNext()) {
			file = fc.item(); //包含文件名
		        alert(file);
		}
</pre>
<p>   写Excel:</p>
<pre name="code"  class="sh_javascript"  linenum="off"   style="background: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:Monacobackground: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:MonacoConsolasConsolasCourierCouriermonospace;monospace;">
		// var files  包含了所有要文件路径的数组， 由上面的逻辑提供
		var fso  = new ActiveXObject(&quot;Scripting.FileSystemObject&quot;);
		try {
			var excelApp  = new ActiveXObject(&quot;Excel.Application&quot;);
		} catch (e) {
			alert(&quot;请确保您安装了 MS-EXCEL &quot;);
			return;
		}
		var excelBook = excelApp.Workbooks.Add();
		var excelSheet;
		var current = 1;
		for ( var file  in files ) {
			excelBook.Worksheets.Add();
			excelSheet = excelBook.WorkSheets(current++);
			excelSheet.name = file; //命名sheet
			//开始读取文件
			var fh = fso.OpenTextFile(file, 1/*reading*/);
			var content = '';
			while ( !fh.AtEndOfStream ) {
				  content += fh.ReadLine();
			}
			fh.close()

			excelSheet.Cells(1, 1).Value = content + '\t'; //写入
		}
		excelApp.ActiveWorkbook.SaveAs(&quot;C:\\TEST.XLS&quot;); //保存Excel文件
		excelSheet.Application.Quit();
  </pre>
<p>  用js来实现， 觉得最爽的就是调用ActiveX Obj，  完全不用我考虑太多~~ , 爽~
</p>
<p><strong>附录, 转载一个有趣的文章</strong>:<br/>
<strong>WSC脚本部件技术：利用Javascript编写ActiveX控件</strong></p>
<p>一个简单的例子：<br/>
将如下代码另存为.wsc文件，并右键“注册”（卸载时右键“不注册”）。</p>
<pre name="code"  class="sh_xml"  linenum="off"   style="background: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:Monacobackground: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:MonacoConsolasConsolasCourierCouriermonospace;monospace;">
&lt;Component&gt;
&lt;registration ProgId=hello.world /&gt;
&lt;public&gt;
        &lt;property name=testStr /&gt;
        &lt;method name=add&gt;
                &lt;parameter name=x /&gt;
                &lt;parameter name=y /&gt;
        &lt;/method&gt;
&lt;/public&gt;
&lt;script&gt;
var testStr=&quot;默认值&quot;;
function add(x, y)
{
        return x+&quot; + &quot;+y+&quot; = &quot;+(x+y);
}
&lt;/script&gt;
&lt;/Component&gt;
</pre>
<p>然后可以通过new ActiveXObject(&#8220;hello.world&#8221;)的方法来调用该控件。<br/>
例如：（另存为.js后双击运行。）</p>
<pre name="code"  class="sh_javascript"  linenum="off"   style="background: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:Monacobackground: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:MonacoConsolasConsolasCourierCouriermonospace;monospace;">
var com=new ActiveXObject(&quot;hello.world&quot;);
WSH.Echo(com.testStr);
WSH.Echo(com.add(84,25));
</pre>
<p>这样就可以将javascript代码嵌入到其他编程语言中。</p>
<p>另外，不注册也可以调用该控件，例如：（另存为.js后双击运行。）</p>
<pre name="code"  class="sh_javascript"  linenum="off"   style="background: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:Monacobackground: #333; color: #d9d9d9; border-left: 15px solid #c9c9c9; padding: 9px; font-size: 1em; overflow-x: auto;font-family:MonacoConsolasConsolasCourierCouriermonospace;monospace;">
var com=GetObject(&quot;script:http://www.zope.org/Members/Rimifon/ZopeTest/Sample.wsc&quot;);
WSH.Echo(com.testStr);
WSH.Echo(com.add(84,25));
</pre>
<p>注意的是，JS的GetObject方法因为安全限制，在IE下可能是不能运行的。</p>
<p>另外 还可以使用<br/>
regsvr32 http://www.zope.org/Members/Rimifon/ZopeTest/Sample.wsc<br/>
指令对远程脚本部件进行本地注册，也可以使用：<br/>
regsvr32 http://www.zope.org/Members/Rimifon/ZopeTest/Sample.wsc /u<br/>
进行卸载
</p>
<p><script type="text/javascript"  src="http://www.laruence.com/wp-content/plugins/shjs-syntax-hiliter/shjs/lang/sh_shell.js" ></script><script type="text/javascript"  src="http://www.laruence.com/wp-content/plugins/shjs-syntax-hiliter/shjs/lang/sh_shell.js" ></script><script type="text/javascript"  src="http://www.laruence.com/wp-content/plugins/shjs-syntax-hiliter/shjs/lang/sh_javascript.js" ></script><script type="text/javascript"  src="http://www.laruence.com/wp-content/plugins/shjs-syntax-hiliter/shjs/lang/sh_javascript.js" ></script><script type="text/javascript"  src="http://www.laruence.com/wp-content/plugins/shjs-syntax-hiliter/shjs/lang/sh_xml.js" ></script><script type="text/javascript"  src="http://www.laruence.com/wp-content/plugins/shjs-syntax-hiliter/shjs/lang/sh_javascript.js" ></script><script type="text/javascript"  src="http://www.laruence.com/wp-content/plugins/shjs-syntax-hiliter/shjs/lang/sh_javascript.js" ></script></p>
<hr/><h2>Comments</h2><ul  style="padding-left:1em;font-size:85%;padding-left:1em;font-size:85%;"><li><a href="http://www.laruence.com/2009/05/18/809.html" >2009/05/18</a>, <a href="http://54chen.com"  rel="external nofollow"  class="url" >cc0cc</a> writes: 没被当木马干掉？
哈哈
还是perl才是王道</li><li><a href="http://www.laruence.com/2009/05/18/809.html" >2009/05/18</a>, <a href="http://www.laruence.com"  rel="external nofollow"  class="url" >雪候鸟</a> writes: perl是王道,,,只是windows下,友人还是女孩,要考虑实际情况么....</li><li><a href="http://www.laruence.com/2009/05/18/809.html" >2009/05/18</a>, ttplay writes: 学习了. 感觉与vbs没多大区别</li><li><a href="http://www.laruence.com/2009/05/18/809.html" >2009/06/10</a>, daniel writes: It's a little jealous about your js ability...^_^</li><li><a href="http://www.laruence.com/2009/05/18/809.html" >2009/07/16</a>, <a href="http://www.324hour.cn"  rel="external nofollow"  class="url" >出国打工</a> writes: 这样挺好的，很喜欢</li><li><a href="http://www.laruence.com/2009/05/18/809.html" >2009/09/05</a>, pchao writes: 看不是很懂啊,新手,以后会长来学习</li><li><a href="http://www.laruence.com/2009/05/18/809.html" >2010/09/10</a>, <a href="http://www.cheapclshoes.com"  rel="external nofollow"  class="url" >christian louboutin</a> writes: 呵呵 代码很强大啊！perl才是天道</li></ul><hr/><h2>Related posts:</h2><ul  style="padding-left:1em;font-size:85%;padding-left:1em;font-size:85%;"><li><a href="http://www.laruence.com/2009/08/09/1036.html"  rel="bookmark"  title="Permanent Link: 正确使用JS中的正则" >正确使用JS中的正则</a></li><li><a href="http://www.laruence.com/2009/05/21/829.html"  rel="bookmark"  title="Permanent Link: JS文件装载器(Eve Js Loader)" >JS文件装载器(Eve Js Loader)</a></li></ul><hr/><small  style="font-size:85%;font-size:85%;">Copyright &copy; 2010 <a href="http://www.laruence.com"  target="_blank" >风雪之隅</a> 版权所有, 转载务必注明. 该Feed只供个人使用, 禁止未注明的转载或商业应用. 非法应用的, 一切法律后果自负. 如有问题, 可发E-mail至my at laruence.com.(Digital Fingerprint: 73540ba0a1738d7d07d4b6038d5615e2)</small><h2 class="related_post_title" >Related Posts:</h2><ul class="related_post"   style="padding-left:1em;font-size:85%;padding-left:1em;font-size:85%;"><li><a href="http://www.laruence.com/2012/01/07/2453.html"  title="2012年1月全球www网站技术报告" >2012年1月全球www网站技术报告</a></li><li><a href="http://www.laruence.com/2010/05/13/1462.html"  title="Javascript原型链和原型的一个误区" >Javascript原型链和原型的一个误区</a></li><li><a href="http://www.laruence.com/2010/01/21/1254.html"  title="IE下var的重要性的又一佐证" >IE下var的重要性的又一佐证</a></li><li><a href="http://www.laruence.com/2009/09/27/1123.html"  title="关于Javascript的俩个有趣的探讨" >关于Javascript的俩个有趣的探讨</a></li><li><a href="http://www.laruence.com/2009/09/23/1089.html"  title="深入理解JavaScript定时机制" >深入理解JavaScript定时机制</a></li><li><a href="http://www.laruence.com/2009/09/08/1076.html"  title="深入理解Javascript之this关键字" >深入理解Javascript之this关键字</a></li><li><a href="http://www.laruence.com/2009/08/09/1036.html"  title="正确使用JS中的正则" >正确使用JS中的正则</a></li><li><a href="http://www.laruence.com/2009/05/28/863.html"  title="Javascript作用域原理" >Javascript作用域原理</a></li><li><a href="http://www.laruence.com/2009/05/17/815.html"  title="关于事件模拟" >关于事件模拟</a></li><li><a href="http://www.laruence.com/2008/09/01/492.html"  title="Js处理Json的&#8221;invalid label&#8221;错" >Js处理Json的&#8221;invalid label&#8221;错</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.laruence.com/2009/05/18/809.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

