3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CSVファイルの内容を表示するHTAアプリケーション

Last updated at Posted at 2020-08-11

※作成中

#ソース

CSVファイル表示.hta
<html>
<head>
  <title>Title</title>

    <hta:application
      id="myApp"
      applicationname="myApp"
      border="thick"
      borderstyle="normal"
      caption="yes"
      icon=""
      maximizebutton="yes"
      minimizebutton="yes"
      showintaskbar="yes"
      singleinstance="no"
      sysmenu="yes"
      version="1.0"
      windowstate="normal"/>

  <script type="text/javascript">

    var fso = new ActiveXObject("Scripting.FileSystemObject"),
        thisScriptPath = myApp.commandLine.replace(/^"|"$/g, ""),
        folderPath = fso.GetParentFolderName(thisScriptPath),
        fileName = "data.csv";

    function init() {

        var con = null, rs = null;

        try {
            con = openConnection(folderPath);
            rs = con.Execute("select * from [" + fileName + "]");
            writeCsvData(rs);
        } catch (e) {
            alert(e.description);
            // throw e;
        } finally {
            if (rs != null) {
                rs.Close();
            }
            if (con != null) {
                con.Close();
            }
        }
    }

    function openConnection(folderPath) {
        var con = new ActiveXObject("ADODB.Connection");
        con.Provider = "Microsoft.Jet.OLEDB.4.0";
        con.Properties("Extended Properties") = "Text;HDR=Yes;FMT=Delimited";
        con.CursorLocation = 3;
        con.Open(folderPath);
        return con;
    }

    function writeCsvData(rs) {

        document.write("<table border=1>");

        // ヘッダー行
        document.write("<tr>");
        for (var f = 0; f < rs.Fields.Count; f++) {
            document.write("<td>");
            document.write(escapeHtml(rs.Fields(f).Name));
            document.write("</td>");
        }
        document.write("</tr>");

        // データ行
        while (!rs.EOF) {
            document.write("<tr>");
            for (var f = 0; f < rs.Fields.Count; f++) {
                document.write("<td>");
                document.write(escapeHtml(rs.Fields(f).Value));
                document.write("</td>");
            }
            document.write("</tr>");
            rs.MoveNext();
        }

        document.write("</table>");
    }

    function escapeHtml(str) {
        // ここでエスケープ処理を入れる予定
        return str;
    }

  </script>
</head>
<body onload="init()">
</body>
</html>

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?