[三流君] −−> [ASPで遊ぶ、失敗する] −−> [バックナンバー一覧]
−−> No.079 文字列9-30をExcelに移すと日付と誤解される・・・

文字列9-30をExcelに移すと日付と誤解される・・・



本文(発行内容)


<文字列9-30をExcelに移すと日付と誤解される・・・>

こんにちは、三流プログラマーのKen3です。 今回は、 9-30とかの文字列として扱いたいデータが、 ASPからExcelへ持って行くと、 親切にじゃなくって、勝手に2003/09/30と認識される、そんな話です。

/* * 1.今回のキッカケ */

メールで下記の質問をもらいました。 ------ >今回の質問はASPでデーターベースのデーターを検索しIEで表示したデーターを >excelに取り込んだところ,ある文字列のデーターの中に"97-12"や"03-2-17"など >が日付データー変わってしまたので,どうしたら,WEBの表示のままでexcelに >はいるでしょうか ------ ん? 何のことだろう? テストで、簡単なテーブルを表示してみる。 <html> <body> <table border=1> <tr><td>code</td><td>DATA</td></tr> <tr><td>b1</td><td>9-30</td></tr> <tr><td>d1</td><td>10-01</td></tr> <tr><td>f1</td><td>2003-10-02</td></tr> </table> </body> </html> これを、コピーして、Excelへ貼り付けると。 あらら・・・9-30が勝手に2003/09/30と認識されたよ。 形式を選択してで、文字列で貼り付けると。 b1 9-30とセルがつながるし、う〜ん。。。

/* * 2.Excelの名前を付けて保存でHTML形式にしてみたら・・・ */

<TD>9-30</TD> だと、Excelへコピーしたりすると、日付に変わってしまいます。 Web-->Excelで、いろいろと探してました。 けど、全然、わかりませんでした。(オイオイ) 少し、頭を切り替えて (なんて言いつつ、別のVBA系質問でシートのhtml保存で気が付いたんだけど) Excelでセルを文字型にして、 9-30,10-1,2003-10-02を入力して、 そのデータを名前を付けて保存の機能を使い、 htmlファイルに保存しました(htmlファイルを作成しました。) すると、 <td class=xl2217744>9-30</td> <td class=xl2217744>10-1</td> <td class=xl2217744>2003-10-02</td> と、TDタグでclassを定義しているみたいです。 このデータをコピーすると(このHTMLをコピーすると) 9-30のままで、Excelにコピーされます。 classを探らないといけませんが、 日付型の項目、これでかわせそうです。 下記、Excel君が名前を付けて保存で作成してくれた、HTMLファイルです。 ※意味不明だけど・・・ <html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta http-equiv=Content-Type content="text/html; charset=shift_jis"> <meta name=ProgId content=Excel.Sheet> <meta name=Generator content="Microsoft Excel 10"> <link rel=File-List href="Book1.files/filelist.xml"> <style id="Book1_17744_Styles"> <!--table {mso-displayed-decimal-separator:"\."; mso-displayed-thousand-separator:"\,";} .font517744 {color:windowtext; font-size:6.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:"MS Pゴシック", monospace; mso-font-charset:128;} .xl1517744 {padding-top:1px; padding-right:1px; padding-left:1px; mso-ignore:padding; color:windowtext; font-size:11.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:"MS Pゴシック", monospace; mso-font-charset:128; mso-number-format:General; text-align:general; vertical-align:middle; mso-background-source:auto; mso-pattern:auto; white-space:nowrap;} .xl2217744 {padding-top:1px; padding-right:1px; padding-left:1px; mso-ignore:padding; color:windowtext; font-size:11.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:"MS Pゴシック", monospace; mso-font-charset:128; mso-number-format:"\@"; text-align:general; vertical-align:middle; mso-background-source:auto; mso-pattern:auto; white-space:nowrap;} ruby {ruby-align:left;} rt {color:windowtext; font-size:6.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:"MS Pゴシック", monospace; mso-font-charset:128; mso-char-type:katakana;} --> </style> </head> <body> <!--[if !excel]>  <![endif]--> <!--以下の情報は、Excel の Web ページとして発行 ウィザードで生成されました。--> <!--同じアイテムが Excel から再発行されるとき、DIV タグ間のすべての情報が置き換えられます。--> <!-----------------------------> <!--Excel の Web ページとして発行 ウィザードのアウトプットの始まり--> <!-----------------------------> <div id="Book1_17744" align=center x:publishsource="Excel"> <table x:str border=0 cellpadding=0 cellspacing=0 width=165 style='border-collapse: collapse;table-layout:fixed;width:124pt'> <col width=72 style='width:54pt'> <col width=93 style='mso-width-source:userset;mso-width-alt:2976;width:70pt'> <tr height=18 style='height:13.5pt'> <td height=18 class=xl1517744 width=72 style='height:13.5pt;width:54pt'>CODE</td> <td class=xl2217744 width=93 style='width:70pt'>DATA</td> </tr> <tr height=18 style='height:13.5pt'> <td height=18 class=xl1517744 style='height:13.5pt'>b1</td> <td class=xl2217744>9-30</td> </tr> <tr height=18 style='height:13.5pt'> <td height=18 class=xl1517744 style='height:13.5pt'>d1</td> <td class=xl2217744>10-1</td> </tr> <tr height=18 style='height:13.5pt'> <td height=18 class=xl1517744 style='height:13.5pt'>f1</td> <td class=xl2217744>2003-10-02</td> </tr> <![if supportMisalignedColumns]> <tr height=0 style='display:none'> <td width=72 style='width:54pt'></td> <td width=93 style='width:70pt'></td> </tr> <![endif]> </table> </div> <!-----------------------------> <!--Excel の Web ページとして発行 ウィザードのアウトプットの終わり--> <!-----------------------------> </body> </html>

