2009年07月09日


HTA : ADO で CSV 出力

サンプルはOracle ですが、接続文字列の書式を変更すると
接続可能な全てのデータベースの処理を行う事ができます

最新ソースコードです(接続してテーブル一覧を取得します)

↓古い一つ前のソースコードです
<SCRIPT language="VBScript">

' **********************************************************
' オブジェクト作成
' **********************************************************
Set Cn = CreateObject( "ADODB.Connection" )
Set Rs = CreateObject( "ADODB.Recordset" )
Set Fs = CreateObject( "Scripting.FileSystemObject" )

Function OraAction()

	strDriver = "{Microsoft ODBC for Oracle}"
	strServer = document.getElementById("p03").value
	strUser = document.getElementById("p01").value
	strPass =  document.getElementById("p02").value
	strTable =  document.getElementById("p04").value

	ConnectionString = _
		"Provider=MSDASQL" & _
		";Driver=" & strDriver & _
		";Server=" & strServer & _
		";UID=" & strUser & _ 
		";PWD=" & strPass & _ 
		";" 

	if vbCancel = MsgBox(ConnectionString, vbOkCancel or vbDefaultButton2, "CSV出力") then
		Exit Function
	end if

	' **********************************************************
	' 接続
	' **********************************************************
	on error resume next
	Cn.Open ConnectionString
	if Err.Number <> 0 then
		alert( Err.Description )
		Exit Function
	end if
	on error goto 0

	' **********************************************************
	' レコードセット取得
	' **********************************************************
	Query = "select * from " & strTable
	on error resume next
	Rs.Open Query, Cn
	if Err.Number <> 0 then
		Cn.Close
		alert( Err.Description )
		Exit Function
	end if
	on error goto 0

	' **********************************************************
	' 出力ファイルオープン
	' **********************************************************
	Set Csv = Fs.CreateTextFile( strTable & ".csv", True )

	' **********************************************************
	' タイトル出力
	' **********************************************************
	Buffer = ""
	For i = 0 to Rs.Fields.Count - 1
		if Buffer <> "" then
			Buffer = Buffer & ","
		end if
		Buffer = Buffer & Rs.Fields(i).Name
	Next
	Csv.WriteLine Buffer

	' **********************************************************
	' データ出力
	' **********************************************************
	Do While not Rs.EOF
		Buffer = ""
		For i = 0 to Rs.Fields.Count - 1
			if Buffer <> "" then
				Buffer = Buffer & ","
			end if
			Buffer = Buffer & Rs.Fields(i).Value
		Next
		Csv.WriteLine Buffer
		Rs.MoveNext
	Loop

	' **********************************************************
	' ファイルクローズ
	' **********************************************************
	Csv.Close

	' **********************************************************
	' レコードセットクローズ
	' **********************************************************
	Rs.Close

	' **********************************************************
	' 接続解除
	' **********************************************************
	Cn.Close

	MsgBox("処理が終了しました")

End Function

</SCRIPT>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=shift_jis" />
<title>CSV出力</title>
<STYLE>
 * {
	font-size:14px;
}
PRE {
	font-weight: bold;
}
</STYLE>
</head>
<body>

<table>
<tr>
	<td>ユーザ</td>
	<td><INPUT type="text" id="p01" value="lightbox"></td>
</tr>
<tr>
	<td>パスワード</td>
	<td><INPUT type="text" id="p02" value="lightbox"></td>
</tr>
<tr>
	<td>サーバー</td>
	<td><INPUT type="text" id="p03" value="pcname/orcl"></td>
</tr>
<tr>
	<td>テーブル</td>
	<td><INPUT type="text" id="p04" value=""></td>
</tr>
</table>

<INPUT type="button" value="実行" onClick='Call OraAction()'>

</body>
</html>

<SCRIPT for=window event=onload language="VBScript">

	nWidth = 800
	nHeight = 600
	top.resizeTo nWidth, nHeight
	top.moveTo (screen.width-nWidth)/2, (screen.height-nHeight)/2

</SCRIPT>

■ 関連する記事
Oracle 10g : System.Data.Odbc( Framework ) : Microsoft Driver
VBScript + MySQL : CSV 出力プログラム


タグ:Oracle ADO CSV
posted by at 17:00 | 自作 | このブログの読者になる | 更新情報をチェックする