我知道是要使用axSpreadSheet控件,但是如何把他和一個已存在的Excel文件關聯起來?也就是如何初始化這個axSpreadSheet控件?謝謝!

解决方案 »

  1.   

    http://dev.csdn.net/article/54/article/45/45299.shtm
      

  2.   

    先要添加对Excel的引用。选择项目-〉添加引用-〉COM-〉添加Microsoft Excel 9.0。(不同的office讲会有不同版本的dll文件)。
       using Excel;
       using System.Reflection;
       
       //产生一个Excel.Application的新进程
       Excel.Application app = new Excel.Application();
       if (app == null) 
       {
        statusBar1.Text = "ERROR: EXCEL couldn''t be started!";
        return ;
       }
       
       app.Visible = true; //如果只想用程序控制该excel而不想让用户操作时候,可以设置为false
       app.UserControl = true;
       
       Workbooks workbooks =app.Workbooks;
      
       _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet); //根据模板产生新的workbook
    //  _Workbook workbook = workbooks.Add("c:\\a.xls"); //或者根据绝对路径打开工作簿文件a.xls   Sheets sheets = workbook.Worksheets;
       _Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
       if (worksheet == null) 
       {
        statusBar1.Text =  "ERROR: worksheet == null";
        return;
       }   // This paragraph puts the value 5 to the cell G1
       Range range1 = worksheet.get_Range("A1", Missing.Value);
       if (range1 == null) 
       {
        statusBar1.Text =  "ERROR: range == null";
        return;
       }
       const int nCells = 2345;
       range1.Value2 = nCells;
      

  3.   

    to  cheng362000(星之辰) 
    這種做法是在程序的外面打開了一個Excel應用,我是想在一個winform中打開一個excel文件。
    anyway ,thks for reply!