[英]appending to an existing excel file
我有寫和保存到excel文件的功能。 我現在想要做的是追加到相同的現有excel文件中。這是函數:
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"))
{
}
您可能要打開文件使其成為活動工作簿,然后進行所需的任何更改。 從那里,您可以保存它或做任何您想做的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.