2012-08-02 134 views
0

我正在编写一个程序来填充Excel工作簿中的数据,但目前我只能访问第一张工作表。如何从Excel工作簿打开多个工作表?

有了这个测试我不能让sheet2和sheet3接收任何数据?

请有人指出我错过了什么?

Excel.Application xlApp; 
Excel.Workbook xlWorkBook; 
Excel.Worksheet[] xlWorkSheet = new Excel.Worksheet[3]; 
object misValue = System.Reflection.Missing.Value; 

private void Form1_Load(object sender, EventArgs e) 
     { 

    if (File.Exists("C:/Users/Shaun/Documents/Template.xls")) 
       { 
        //Load Templete SpreadSheet 
        xlApp = new Excel.ApplicationClass(); 
        xlWorkBook = xlApp.Workbooks.Open("Template.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); 
        xlWorkSheet[0] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
        xlWorkSheet[1] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
        xlWorkSheet[2] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
       } 
       else 
       { 
        MessageBox.Show("Could not find file Template.xls"); 
       } 
      } 



private void button1_Click(object sender, EventArgs e) 
     { 
      xlWorkSheet[0].Cells[1, 1] = "Sheet1"; 
      xlWorkSheet[1].Cells[1, 1] = "Sheet2"; 
      xlWorkSheet[2].Cells[1, 1] = "Sheet3"; 

     String Saveas; 
     Saveas = Microsoft.VisualBasic.Interaction.InputBox("Save As", "Save As", ""); 
     xlWorkBook.SaveAs(Saveas + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); 
     xlWorkBook.Close(true, misValue, misValue); 
     xlApp.Quit(); 
     for (int p = 0; p < 3; p++) 
     { 
      releaseObject(xlWorkSheet[p]); 
     } 
     releaseObject(xlWorkBook); 
     releaseObject(xlApp); 
     MessageBox.Show("File Saved!");  
     } 

private void releaseObject(object obj) 
    { 
     try 
     { 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); 
      obj = null; 
     } 
     catch (Exception ex) 
     { 
      obj = null; 
      MessageBox.Show("Unable to release the Object " + ex.ToString()); 
     } 
     finally 
     { 
      GC.Collect(); 
     } 
    } 

编辑

即使这种改变还有什么发生,我不能拿起任何表,但第一个。

xlWorkSheet[0] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
xlWorkSheet[1] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2); 
xlWorkSheet[2] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3); 
+2

我试着使用一个空的xls文件作为Template.xls您的确切代码(当然与工作表集合的索引中的更改)。一切正常。它可能是你的模板中的问题? – 2012-08-02 14:06:08

+1

是的,谢谢:) – Pomster 2012-08-03 09:06:13

回答

4

也许错误是在这里:

xlWorkSheet[0] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
xlWorkSheet[1] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
xlWorkSheet[2] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 

get_Item(2)get_Item(3)

+0

我试过没有成功。 – Pomster 2012-08-02 09:50:36

+0

在这个问题中,程序员也只有它.get_Item(1);对全部。 http://stackoverflow.com/q/3668282/1479146 – Pomster 2012-08-02 10:22:07

+1

你有没有尝试'xlWorkBook.Worksheets [i]'为我= 1,2,3? – martin 2012-08-02 12:34:20

0

希望以下内容有所帮助,因为这是我过去所做的。

private Excel.Application _xlApp = new Excel.Application(); 
private Excel.Workbooks _xlWorkBooks; 
private Excel.Workbook _xlWorkBook; 
private Excel.Worksheet _xlWorkSheet; 

_xlWorkBooks = _xlApp.Workbooks; 
_xlWorkBook = _xlWorkBooks.Add(1); 

//get a worksheet 
_xlWorkSheet = (Excel.Worksheet)_xlWorkBook.Sheets[_xlWorkBook.Worksheets.Count]; 

//get a range 
rng = _xlWorkSheet.get_Range(_xlWorkSheet.Cells[8, 5], _xlWorkSheet.Cells[9, 5]); 

//e.g. bold the range 
rng.Cells.Font.Bold = true; 

//merge the range cells 
rng.MergeCells = true; 

//set cell data 
_xlWorkSheet.Cells[8, 3] = "Text Here"; 
相关问题