±â¼ú ÀÚ·á: 302096 - ¸¶Áö¸· °ËÅä: 2007³â 1¿ù 17ÀÏ ¼ö¿äÀÏ - ¼öÁ¤: 8.3

Visual C#¿¡¼­ ExcelÀ» ÀÚµ¿È­ÇÏ¿© ¹è¿­À» ÅëÇØ ÀÏÁ¤ ¹üÀ§ÀÇ µ¥ÀÌÅ͸¦ ä¿ì°Å³ª °¡Á®¿À´Â ¹æ¹ý

½Ã½ºÅÛ ÆÁº» ¹®¼­ÀÇ ³»¿ëÀº ±ÍÇϰ¡ »ç¿ëÇÏ´Â ¿î¿µ üÁ¦¿Í ´Ù¸¥ ¿î¿µ üÁ¦¿¡ ÇØ´çÇÕ´Ï´Ù. ¹®¼­ ³»¿ë Áß ±ÍÇÏ¿Í °ü·Ã ¾ø´Â ºÎºÐÀº Ç¥½ÃµÇÁö ¾Ê½À´Ï´Ù.
ÀÌ ¹®¼­´Â ÀÌÀü¿¡ ´ÙÀ½ ID·Î ÃâÆÇµÇ¾úÀ½: KR302096
¸ðµÎ È®´ë | ¸ðµÎ Ãà¼Ò

¿ä¾à

ÀÌ ¹®¼­¿¡¼­´Â Microsoft Visual C# 2005 ¶Ç´Â Microsoft Visual C# .NET¿¡¼­ Microsoft ExcelÀ» ÀÚµ¿È­ÇÏ¿© ¹è¿­À» ÅëÇØ ¿©·¯ ¼¿ ¹üÀ§ÀÇ °ªÀ» ä¿ì°í °¡Á®¿À´Â ¹æ¹ý¿¡ ´ëÇØ ¼³¸íÇÕ´Ï´Ù.

Ãß°¡ Á¤º¸

ÇÑ ¹ø¿¡ Çϳª¾¿ ¼¿À» ä¿ìÁö ¾Ê°í ¿©·¯ ¼¿ ¹üÀ§¸¦ ä¿ì·Á¸é Range °³Ã¼ÀÇ Value ¼Ó¼ºÀ» 2Â÷¿ø ¹è¿­·Î ¼³Á¤ÇÏ¸é µË´Ï´Ù. ¸¶Âù°¡Áö·Î Value ¼Ó¼ºÀ» »ç¿ëÇÏ¿© ÇÑ ¹ø¿¡ ¿©·¯ ¼¿¿¡ ´ëÇÑ 2Â÷¿ø ¹è¿­ °ªÀ» °¡Á®¿Ã ¼ö ÀÖ½À´Ï´Ù. ´ÙÀ½ ´Ü°è¿¡¼­´Â 2Â÷¿ø ¹è¿­À» »ç¿ëÇÏ¿© µ¥ÀÌÅ͸¦ ¼³Á¤ÇÏ°í °Ë»öÇÏ´Â °úÁ¤À» º¸¿© ÁÝ´Ï´Ù.

