C#ã§ã¨ã¯ã»ã«ãã¡ã¤ã«ãæ±ãæ¹æ³ã®å¿åé²
ã¯ããã«
ä¼ç¤¾ã§ã¨ããã¨ã¯ã»ã«ã·ã¼ãã®å 容ãèªã¿è¾¼ã¿ãä»ã®ãã¼ã¿ã¨æ¯ã¹ã¦æ°ãããªã¹ããä½æããå¿ è¦ããã£ãã®ã§ããã®ããã°ã©ã ãC#ã§ä½ãã¾ãããï¼4000åè¿ãã®ãã¼ã¿ãå¥ã®ã·ã¼ãããæãåºã並ã¹ããã®ãªã®ã§ã¨ã¦ãæããã§ããªãã¨ï¼
VBSã使ç¨ãããã¯ããçµãã§ãè¯ãã£ããã¨æãã¾ãããVBSã«ã¤ãã¦ã¯ããããããªãã®ã§ãä»åã¯C#ã§ããã°ã©ã ãä½æãã¾ããã
ãã®æã®å¿åé²ã¨ãã¦C#ã«ããã¨ã¯ã»ã«ã·ã¼ãä¸ã®ãã¼ã¿ã®åå¾æ¹æ³ã«ã¤ãã¦æ¸ãã¦ãã¾ãã使ç¨ããC#ã¯VisualStudio2008ã«ä»ãã¦ãããã®ã§ãã
åç §ã®è¿½å
ã¢ããªã±ã¼ã·ã§ã³ããã¨ã¯ã»ã«ã«ã¢ã¯ã»ã¹ããã«ã¯ãã¨ã¯ã»ã«ãå ¬éããCOMã³ã³ãã¼ãã³ãã¸ã®åç §ãããã¸ã§ã¯ãã«è¿½å ããå¿ è¦ãããã¾ãã
ããã«ã¯ï¼»åç §ã®è¿½å ï¼½ã¦ã£ã³ãã¦ã§ï¼»COMï¼½ã¿ããé¸æãããMicrosoft Excel 12.0 Object Libraryãã追å ãã¾ãã
ããã«ãããã½ãªã¥ã¼ã·ã§ã³ã»ã¨ã¯ã¹ããã¼ã©ã®ãåç §è¨å®ãã®ããªã¼ã«ãMicrosoft.Office.Coreãã¨ãMicrosoft.Office.Interop.Excelãã追å ããã¾ãã
使ç¨ããã¨ã¯ã»ã«ã®ãã¼ã¸ã§ã³ã«ããé¸ã¶ã©ã¤ãã©ãªãéãããã§ãã®ã§ã注æãã¦ãã ããã
åå空éã®ã¤ã³ãã¼ã
追å ããã©ã¤ãã©ãªã«ããã¨ã¯ã»ã«ã¸ã®ã¢ã¯ã»ã¹ãè¡ãããã«åå空éã®ã¤ã³ãã¼ããå¿ è¦ã«ãªãã¾ãã
ã½ã¼ã¹ã®ä¸ã®æ¹ã«ä»¥ä¸ã®ã³ã¼ããè¨å ¥ãã¾ãã
using Excel = Microsoft.Office.Interop.Excel;
ãã¡ã¤ã«ã®ãªã¼ãã³
使ç¨ããã¨ã¯ã»ã«ãã¡ã¤ã«ã®ãªã¼ãã³ãè¡ãã¾ãããªã¼ãã³ã«ã¯ã¨ã¯ã»ã«ãªãã¸ã§ã¯ããã¯ã¼ã¯ããã¯ãªãã¸ã§ã¯ããæå®ãã¦ãªã¼ãã³ãã¾ãã
ãªã¼ãã³ã®éã«ã¯é©å½ãªããããã£ãè¨å®ãã¾ãã
string excelName = "C:\\sample.xls"; Excel.Application oXls; //ã¨ã¯ã»ã«ãªãã¸ã§ã¯ã Excel.Workbook oWBook; //ã¯ã¼ã¯ããã¯ãªãã¸ã§ã¯ã oXls = new Excel.Application(); oXls.Visible = true; //確èªã®ããã¨ã¯ã»ã«ã®ã¦ã£ã³ãã¦ã表示ãã //ã¨ã¯ã»ã«ãã¡ã¤ã«ããªã¼ãã³ãã oWBook = (Excel.Workbook)(oXls.Workbooks.Open( excelName, // ãªã¼ãã³ããExcelãã¡ã¤ã«å Type.Missing, // ï¼çç¥å¯è½ï¼UpdateLinks (0 / 1 / 2 / 3) Type.Missing, // ï¼çç¥å¯è½ï¼ReadOnly (True / False ) Type.Missing, // ï¼çç¥å¯è½ï¼Format // 1:ã¿ã / 2:ã«ã³ã (,) / 3:ã¹ãã¼ã¹ / 4:ã»ãã³ãã³ (;) // 5:ãªã / 6:å¼æ° Delimiterã§æå®ãããæå Type.Missing, // ï¼çç¥å¯è½ï¼Password Type.Missing, // ï¼çç¥å¯è½ï¼WriteResPassword Type.Missing, // ï¼çç¥å¯è½ï¼IgnoreReadOnlyRecommended Type.Missing, // ï¼çç¥å¯è½ï¼Origin Type.Missing, // ï¼çç¥å¯è½ï¼Delimiter Type.Missing, // ï¼çç¥å¯è½ï¼Editable Type.Missing, // ï¼çç¥å¯è½ï¼Notify Type.Missing, // ï¼çç¥å¯è½ï¼Converter Type.Missing, // ï¼çç¥å¯è½ï¼AddToMru Type.Missing, // ï¼çç¥å¯è½ï¼Local Type.Missing // ï¼çç¥å¯è½ï¼CorruptLoad ));
ã¯ã¼ã¯ã·ã¼ãã®é¸æ
使ç¨ããã¯ã¼ã¯ã·ã¼ããé¸æãã¾ããã¯ã¼ã¯ã·ã¼ãã¯ã¤ã³ããã¯ã¹ã«ãã£ã¦æå®ãã¾ãã®ã§ãã¯ã¼ã¯ã·ã¼ãåããã¤ã³ããã¯ã¹ãåå¾ãã¦è©²å½ããã¯ã¼ã¯ã·ã¼ããªãã¸ã§ã¯ããåå¾ãã¾ãã
// æå®ãããã¯ã¼ã¯ã·ã¼ãåã®ã¤ã³ããã¯ã¹ãè¿ãã¡ã½ãã private int getSheetIndex(string sheetName, Excel.Sheets shs) { int i = 0; foreach (Excel.Worksheet sh in shs) { if (sheetName == sh.Name) { return i + 1; } i += 1; } return 0; } // ä¸ããããã¯ã¼ã¯ã·ã¼ãåãããã¯ã¼ã¯ã·ã¼ããªãã¸ã§ã¯ããå¾ã string sheetName = "Sheet2"; Excel.Worksheet oSheet; // ã¯ã¼ã¯ã·ã¼ããªãã¸ã§ã¯ã oSheet = (Excel.Worksheet)oWBook.Sheets[getSheetIndex(sheetName, oWBook.Sheets)];
ã»ã«ã®å 容ãèªã¿è¾¼ã
ã¯ã¼ã¯ã·ã¼ããªãã¸ã§ã¯ãããç®çã¨ããã»ã«ã®å 容ãèªã¿è¾¼ã¿ã¾ãã
ã»ã«ã§ç¸¦æ¹åã«ä¸¦ã¶ã®ãåããã«ã©ã ï¼Columnï¼ã§æ¨ªæ¹åã«ä¸¦ã¶ãè¡ãã¯ãã¦ï¼Rowï¼ã§ãã
ã»ã«ã¯ã¯ã¼ã¯ã·ã¼ããªãã¸ã§ã¯ãã®Cells[row, col]ã«ããå¼ã³åºãã¾ãã
ã¡ãªã¿ã«ãã«ã©ã ããã¦å ±ã«(0,0)ããã§ã¯ãªã(1,1)ããå§ã¾ãã¾ãã
string sCellVal; Excel.Range rng; // Rangeãªãã¸ã§ã¯ã rng = (Excel.Range)oSheet.Cells[1, 1]; sCellVal = rng.Text.ToString(); // A1ã»ã«ã®å 容
çµäºå¦ç
æå¾ã«çµäºå¦çã§ããã¯ã¼ã¯ããã¯ãªãã¸ã§ã¯ããéãã¦çµäºã§ãã
oWBook.Close(Type.Missing, Type.Missing, Type.Missing); oXls.Quit();
æå¾ã«
ãããªãããã§C#ããã¨ã¯ã»ã«ã·ã¼ãã«ã¤ãã¦ã¢ã¯ã»ã¹ãããã¨ãã§ãã¾ãã
ã¾ããCSVå½¢å¼ã«ãã¦ãã ã®ãã¡ã¤ã«ã¨ãã¦æ±ã£ãã»ãã楽ãªã®ããããã¾ããããä»åã¯ã¨ã¯ã»ã«ã¨ãã¦æ±ã£ã¦ã¿ã¾ããã
- ä½è : ãã¼ãã¼ãã»ã·ã«ã,ã¨ãã£ãã£ã¹ãã©ã¼ãã³ã°æ ªå¼ä¼ç¤¾ç¢å¶è¡
- åºç社/ã¡ã¼ã«ã¼: ç¿æ³³ç¤¾
- çºå£²æ¥: 2010/12/03
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 5人 ã¯ãªãã¯: 55å
- ãã®ååãå«ãããã° (6件) ãè¦ã
ããã°ã©ãã³ã°C# 第6ç
- ä½è : Ian Griffiths,Matthew Adams,Jesse Liberty,é´æ¨å¹¸æ,é¦è¤ä¸å¹¸,æ ªå¼ä¼ç¤¾æ å ±æç
- åºç社/ã¡ã¼ã«ã¼: ãªã©ã¤ãªã¼ã¸ã£ãã³
- çºå£²æ¥: 2011/11/29
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 2人 ã¯ãªãã¯: 14å
- ãã®ååãå«ãããã° (11件) ãè¦ã
ç«ã§ããããC#ããã°ã©ãã³ã° 第2ç (ç«ã§ããããããã°ã©ãã³ã°)
- ä½è : ç²äºåº·å
- åºç社/ã¡ã¼ã«ã¼: SBã¯ãªã¨ã¤ãã£ã
- çºå£²æ¥: 2010/08/28
- ã¡ãã£ã¢: åè¡æ¬
- è³¼å ¥: 1人 ã¯ãªãã¯: 20å
- ãã®ååãå«ãããã° (6件) ãè¦ã