各位大哥:
    我现使用VS2010 旗舰版,在编写一段代码实现将数组的内容写入一个EXCEL文件中的不同Sheet表里时,总是失败,请大家能否给一段成功运行的代码呀?谢谢了。下面贴上我的代码段,也请大家帮忙看看为什么失败?
private void createxcel()
        {
           
                Excel.Application xlapp1 = default(Excel.Application);
                //Excel对象
                Excel.Workbook xlbook1 = default(Excel.Workbook);
                //工作簿
                Excel.Worksheet xlsheet1 = default(Excel.Worksheet);
                //工作表
              
               
                try
                {
                    xlapp1 = new Excel.Application();
                    xlapp1.Visible = false;
                    xlapp1.DisplayAlerts = false;
                    xlapp1.ShowWindowsInTaskbar = false;
                    
                    xlbook1 = xlapp1.Workbooks.Add() ;
                    //新建EXCEL工件簿文件
                    xlsheet1 = xlbook1.Worksheets[1];//这句代码总是出错,无法通过编译,该怎么写呢?
                                       
                    xlsheet1.Cells[1, 1] = "流水号";
                    xlsheet1.Cells[1, 1] = "流水号";
                    xlsheet1.Cells[1, 2] = "名称";
                    xlsheet1.Cells[1, 3] = "主题";
                    xlsheet1.Cells[1, 4] = "时间";
                    xlsheet1.Cells[1, 5] = "时限";
                    xlsheet1.Cells[1, 6] = "状态";
                    xlsheet1.Cells[1, 7] = "时间";
                    xlsheet1.Cells[1, 20] = "地址";
                    xlsheet1.Name = DateTime.Now.Year.ToString() + "年";
                    xlsheet1.Cells.Font.Size = 10;
                    xlsheet1.Cells.Font.Bold = false;                    xlsheet1 = xlbook1.Worksheets[2];//和上面的sheet不同。
                    
                    xlsheet1.Cells[1, 1] = "流水号";
                    xlsheet1.Cells[1, 2] = "主题";
                    xlsheet1.Cells[1, 3] = "时间";
                    xlsheet1.Cells[1, 4] = "时限";
                    xlsheet1.Cells[1, 5] = "状态";
                    xlsheet1.Cells[1, 6] = "时间";
                    xlsheet1.Cells[1, 20] = "地址";
                    xlsheet1.Name = DateTime.Now.Year.ToString() + "年";
                    xlsheet1.Cells.Font.Size = 10;
                    xlsheet1.Cells.Font.Bold = false;                    xlbook1.SaveAs(datafile + ".xls");
                    //保存文件                }
                catch (Exception ex)
                {
                    log(ex.Message.ToString());
                }
                finally
                {
                    xlapp1.Workbooks.Close();
                    xlapp1.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp1);
                    xlsheet1 = null;
                    xlbook1 = null;
                    xlapp1 = null;
                    System.GC.Collect(0);
                }
                    }

解决方案 »

  1.   

    xlbook1.Worksheets[0];
    xlbook1.Worksheets[1];
      

  2.   

    我用的: xlsheet1 = xlbook1.Worksheets[1];语法错误了呀,改成0了,也还是错。
    xlbook1.Worksheets[1]这块下面显示红色的波浪号,并有提示“找不到编译动态表达式的一种或多种类型。是否缺少对microsoft.CSharp.dll和System.core.dll的引用”。这里我就不知道该怎么弄了。
      

  3.   

    这很明显了缺少DLL请查看是否已经引用或者你电脑上压根没装OFFICE
      

  4.   

    前提必须引用DLL
     private void SaveTable()
            {
                Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                Microsoft.Office.Interop.Excel.WorkbookClass ExcelWorkBook = (Microsoft.Office.Interop.Excel.WorkbookClass)ExcelApp.Workbooks.Open(Application.StartupPath + "\\mybook.xls", Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value
                    , Missing.Value, Missing.Value, Missing.Value);
                Microsoft.Office.Interop.Excel.Worksheet ExcelWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)ExcelWorkBook.Worksheets[1];
                pm.GetEntitySize(pm.enumPowerMILLEntityType.pmModel, ModName, ref Xmin, ref Xmax, ref Ymin, ref Ymax, ref Zmin, ref Zmax);
                ExcelWorkSheet.Cells[22, 5] = "X:" + ((Xmax - Xmin) / 2).ToString() + " Y:" + ((Ymax - Ymin) / 2).ToString() + " Z:" + (Zmax - Zmin).ToString();
                //ExcelWorkSheet.Cells[5, 12] = Session;
                //ExcelWorkSheet.Cells[32, 9] = Date;
                //ExcelWorkSheet.Cells[2, 8] = Xmax;
                //ExcelWorkSheet.Cells[2, 9] = Ymax;
                //ExcelWorkSheet.Cells[2, 10] = Zmax;
                //ExcelWorkSheet.Cells[3, 8] = Xmin;
                //ExcelWorkSheet.Cells[3, 9] = Ymin;
                //ExcelWorkSheet.Cells[3, 10] = Zmin;
                ExcelWorkBook.Save();
                ExcelApp.Quit();
                ExcelApp = null;
                ExcelWorkBook = null;
                ExcelWorkSheet = null;
                GC.Collect();
            }
      

  5.   

    我之前引用了如下:
    using System;
    using System.Configuration;
    using System.Security.Cryptography;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    using System.Net.Mail;
    using Microsoft.Office.Core;
    using Excel = Microsoft.Office.Interop.Excel;
    using System.IO;另外,我电脑上肯定是装了OFFICE2007呀。
      

  6.   

    添加引用而不是添加USING
    OFFICE2007 是Microsoft.Office.Interop.Excel 12 版的
      

  7.   

    我也添加了引用的。12.0的版本,还是一样的问题。另外我将你上面的代码复制到VS里,在ApplicationClass下面出现红色波浪号,提示“无法嵌入互操作类型Microsoft.Office.Interop.Excel.ApplicationClass,请改用适用的接口”的提示。之前我也遇到过类似的问题,然后我就用的application。还有在Microsoft.Office.Interop.Excel.Worksheet ExcelWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)ExcelWorkBook.Worksheets[1];这句的括号下面也出现了红色波浪线,提示“找不到编译动态表达式的一种或多种类型。是否缺少对microsoft.CSharp.dll和System.core.dll的引用”。难道是我的电脑里缺什么吗?
      

  8.   

    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    Workbook xlBook = xlApp.Workbooks.Add(true);第一个sheet
      Worksheet wsMask = (Worksheet)xlBook.Worksheets["sheet1"];
      wsMask.Columns.ColumnWidth = 2;
      wsMask.Columns.RowHeight = 13.5;
      wsMask.Name = "1";第二个sheet Worksheet wsElement = (Worksheet)xlBook.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
     wsElement.Name = "2";第三个sheet Worksheet wsElement2 = (Worksheet)xlBook.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
     wsElement2.Name = "3";。保存成07的  xlBook.SaveAs(outputFileName, 56, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);保存成03的  
     xlBook.SaveAs(outputFileName, 43, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
      

  9.   

    已经知道是为什么老是出错的原因了,解决办法是将引入的COM对象(miscrosoft excel 12.0 object library),属性里的嵌入互操作类型改为Fasle。感谢xu56180825和kkxxllasasas两位同学的回答。