2016-05-30 29 views
0

这似乎是我的逻辑错误。但是如何?这对我来说似乎是好的。我究竟做错了什么?表单已创建。但整个内容都在第一张纸上。 我甚至记得有一次硬编码.get_Item(2),它写在第二张纸上,为什么不现在呢?C#和Excel:写在多张纸上,它总是写在第一张?

public void createXls() 
    { 
     Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); 

     if (xlApp == null) 
     { 
      Console.WriteLine("Excel is not properly installed!!"); 
      return; 
     } 

     object misValue = System.Reflection.Missing.Value; 
     Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(misValue); 

     for (int z = 1; z <= Sheets.Count; z++) 
     { 
      if (z > 1) xlWorkBook.Worksheets.Add(misValue, misValue, 1, misValue); 
      Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(z); 
      Console.WriteLine("We are writing on sheet #" + z); // it says ...1, 2, 3, 4, 5 
      for (int y = 1; y <= Sheets[z - 1].Count; y++) 
      { 
       int x = 1; 
       foreach (var Column in Sheets[z - 1][y - 1]) 
       { 
        xlWorkSheet.Cells[y, x] = Column.Value; 
        x++; 
       } 
      } 
      releaseObject(xlWorkSheet); 
     } 


     xlWorkBook.SaveAs("\\\\psf\\Home\\Desktop\\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); 
     xlWorkBook.Close(true, misValue, misValue); 
     xlApp.Quit(); 

     releaseObject(xlWorkBook); 
     releaseObject(xlApp); 
    } 

    private void releaseObject(object obj) 
    { 
     try 
     { 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); 
      obj = null; 
     } 
     catch (Exception ex) 
     { 
      obj = null; 
      Console.WriteLine("Exception Occured while releasing object " + ex.ToString()); 
     } 
     finally 
     { 
      GC.Collect(); 
     } 
    } 
+0

我可能是盲人,Sheets'定义在哪里? –

回答

0

看来,什么是错与样品 - Sheets不是变量,也不是一个类的范围。

当我将发生的Sheets更改为xlWorkBook.Sheets时,我得到一个未处理的COM异常,指出该索引无效。

我认为你的问题最有可能的解释是你在代码中没有粘贴样本的地方分配了Sheets。您还应该仔细检查您的索引是否正确,以及您是否在正确的对象上使用索引器。

无关提示:

您可以使用索引属性,而不是get_Item例如[2]而不是get_Item(2)

局部变量应该以小写字母开头(例如column应该用来代替Column)。

+0

哦,这可能会让人困惑,但Sheets是我公开声明的变数。但谢谢你的提示 – Stringering

0

我认为它应该是这样的。

public class Tyburn1 
{ 
    object missing = Type.Missing; 
    public Tyburn1() 
    { 
     Excel.Application oXL = new Excel.Application(); 
     oXL.Visible = false; 
     Excel.Workbook oWB = oXL.Workbooks.Add(missing); 
     Excel.Worksheet oSheet = oWB.ActiveSheet as Excel.Worksheet; 
     oSheet.Name = "The first sheet"; 
     oSheet.Cells[1, 1] = "Something"; 
     Excel.Worksheet oSheet2 = oWB.Sheets.Add(missing, missing, 1, missing) 
         as Excel.Worksheet; 
     oSheet2.Name = "The second sheet"; 
     oSheet2.Cells[1, 1] = "Something completely different"; 
     string fileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)   
           + "\\SoSample.xlsx"; 
     oWB.SaveAs(fileName, Excel.XlFileFormat.xlOpenXMLWorkbook, 
      missing, missing, missing, missing, 
      Excel.XlSaveAsAccessMode.xlNoChange, 
      missing, missing, missing, missing, missing); 
     oWB.Close(missing, missing, missing); 
     oXL.UserControl = true; 
     oXL.Quit(); 
    } 
} 

,增加了第二片是语句...

Excel.Worksheet oSheet2 = oWB.Sheets.Add(missing, missing, 1, missing) 
          as Excel.Worksheet; 

“1”参数指定一张纸,它可以是更多,如果你想一次添加多个表。

最后说明:声明oXL.Visible = false;告诉Excel以静默模式启动。

相关问题