/* * 3.長いのヤダな、、style='mso-number-format:"\@";' を指定する */

でも、日付への変換を防ぐために、あんなに.Classを定義しないといけないの? う〜ん、あまり使いたくないですよね。 プロパティじゃないけど、 <td class=xl2217744>9-30</td> <td class=xl2217744>10-1</td> <td class=xl2217744>2003-10-02</td> のclass=xl2217744を見ると、 .xl2217744 {padding-top:1px; padding-right:1px; padding-left:1px; mso-ignore:padding; color:windowtext; font-size:11.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:"MS Pゴシック", monospace; mso-font-charset:128; mso-number-format:"\@"; text-align:general; vertical-align:middle; mso-background-source:auto; mso-pattern:auto; white-space:nowrap;} なんて感じで定義されています。 フォントとかイロイロとありますが、 怪しいのは、 mso-number-format:"\@"; これって、なんか怪しくないですか? HTMLのスタイルシートのページを見ていると、 クラスはスタイルの集まりみたいなことを書いてあります。 サンプルを見たりすると、 <TD style='XXXXXX'> なんて書いてある書き方も発見しました。 よし、チョット遊んでみるか?と思い、下記のHTMLを作成しました。 <html> <body> <table border=1> <tr><td>code</td><td>DATA</td></tr> <tr><td>a1</td><td style='mso-number-format:"\@";'>9-30</td></tr> <tr><td>b1</td><td>9-30</td></tr> <tr><td>c1</td><td style='mso-number-format:"\@";'>10-01</td></tr> <tr><td>d1</td><td>10-01</td></tr> </table> </body> </html> このデータをコピーすると、 style='mso-number-format:"\@";' を指定したデータは、日付に変換されなかったです。(おっ、これか?) 面倒だけど、Excelへ貼り付けるデータ で 日付が入りそうなデータを出力する場合は、 <td style='mso-number-format:"\@";'>10-01</td> みたいに、スタイルを指定するのも1つの方法かなぁ。

/* * 4.サンプルプログラム */

