関連ページ  
力任せの WEB アプリ (2) -- IFRAME 内の Excel アクセス

ダウンロードした Excel を IFRAME 内に表示する
c:\\tmp\\001\\app に Excel をダウンロードしていますが、
ここでは、実行しているページのあるファイルシステムのパスです。

ですから、ダウンロード直後に "webexcel.xls" として、パス無しで
セットしています。


Excel のロードはいろいろ問題があって注意する必要がありますが、
IFRAME の onLoad イベントを使って、実際ロードしたい URL を
セットする前に、"about:blank" をセットします。

これは、て前回ロードしていたExcel そのものをプロセスから
アンロードする処理になります。

※ 初回のみフラグで実行しないようにしてあります
<IFRAME
	id="xls"
	frameborder="no"
	scrolling="no"
	width="600"
	height="400"
	onLoad="download()"
></IFRAME>
<br>
<INPUT type="button" value="download" onClick="flg=true;reload()">

<script type="text/javascript">

var flg = false;

function reload() {

	document.getElementById("xls").contentWindow.location = "about:blank";

}

function download() {

	if ( !flg ) {
		return;
	}

	var objSrvHTTP = new ActiveXObject("Msxml2.ServerXMLHTTP");
	var Stream = new ActiveXObject("ADODB.Stream");

	objSrvHTTP.open("GET", "http://localhost/php/test/Book1.xls", false );
	objSrvHTTP.send();

	Stream.Open();
	Stream.Type = 1;
	Stream.Write( objSrvHTTP.responseBody )
	Stream.SaveToFile( "c:\\tmp\\001\\app\\webexcel.xls", 2 );
	Stream.Close()

	document.getElementById("xls").contentWindow.location = "webexcel.xls";


}

</script>
最も注意する事は、IFRAME に表示されている間( プロセス上に Excel が存在する間 )
は、その Excel ブックをローカルの Excel で開かないようにする事です。

そうしないと、タスクの中に非表示の Excel が残ってしまう事があります。
残っている場合は、すみやかにタスクマネージャから終了させて下さい。




IFRAME 内の Excel のセルの内容を HTML 上の VBScript で変更する
ここからは、VBScript です。Windows のバージョンや、Office のバージョンに左右される
可能性がありますが、Excel のオブジェクトを使うとなると VBScript でないと
いろいろまずい結果になります。

※ IE の設定として、「ページを表示するごとに確認する」にしておく必要があります


JavaScript での大きな違いはやはり、Set ExcelApp = Nothing の一文で、
JavaScript では全く同じ動作をする手段が無いようです。( null セットは動作せず )

Windows 2000 と 古い Office なら JavaScript でも動いたのですが・・・


結局全て VBScript で書くのが「吉」のようです
<HTML>
<HEAD>
<script language="VBScript">

	Dim flg,ExcelSheet,MyBook
	flg = False

</script>
</HEAD>
<BODY>
<IFRAME
	id="xls"
	frameborder="no"
	scrolling="no"
	width="600"
	height="400"
	onLoad="Call update_cell()"
></IFRAME>
<br>
<INPUT type="button" value="この値をセルにセット" onClick="flg=True:Call reload()">
<INPUT type="text" id="cell">

<script language="VBScript">


document.getElementById("xls").contentWindow.location = "webexcel.xls"

function update_cell() 

	if flg then

		Set ExcelApp = CreateObject("Excel.Application")
		Set MyBook = ExcelApp.Workbooks.Open("c:\\tmp\\001\\app\\webexcel.xls")

		MyBook.Sheets("Sheet1").Cells(1, 1) = document.getElementById("cell").value

		MyBook.Save()
		ExcelApp.Quit()

		Set ExcelApp = Nothing

		document.getElementById("xls").contentWindow.location = "webexcel.xls"

	end if

end function

function reload() 

	if flg then
		document.getElementById("xls").contentWindow.location = "about:blank"
	end if

end function


</script>

</body>
</html>