C# 如何从excel工作簿中打开多个工作表?

C# 如何从excel工作簿中打开多个工作表?,c#,excel,interop,C#,Excel,Interop,我正在编写一个程序,用数据填充Excel工作簿,但目前我只能访问第一张工作表 通过此测试,我无法让sheet2和Sheet3接收任何数据 有人能指出我遗漏了什么吗 Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet[] xlWorkSheet = new Excel.Worksheet[3]; object misValue = System.Reflection.Missing.Value; priv

我正在编写一个程序,用数据填充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(1);
xlWorkSheet[2] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

应该是
get\u项(2)
get\u项(3)

可能错误在这里:

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\u Item(2)
get\u Item(3)

希望下面的内容对我有所帮助,就像我过去所做的那样

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";

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

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";

在这个问题上,程序员也有它;对所有人来说。您是否尝试了i=1、2、3的
xlWorkBook.Worksheets[i]
?我刚刚尝试了
get\u项(1);对于所有
,它会将所有内容写入第一页。然后我尝试了马丁的建议(获取第(2)项等),效果很好。我将
xlApp.Visible=true
添加到表单加载以检查这一点。我有点困惑,@Pommy没有打开一个完整地址的工作簿。在这个问题上,程序员也有它;对所有人来说。您是否尝试了i=1、2、3的
xlWorkBook.Worksheets[i]
?我刚刚尝试了
get\u项(1);对于所有
,它会将所有内容写入第一页。然后我尝试了马丁的建议(获取第(2)项等),效果很好。我将
xlApp.Visible=true
添加到表单加载以检查这一点。@Pommy没有打开一个完整地址的工作簿,这让我有点困惑。我使用一个空的xls文件作为Template.xls尝试了您的确切代码(当然是工作表集合索引中的更改)。一切正常。这可能是模板中的问题吗?我使用空xls文件template.xls尝试了您的确切代码(当然是工作表集合索引中的更改)。一切正常。这可能是模板中的问题吗?