せっかくなので、ASPからの出力サンプルを作ってみます。 No.59 結果をExcelで返すならresponse.ContentType=application/vnd.ms-excel http://www.ken3.org/backno/backno_asp12.html#59 で、 ヘッダ情報にresponse.ContentType=application/vnd.ms-excelと入れると、 Excelファイルとして認識する、そんなことをやったので、 通常表示/Excelファイルの作成とパラメータで切り替えるサンプルを作ってみました http://www.ken3.org/cgi-bin/test/test079-1.asp で、普通に画面表示、 http://www.ken3.org/cgi-bin/test/test079-1.asp?MODE=Excel で、Excelファイルを作成します。 style='mso-number-format:"\@";'の有り無しを交互に作成しているので、 試してみてください。 ポイントは、"を表示させたかったので、""と下記のように書いてます response.write "<td style='mso-number-format:""\@"";'>9-30</td>" <%@ language=vbscript %> <% If Request.QueryString("MODE") = "Excel" Then 'Excel作成なら response.buffer = true 'タイプとヘッダがExcelですよ、、と教えてあげるため response.ContentType = "application/vnd.ms-excel" response.AddHeader "content-disposition", "inline; filename=dynamic.xls" Call OUT_DATA() 'データの出力ルーチンを呼ぶ response.flush response.end Else 'パラメータ無しなら response.write "<html><head><title>" response.write "style='mso-number-format:&quot;\@&quot;;'を指定してExcelへ" response.write "</title></head><body>" response.write "↓普通のデータ表示です<hr>" Call OUT_DATA() 'データの出力ルーチンを呼ぶ response.write "<hr>↑ここまで。<br>" response.write "<a href='test079-1.asp?MODE=Excel'>Excel形式の表示</a><br>" response.write "↑xlsファイルのダウンロード<br>" response.write "</body></html>" End If %> <% 'データの出力 テーブル形式でデータを出力します
Sub OUT_DATA
  response.write "<table Border=1>"
  response.write "<tr>"
  for i = 1 to 4
    response.write "<td width=80>"
    response.write i + i
    response.write "</td>"
  next
  response.write "<td width=80><b>=sum(A1:D1)</b></td>"
  response.write "</tr>"
  '日付と誤解されるデータの出力をチェックする
  response.write "<tr>"
  response.write "<td>9-30</td>" '9-30普通に表示されるけどExcelへ行くと
  ' style='mso-number-format:"\@";'を指定しました  ↓""は"1つの表現です
  response.write "<td style='mso-number-format:""\@"";'>9-30</td>"
  response.write "<td>2003-10-01</td>" '2003-10-01違いは感じられないが・・・
  response.write "<td style='mso-number-format:""\@"";'>2003-10-01</td>"
  response.write "<td>←Excelにすると・・・</td>"
  response.write "</tr>"
  response.write "</table>"
End Sub
%>

/* * 4.終わりの挨拶 </HTML> */

今回は、9-30が変換されてしまう問題を、 style='mso-number-format:"\@";' と指定をして、かわしてみました。 違う方法があるかもしれないけど、 何かの参考となれば幸いです。 ASP、VBScript勉強中の三流プログラマーのKen3でした。


ページフッター

ここまで、読んでいただきどうもです。目的の情報が見つかったか?少々心配しつつ、、、

三流君へ メッセージを送る

感想や質問・要望・苦情など 三流君へメッセージを送る。
返信例 XXXXさんへ
下記のフォームからメッセージを送ることができます。


あなたのお名前(ニックネーム):さん
返信は?:

アドレス:に返事をもらいたい
感想や質問↓:


(感想や質問・要望・苦情はHPで記事に載せることがあります。)

種類別のリンク や 広告など

[三流君(TOP ken3.org へ戻る)] / [ASPで遊ぶ、失敗する] / [ASP記事 バックナンバー目次]


Blogとリンク:[三流君のMemo別館]/ [ASP 三流君のソースコード置き場]/ [Ken3Video YouTubeで動画解説]
広告:

気になった ジャンル ↓を選択してください。

まぁ、基本はデータの受け取りかなぁ。
・[Form等を使用したデータのやり取り]・・・ASPと言っても、HTMLの入力フォームからデータを受け取ります。POSTやGETでやりとりを押さえますか。

次は、データの入出力 で ADOを使った(ADOで接続) と SQLの解説を少々
・[ADOでMdbファイルを使う]・・・MDBと接続して、簡単な追加・更新・削除を行った。
・[ADOでExcelと接続してみた]・・・.xlsと接続してSQLを使ってみた。
・[ADOでCSVと接続してみた]・・・.CSV テキストを読み出した。※更新・削除はできません

広告:



DBが使えるので、あまり使用しないけど、普通のテキストファイル処理
・[テキストファイル処理]・・・ファイルを開いて、書き込む。1行読み込みなどを軽く

VBScriptでFormat関数が無いなど、微妙にVBAと違うけど
[VBScript関数関係の説明]・・・少し、処理を書いてみた。
[その他処理サンプル]・・・あまり良いサンプル作れなかったけど。。。
何かの参考となれば幸いです。



[三流君(TOP ken3.org へ戻る)] / [ASPで遊ぶ、失敗する] / [ASP記事 バックナンバー目次]


広告: