回 帖 发 新 帖 刷新版面

主题:关于C# 对Excel的操作,---help me ~

首先添加引用:



          using System.Reflection; 
          using Excel = Microsoft.Office.Interop.Excel;


     声明两个类的成员变量     
      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" );
         }
      }

回复列表 (共6个回复)

沙发

好心的人啊~

帮个忙啊,解释一下...

板凳

HELP...

3 楼

4 楼

.........

帮忙顶一下啦?哪里错了???

5 楼

多少积分阿????

6 楼

首先添加引用:



          using System.Reflection; 
          using Excel = Microsoft.Office.Interop.Excel;


     声明两个类的成员变量     
      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.
    //初始化Excel,并开始一个新的工作簿.
            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.
    //m_sStartingCell和他的尺寸是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.
        //返回Excel控制给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" );
         }
      }


你是这个意思吧?

我来回复

您尚未登录,请登录后再回复。点此登录或注册