附加到现有的Excel文件

我有写这个function,并保存到一个excel文件。 我现在要做的是附加到相同的现有的Excel文件。这是function:

private void button8_Click(object sender, EventArgs e)//creates excel file and writes data to it { Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { MessageBox.Show("Excel is not properly installed!!"); return; } if (System.IO.File.Exists("cross_check.xls")) { } else { Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); IndexProp += 1; xlWorkSheet.Cells[IndexProp, 1] = comboBox2.Text; xlWorkSheet.Cells[IndexProp, 2] = textBox5.Text; xlWorkSheet.Cells[IndexProp, 3] = textBox2.Text; xlWorkSheet.Cells[IndexProp, 4] = comboBox3.Text; xlWorkSheet.Cells[IndexProp, 5] = textBox3.Text; xlWorkSheet.Cells[IndexProp, 6] = comboBox1.Text; xlWorkBook.SaveAs(@"cross_check.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); Marshal.ReleaseComObject(xlWorkSheet); Marshal.ReleaseComObject(xlWorkBook); Marshal.ReleaseComObject(xlApp); MessageBox.Show("Excel file created succcessfully"); } } } 

这个特定的块是检查文件是否存在,然后执行追加操作(如果存在)。 我该怎么做呢?

  if (System.IO.File.Exists("cross_check.xls")) { } 

你会想打开文件使其成为活动的工作簿,然后做任何你想要的更改。 从那里你可以保存或做你想要的东西。