Microsoft Excel¿ë ÀÚµ¿È­ Ŭ¶óÀÌ¾ðÆ® ºôµå

  1. Microsoft Visual Studio 2005 ¶Ç´Â Microsoft Visual Studio .NET¸¦ ½ÃÀÛÇÕ´Ï´Ù.
  2. ÆÄÀÏ ¸Þ´º¿¡¼­ »õ·Î ¸¸µé±â¸¦ ´©¸£°í ÇÁ·ÎÁ§Æ®¸¦ ´©¸¨´Ï´Ù. Visual C# ÇÁ·ÎÁ§Æ® Çü½Ä¿¡¼­ Windows ÀÀ¿ë ÇÁ·Î±×·¥À» ¼±ÅÃÇÕ´Ï´Ù. ±âº»ÀûÀ¸·Î Form1ÀÌ ¸¸µé¾îÁý´Ï´Ù.
  3. Visual Studio 2005ÀÇ °æ¿ì Microsoft Excel 11.0 Object Library¿¡ ´ëÇÑ ÂüÁ¶³ª, Visual Studio .NETÀÇ °æ¿ì Microsoft Excel Object Library¿¡ ´ëÇÑ ÂüÁ¶¸¦ Ãß°¡ÇÕ´Ï´Ù. ÇØ´ç ÂüÁ¶¸¦ Ãß°¡ÇÏ·Á¸é ´ÙÀ½°ú °°ÀÌ ÇϽʽÿÀ.
    1. ÇÁ·ÎÁ§Æ® ¸Þ´º¿¡¼­ ÂüÁ¶ Ãß°¡¸¦ ´©¸¨´Ï´Ù.
    2. COM ÅÇ¿¡¼­ Microsoft Excel Object Library¸¦ ã¾Æ ¼±ÅÃÀ» ´©¸¨´Ï´Ù.

      Visual Studio 2005ÀÇ °æ¿ì COM ÅÇ¿¡¼­ Microsoft Excel 11.0 Object Library¸¦ ã½À´Ï´Ù.

      Âü°í Microsoft Office 2003¿¡´Â PIA(±âº» Interop ¾î¼Àºí¸®)°¡ Æ÷ÇԵǾî ÀÖ½À´Ï´Ù. Microsoft Office XP¿¡´Â PIA°¡ Æ÷ÇԵǾî ÀÖÁö ¾ÊÁö¸¸ PIA¸¦ ´Ù¿î·ÎµåÇÒ ¼ö ÀÖ½À´Ï´Ù. Office XP PIA¿¡ ´ëÇÑ ÀÚ¼¼ÇÑ ³»¿ëÀº Microsoft ±â¼ú ÀÚ·áÀÇ ´ÙÀ½ ¹®¼­¸¦ ÂüÁ¶ÇϽʽÿÀ.
      328912? (http://support.microsoft.com/kb/328912/ ) Microsoft Office XP PIA¸¦ ´Ù¿î·ÎµåÇÒ ¼ö ÀÖ´Ù
    3. ÂüÁ¶ Ãß°¡ ´ëÈ­ »óÀÚ¿¡¼­ È®ÀÎÀ» ´­·¯ ¼±ÅÃÇÑ ³»¿ëÀ» Àû¿ëÇÕ´Ï´Ù. ¼±ÅÃÇÑ ¶óÀ̺귯¸®¿¡ ´ëÇØ ·¡ÆÛ¸¦ »ý¼ºÇÒ °ÍÀÎÁö ¹¯´Â ¸Þ½ÃÁö°¡ ³ªÅ¸³ª¸é ¿¹¸¦ ´©¸¨´Ï´Ù.
  4. º¸±â ¸Þ´º¿¡¼­ µµ±¸ »óÀÚ¸¦ ¼±ÅÃÇÏ¿© µµ±¸ »óÀÚ¸¦ Ç¥½ÃÇÕ´Ï´Ù. ´ÜÃß µÎ °³¿Í È®Àζõ Çϳª¸¦ Form1¿¡ Ãß°¡ÇÕ´Ï´Ù.
  5. È®ÀζõÀÇ À̸§ ¹× ÅØ½ºÆ® ¼Ó¼ºÀ» FillWithStrings·Î ¼³Á¤ÇÕ´Ï´Ù.
  6. Button1À» µÎ ¹ø ´©¸¨´Ï´Ù. ÇØ´ç Æû¿¡ ´ëÇÑ ÄÚµå âÀÌ ³ªÅ¸³³´Ï´Ù.
  7. ÄÚµå â¿¡¼­
    private void button1_Click(object sender, System.EventArgs e)
    {
    }
    					
    À§ÀÇ Äڵ带 ¾Æ·¡¿Í °°ÀÌ ¹Ù²ß´Ï´Ù.
          //Declare these two variables globally so you can access them from both
          //Button1 and Button2.
          Excel.Application objApp;
          Excel._Workbook objBook;
    
          private void button1_Click(object sender, System.EventArgs e)
          {
             Excel.Workbooks objBooks;
             Excel.Sheets objSheets;
             Excel._Worksheet objSheet;
             Excel.Range range;
    
             try
             {
                // Instantiate Excel and start a new workbook.
                objApp = new Excel.Application();
                objBooks = objApp.Workbooks;
                objBook = objBooks.Add( Missing.Value );
                objSheets = objBook.Worksheets;
                objSheet = (Excel._Worksheet)objSheets.get_Item(1);
    
                //Get the range where the starting cell has the address
                //m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
                range = objSheet.get_Range("A1", Missing.Value);
                range = range.get_Resize(5, 5);
    
                if (this.FillWithStrings.Checked == false)
                {
                   //Create an array.
                   double[,] saRet = new double[5, 5];
    
                   //Fill the array.
                   for (long iRow = 0; iRow < 5; iRow++)
                   {
                      for (long iCol = 0; iCol < 5; iCol++)
                      {
                         //Put a counter in the cell.
                         saRet[iRow, iCol] = iRow * iCol;
                      }
                   }
    
                   //Set the range value to the array.
                   range.set_Value(Missing.Value, saRet );
                }
    
                else
                {
                   //Create an array.
                   string[,] saRet = new string[5, 5];
    
                   //Fill the array.
                   for (long iRow = 0; iRow < 5; iRow++)
                   {
                      for (long iCol = 0; iCol < 5; iCol++)
                      {
                         //Put the row and column address in the cell.
                         saRet[iRow, iCol] = iRow.ToString() + "|" + iCol.ToString();
                      }
                   }
    
                   //Set the range value to the array.
                   range.set_Value(Missing.Value, saRet );
                }
    
                //Return control of Excel to the user.
                objApp.Visible = true;
                objApp.UserControl = true;
             }
             catch( Exception theException ) 
             {
                String errorMessage;
                errorMessage = "Error: ";
                errorMessage = String.Concat( errorMessage, theException.Message );
                errorMessage = String.Concat( errorMessage, " Line: " );
                errorMessage = String.Concat( errorMessage, theException.Source );
    
                MessageBox.Show( errorMessage, "Error" );
             }
          }
    					
    Âü°í Visual Studio 2005¿¡¼­´Â Äڵ带 º¯°æÇØ¾ß ÇÕ´Ï´Ù. ±âº»ÀûÀ¸·Î Visual C#¿¡¼­´Â Windows Forms ÇÁ·ÎÁ§Æ®¸¦ ¸¸µé¸é ÇØ´ç ÇÁ·ÎÁ§Æ®¿¡ Æû Çϳª°¡ Ãß°¡µË´Ï´Ù. ÀÌ ÆûÀÇ À̸§Àº Form1ÀÔ´Ï´Ù. ÆûÀ» ³ªÅ¸³»´Â µÎ °³ÀÇ ÆÄÀÏ À̸§Àº °¢°¢ Form1.cs¿Í Form1.designer.csÀÔ´Ï´Ù. ÄÚµå´Â Form1.cs¿¡ ÀÛ¼ºÇÕ´Ï´Ù. Windows Forms µðÀÚÀ̳ʿ¡¼­´Â »ç¿ëÀÚ°¡ µµ±¸ »óÀÚ¿¡¼­ ÄÁÆ®·ÑÀ» ²ø¾î ³õ´Â ¹æ¹ýÀ¸·Î ¼öÇàÇÑ ¸ðµç ÀÛ¾÷À» ±¸ÇöÇÏ´Â Äڵ带 Form1.designer.cs ÆÄÀÏ¿¡ ÀÛ¼ºÇÕ´Ï´Ù.

    Visual C# 2005ÀÇ Windows Forms µðÀÚÀ̳ʿ¡ ´ëÇÑ ÀÚ¼¼ÇÑ ³»¿ëÀº ´ÙÀ½ MSDN(Microsoft Developer Network) À¥ »çÀÌÆ®¸¦ ÂüÁ¶ÇϽʽÿÀ.
    http://msdn2.microsoft.com/ko-kr/library/ms173077.aspx (http://msdn2.microsoft.com/ko-kr/library/ms173077.aspx)
  8. Form1ÀÇ µðÀÚÀÎ º¸±â·Î µ¹¾Æ°¡¼­ Button2¸¦ µÎ ¹ø ´©¸¨´Ï´Ù.
  9. ÄÚµå â¿¡¼­
    private void button2_Click(object sender, System.EventArgs e)
    {
    }
    					
    À§ÀÇ Äڵ带 ¾Æ·¡¿Í °°ÀÌ ¹Ù²ß´Ï´Ù.
    private void button2_Click(object sender, System.EventArgs e)
          {
             Excel.Sheets objSheets;
             Excel._Worksheet objSheet;
             Excel.Range range;
    
             try
             {
                try
                {
                   //Get a reference to the first sheet of the workbook.
                   objSheets = objBook.Worksheets;
                   objSheet = (Excel._Worksheet)objSheets.get_Item(1);
                }
    
                catch( Exception theException ) 
                {
                   String errorMessage;
                   errorMessage = "Can't find the Excel workbook.  Try clicking Button1 " +
                      "to create an Excel workbook with data before running Button2.";
    
                   MessageBox.Show( errorMessage, "Missing Workbook?");
    
                   //You can't automate Excel if you can't find the data you created, so 
                   //leave the subroutine.
                   return;
                }
    
                //Get a range of data.
                range = objSheet.get_Range("A1", "E5");
    
                //Retrieve the data from the range.
                Object[,] saRet;
                saRet = (System.Object[,])range.get_Value( Missing.Value );
    
                //Determine the dimensions of the array.
                long iRows;
                long iCols;
                iRows = saRet.GetUpperBound(0);
                iCols = saRet.GetUpperBound(1);
    
                //Build a string that contains the data of the array.
                String valueString;
                valueString = "Array Data\n";
    
                for (long rowCounter = 1; rowCounter <= iRows; rowCounter++)
                {
                   for (long colCounter = 1; colCounter <= iCols; colCounter++)
                   {
    
                      //Write the next value into the string.
                      valueString = String.Concat(valueString,
                         saRet[rowCounter, colCounter].ToString() + ", ");
                   }
    
                   //Write in a new line.
                   valueString = String.Concat(valueString, "\n");
                }
    
                //Report the value of the array.
                MessageBox.Show(valueString, "Array Values");
             }
    
             catch( Exception theException ) 
             {
                String errorMessage;
                errorMessage = "Error: ";
                errorMessage = String.Concat( errorMessage, theException.Message );
                errorMessage = String.Concat( errorMessage, " Line: " );
                errorMessage = String.Concat( errorMessage, theException.Source );
    
                MessageBox.Show( errorMessage, "Error" );
             }
          }
    					
  10. ÄÚµå âÀÇ ¸Ç À§·Î ½ºÅ©·ÑÇÕ´Ï´Ù. using Áö½Ã¹® ¸ñ·ÏÀÇ ³¡¿¡ ´ÙÀ½ ÇàÀ» Ãß°¡ÇÕ´Ï´Ù.
    using System.Reflection; 
    using Excel = Microsoft.Office.Interop.Excel;
    					

ÀÚµ¿È­ Ŭ¶óÀÌ¾ðÆ® Å×½ºÆ®

  1. F5 ۸¦ ´­·¯ ¿¹Á¦ ÇÁ·Î±×·¥À» ºôµåÇÏ°í ½ÇÇàÇÕ´Ï´Ù.
  2. Button1À» ´©¸¨´Ï´Ù. Microsoft ExcelÀÌ ½ÃÀÛµÇ¾î »õ·Î¿î ÅëÇÕ ¹®¼­°¡ ³ªÅ¸³ª°í ù ¹øÂ° ¿öÅ©½ÃÆ®ÀÇ A1:E5 ¼¿ÀÌ ¹è¿­ÀÇ ¼ýÀÚ µ¥ÀÌÅͷΠä¿öÁý´Ï´Ù.
  3. Button2¸¦ ´©¸¨´Ï´Ù. A1:E5 ¼¿ÀÇ µ¥ÀÌÅͰ¡ »õ·Î¿î ¹è¿­¿¡ »ðÀÔµÇ°í °á°ú°¡ ¸Þ½ÃÁö »óÀÚ¿¡ Ç¥½ÃµË´Ï´Ù.
  4. FillWithStrings¸¦ ¼±ÅÃÇϰí Button1À» ´­·¯ A1:E5 ¼¿À» ¹®ÀÚ¿­ µ¥ÀÌÅͷΠä¿ó´Ï´Ù.

ÂüÁ¶

ÀÚ¼¼ÇÑ ³»¿ëÀº ´ÙÀ½ MSDN(Microsoft Developer Network) À¥ »çÀÌÆ®¸¦ ÂüÁ¶ÇϽʽÿÀ.
Visual Studio¸¦ »ç¿ëÇÑ Microsoft Office °³¹ß
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx (http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx) (¿µ¹®)
¹è¿­À» »ç¿ëÇÏ¿© ÀÌÀü ¹öÀüÀÇ Visual Studio¿¡¼­ Excel µ¥ÀÌÅ͸¦ ¼³Á¤ÇÏ°í °¡Á®¿À´Â ¹æ¹ý¿¡ ´ëÇÑ ÀÚ¼¼ÇÑ ³»¿ëÀº Microsoft ±â¼ú ÀÚ·áÀÇ ´ÙÀ½ ¹®¼­¸¦ ÂüÁ¶ÇϽʽÿÀ.
186120? (http://support.microsoft.com/kb/186120/ ) MFC¸¦ »ç¿ëÇÏ¿© ExcelÀ» ÀÚµ¿È­ÇÏ°í ¹è¿­·Î ¹üÀ§¸¦ ä¿ì´Â ¹æ¹ý
186122? (http://support.microsoft.com/kb/186122/ ) MFC¸¦ »ç¿ëÇÏ¿© Excel 2000 ¹× Excel 2002¸¦ ÀÚµ¿È­Çϰí Excel 2000 ¹× Excel 2002ÀÇ ¹üÀ§¿¡¼­ ¹è¿­À» ±¸ÇÏ´Â ¹æ¹ý
247412? (http://support.microsoft.com/kb/247412/ ) Visual Basic¿¡¼­ Excel·Î µ¥ÀÌÅ͸¦ Àü¼ÛÇÏ´Â ¹æ¹ý




Microsoft Á¦Ç° °ü·Ã ±â¼ú Àü¹®°¡µé°ú ¿Â¶óÀÎÀ¸·Î Á¤º¸¸¦ ±³È¯ÇϽ÷Á¸é Microsoft ´º½º ±×·ì (http://support.microsoft.com/newsgroups/default.aspx) ¿¡ Âü¿©ÇϽñ⠹ٶø´Ï´Ù.

º» ¹®¼­ÀÇ Á¤º¸´Â ´ÙÀ½ÀÇ Á¦Ç°¿¡ Àû¿ëµË´Ï´Ù.
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Office Excel 2003
  • Microsoft Excel 2002 Standard Edition
Ű¿öµå:?
kbautomation kbhowto KB302096