好学IT学院:IT信息技术分享交流平台
标签:C#  来源:Microsoft Corporation  作者:本站整理  发布时间:2007-02-04  ★★★加入收藏〗〖手机版
摘要:若要填充一个多单元格区域而又不是一次一个单元格地进行填充,可以将“Range”对象的“Value”属性设置为二维数组。同样,通过使用“Value”属性,可以一次检索多个单元格的值的二维数组…

内容提示:若要填充一个多单元格区域而又不是一次一个单元格地进行填充,可以将“Range”对象的“Value”属性设置为二维数组。同样,通过使用“Value”属性,可以一次检索多个单元格的值的二维数组。下面的步骤阐述了这一使用二维数组设置和检索数据的过程。

为 Microsoft Excel 生成自动化客户端

1. 启动 Microsoft Visual Studio 2005 或 Microsoft Visual Studio .NET。

2. 在“文件”菜单上,单击“新建”,然后单击“项目”。从 Visual C# 项目类型中选择“Windows 应用程序”。默认情况下会创建 Form1。

3. 添加对 Visual Studio 2005 中“Microsoft Excel 11.0 对象库”或 Visual Studio .NET 中“Microsoft Excel 对象库”的引用。为此,请按照下列步骤操作:

a.  在“项目”菜单上,单击“添加引用”。
b.  在“COM”选项卡上,找到“Microsoft Excel 对象库”,然后单击“选择”。

在 Visual Studio 2005 中,请在“COM”选项卡上找到“Microsoft Excel 11.0 对象库”。

注意:Microsoft Office 2003 包含主互操作程序集 (PIA)。Microsoft Office XP 不包含 PIA,但您可以下载 PIA。有关 Office XP PIA 的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
328912 (http://support.microsoft.com/kb/328912/) MicrosoftOffice XP 主互操作程序集 (PIA) 可供下载

c.  在“添加引用”对话框中,单击“确定”以接受您的选择。如果系统提示您为选定的库生成包装,请单击“是”。

4. 在“视图”菜单上,选择“工具箱”以显示工具箱。向 Form1 添加两个按钮和一个复选框。

5. 将复选框的“Name”和“Text”属性设置为“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 中更改代码。默认情况下,当您创建一个 Windows 窗体项目时,Visual C# 将向该项目中 添加一个窗体。该窗体名为 Form1。表示该窗体的两个文件分别名为 Form1.cs 和 Form1.designer.cs。您在 Form1.cs 中编写代码。而 Windows 窗体设计器在 Form1.designer.cs 文件中写入代码,以实现您通过在工具箱中拖放控件所执行的所有操作。

有关 Visual C# 2005 中 Windows 窗体设计器的更多信息,请访问下面的 Microsoft Developer Network (MSDN) 网站:
http://msdn2.microsoft.com/zh-cn/library/ms173077.aspx (http://msdn2.microsoft.com/zh-cn/library/ms173077.aspx