0

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);
4

2 に答える 2

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)?_

于 2012-08-02T09:49:51.147 に答える
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";
于 2012-08-02T13:10:18.260 に答える