CSVãExcelãã¡ã¤ã«ã«å¤æããã¹ã¯ãªãããï¼JScript/WSHã§ï¼ã«ã³ãåºåãCSVãã¡ã¤ã«ãã.xlsãçæï¼
JScript/WSHã§ï¼CSVã.xlsãã¡ã¤ã«ã«å¤æããã¹ã¯ãªããã
å®ã¯ï¼Excelã«ã¯è´å½çãªãã°ï¼è´å½çãªä»æ§ï¼ãããï¼CSVã®æ±ããã²ã©ãã
ã©ãã»ã©å·¥å¤«ãã¦ãï¼CSVãExcelä¸ã§éãã¨ï¼
- åããããã
- ã»ã«ã®è¡¨ç¤ºå½¢å¼ã楽ã«å¶å¾¡ã§ããï¼å¤ãå¤ãã£ãã
- ã»ã«ä¸ã®æ°å¼ã¨ãã¦ã="ã"ãã¨æ¸ãããï¼æ°å¼ã¨ãã¦è§£éãããããããªãã£ããã»ã»ã»ã»
ã¨ããå ·åã«ï¼Excelã¯CSVå¦çæã«èª¤èªèãå¤ãããã®ã ã
ããã§ã¯ï¼ä»äºã«ãªããªãã
ãªã®ã§ä»£ããã«ï¼
- csvããã®ã¾ã¾Excelä¸ã§éãã®ã§ã¯ãªãï¼
- å¥ã®æ段ã«ãã£ã¦csvãxlsã«å¤æãï¼ããããExcelä¸ã§éã
ã¨ããã°ããã
ããã¯ç¢ºå®ãªæ¹æ³ã ã
ä¸è¨ã«æ²è¼ããã³ã¼ããï¼csv2xls.js ã¨ãã¦ä¿åã
ããã¦ã³ãã³ãã©ã¤ã³ããï¼
cscript csv2xls.js old.csv new.xls
ã®ããã«å®è¡ã
ï¼â»xlsã®ã»ãã¯çµ¶å¯¾ãã¹ã§æ¸ããªãã¨ï¼ãã¤ããã¥ã¡ã³ãå
ã«ãã¡ã¤ã«ãçæããããï¼
/* CSVãExcelã«å¤æããã¹ã¯ãªãã CSVã®åææ¡ä»¶: ã»SJIS ã»ã«ã³ãåºåã ã»""ã«ãããããç¡ã ã»ãã¼ã¿ä¸ã®ã«ã³ãã¯\ã§ã¨ã¹ã±ã¼ãããã¦ãã 使ãæ¹ cscript csv2xls.js old.csv new.xls */ // -------------------- ã¡ã¤ã³å¦ç -------------------- // å¼æ°åå¾ if( WScript.Arguments.length == 0 ) { log("å¼æ°ãããã¾ããã"); WScript.Quit(); } var csv_path = WScript.Arguments.Unnamed(0); var xls_path = WScript.Arguments.Unnamed(1); // CSVãèªã¿åã var arr_data = get_csv_data( csv_path ); // Excelã«æ¸ãè¾¼ã make_excel( arr_data, xls_path ); log("çµäºãã¾ããã"); // -------------------- é¢æ° -------------------- // CSVãã¡ã¤ã«ä¸ã®ãã¼ã¿ã2次å é åã§è¿ãã¾ã function get_csv_data( csv_path ) { var arr_data = []; var fso = WScript.CreateObject("Scripting.FileSystemObject"); var txt_r = fso.OpenTextFile( csv_path, 1 ); var line = ""; var arr_line_data; while( ! txt_r.AtEndOfStream ) { line = txt_r.ReadLine(); // ãã®è¡ã解æ arr_line_data = parse_csv_line( line ); arr_data.push( arr_line_data ); } txt_r.Close(); fso = null; return arr_data; } // CSVå½¢å¼ã®1è¡ã®æååãåãåãï¼é åã¨ãã¦è¿ãã¾ã function parse_csv_line( line ) { var arr_line_data = []; log( "ã" + line + "ãã解æãã¾ãã" ); // å é ãã1æåã㤠var index = 0; // ç¾å¨æãã¦ããç®æ var ch = ""; // ç¾å¨æãã¦ããæå var csv_value_length = 0; // åãåºããã¨ãã¦ãããã¼ã¿ã®é·ã var csv_value = ""; // åãåºããã1ã»ã«ã®ãã¼ã¿ var last_comma_index = -1; // ååã«ã³ãããã£ãç®æ var is_escaped = false; // ç¾å¨ã¨ã¹ã±ã¼ãä¸ãã©ã° var len = line.length; while( true ) { if( index >= len ) { // æ«å°¾ã«æ¥ã // 空è¡ã§ãªãã£ããªãã°ï¼è¡æ«ããã¼ã¿åºåãã¨ã¿ãªã if( index > 0 ) { // ç»é² record_to_arr( arr_line_data, csv_value ); } break; } ch = line.charAt( index ); log( index + " æåç®ã¯ " + ch ); // æå種ã«ãã£ã¦åå² if( // CSVã®æå³ä¸ã®ã«ã³ããæ¥ãå ´å ( ! is_escaped ) && ( ch == "," ) ) { // ç»é² record_to_arr( arr_line_data, csv_value ); last_comma_index = index; is_escaped = false; csv_value = ""; } else if( ( ! is_escaped ) && ( ch == "\\" ) ) { // ã¨ã¹ã±ã¼ãç¶æ ãè¨å®ãã is_escaped = true; log("\\ã«ããã¨ã¹ã±ã¼ããçºç"); } else { // \,以å¤ã®ã¨ã¹ã±ã¼ãã¯ééããã if( is_escaped && ! ( ch == "," ) ) { csv_value += "\\"; } csv_value += ch; is_escaped = false; } // 次㸠index ++; } return arr_line_data; } // CSVã®1ãã¼ã¿ãExcelç¨ã«å å·¥ãï¼é åã«ç»é²ãã¾ãã function record_to_arr( arr_line_data, csv_value ) { var new_value = csv_value; // TODO:CSVä¸ã®ãã¼ã¿åå¾æã«å å·¥ãè¡ãªãå ´åã¯ï¼ããã§new_valueãå¤æ´ã log( "ã" + csv_value + "ããç»é² " // + "(Excelä¸ã§ã¯ã" + new_value + "ã)" ); arr_line_data.push( new_value ); } // 2次å é åãåãåãï¼Excelã«æ¸ãåºãã¾ã function make_excel( arr_data, xls_path ) { // Excelãç«ã¡ä¸ãã var excel = WScript.CreateObject("Excel.Application"); excel.Visible = true; // æ°è¦ã¯ã¼ã¯ããã¯ãä½æ(æ¢åã®ç©ã¯ç¡è¦) excel.Workbooks.Add(); var book = excel.Workbooks( excel.Workbooks.Count ); // ã¯ã¼ã¯ããã¯ãåå¾ // ããã¯ãç·¨é var sheet = book.WorkSheets("Sheet1"); // ã·ã¼ããåå¾ var data_height = arr_data.length; // ãã¼ã¿è¡æ° var data_width = 0; if( data_height > 0 ) { data_width = arr_data[0].length; // ãã¼ã¿åæ° } var csv_value; for( var i = 0; i < data_height; ++i ) { for( var j = 0; j < data_width; ++j ) { csv_value = arr_data[ i ][ j ]; sheet.Cells( i + 1, j + 1 ).Value = "'" + csv_value; // æååã¨ãã¦ã»ã«ã«æ¸ã込㿠} } // ããã¯ãä¿å excel.DisplayAlerts = false; // æ¢åãã¡ã¤ã«ããã£ã¦ãä¸æ¸ããã book.SaveAs( xls_path ); // Excelãéãã¦çµäº excel.Quit(); excel = null; } // ãã°åºå function log( str ) { WScript.Echo( str ); }
æ¦èª¬ï¼
- parse_csv_line() ä¸ã®whileæã§ï¼CSVã®ä¸ã¤ã®è¡ã解æãã¦ããã
- ãããå¤ããã°ï¼""ã§ãããããï¼enclosed by \" ã§ãããããªï¼CSVãã¼ã¿ãåãæ±ãå¯è½ã
使ç¨ä¾
ä¸è¨ãï¼a.csvã¨ãã¦ä¿åã
1,2,3,4,5,6,7 , ,aaa,,,', ,hello\,world,,01,2009/01/01,,
ããã¦ã³ãã³ãã©ã¤ã³ãã
cscript csv2xls.js a.csv b.xls
ã¨å®è¡ã
ã³ãã³ãããã³ããä¸ã«ã¯ï¼ä¸è¨ã®ããã«è§£æç¶æ³ã表示ãããã
D:\temp>cscript csv2xls.js a.csv d:\temp\b.xls Microsoft (R) Windows Script Host Version 5.7 Copyright (C) Microsoft Corporation 1996-2001. All rights reserved. ã1,2,3,4,5,6,7ãã解æãã¾ãã 0 æåç®ã¯ 1 1 æåç®ã¯ , ã1ããç»é² 2 æåç®ã¯ 2 3 æåç®ã¯ , ã2ããç»é² 4 æåç®ã¯ 3 5 æåç®ã¯ , ã3ããç»é² 6 æåç®ã¯ 4 7 æåç®ã¯ , ã4ããç»é² 8 æåç®ã¯ 5 9 æåç®ã¯ , ã5ããç»é² 10 æåç®ã¯ 6 11 æåç®ã¯ , ã6ããç»é² 12 æåç®ã¯ 7 ã7ããç»é² ã, ,aaa,,,',ãã解æãã¾ãã 0 æåç®ã¯ , ãããç»é² 1 æåç®ã¯ 2 æåç®ã¯ , ã ããç»é² 3 æåç®ã¯ a 4 æåç®ã¯ a 5 æåç®ã¯ a 6 æåç®ã¯ , ãaaaããç»é² 7 æåç®ã¯ , ãããç»é² 8 æåç®ã¯ , ãããç»é² 9 æåç®ã¯ ' 10 æåç®ã¯ , ã'ããç»é² ãããç»é² ã,hello\,world,,01,2009/01/01,,ãã解æãã¾ãã 0 æåç®ã¯ , ãããç»é² 1 æåç®ã¯ h 2 æåç®ã¯ e 3 æåç®ã¯ l 4 æåç®ã¯ l 5 æåç®ã¯ o 6 æåç®ã¯ \ \ã«ããã¨ã¹ã±ã¼ããçºç 7 æåç®ã¯ , 8 æåç®ã¯ w 9 æåç®ã¯ o 10 æåç®ã¯ r 11 æåç®ã¯ l 12 æåç®ã¯ d 13 æåç®ã¯ , ãhello,worldããç»é² 14 æåç®ã¯ , ãããç»é² 15 æåç®ã¯ 0 16 æåç®ã¯ 1 17 æåç®ã¯ , ã01ããç»é² 18 æåç®ã¯ 2 19 æåç®ã¯ 0 20 æåç®ã¯ 0 21 æåç®ã¯ 9 22 æåç®ã¯ / 23 æåç®ã¯ 0 24 æåç®ã¯ 1 25 æåç®ã¯ / 26 æåç®ã¯ 0 27 æåç®ã¯ 1 28 æåç®ã¯ , ã2009/01/01ããç»é² 29 æåç®ã¯ , ãããç»é² ãããç»é² çµäºãã¾ããã
ããã¨ï¼\My Documents\b.xlsãçæãããã
ããCSVã巨大ãªããã«ã³ãã³ãããã³ããä¸ã§ã®å¦çãéãï¼ã¨æãããï¼æå¾ã®log()å
ã®å¦çãã³ã¡ã³ãã¢ã¦ãããã¨ããã
è£è¶³
DBã®ãã¼ã¿ãCSVã§ã¨ã¯ã¹ãã¼ããï¼ãããExcelä¸ã§ç·¨éãããï¼ã¨ããæãªã©ã«éå®ããã
ãªãï¼ä»åã®ããããé©ç¨å¯è½ãªå½¢å¼ã®CSVã¨ãã¦ãã¼ãã«ãã¨ã¯ã¹ãã¼ãããããã®SQLã¯ä¸è¨ã
postgres
COPY tablename TO '/tmp/hoge.out' WITH DELIMITER AS ',' NULL AS '';
mysql
SELECT * FROM tablename INTO OUTFILE '/tmp/hoge.out' FIELDS TERMINATED BY ',' LINES TERMINATER BY '\r\n';
ãã ãMySQLã§ã¯ï¼ã»ã«å ã®æ¹è¡ããè¡æ«ã®\ãã¨ãªã£ã¦åºåãéä¸ã§æ¹è¡ããã¦ãã¾ãã®ã§ï¼å¥éãããã§\r\nã«æ¸ãæããå¿ è¦ãããã
ã