如何将数据插入Excel工作表

我有一个文件Excel包含4张表格,我怎样才能写入数据到表格2,而不使用OLEDB,我想逐个写入数据…可以帮助我吗?

我有这样的代码:

Excel._Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; private void button1_Click(object sender, EventArgs e) { Excel._Application xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Open("E:\\Project Skripsi\\normalisasi.xlsx", 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xlWorkSheet.Cells[1, 1].Value2 = Convert.ToInt32(xlWorkSheet.Cells[1, 1].Value2) + 1; xlWorkBook.Close(false, misValue, misValue); xlApp.Quit(); } 

对于使用工作表2,你将不得不使用这个..

 xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2); 

这意味着xWorkSheet对象指向sheet2,添加到单元格的值将被添加到sheet2。

你可以使用它为单元格添加值,

 xlWorkSheet.Cells[1, 1] = "YourValue(Could be of any data type)"; 

使用Office-Interop,可以通过使用Workbook对象中Worksheet-property的索引访问不同的工作表:

  //select the first sheet Excel.Worksheet sheet1 = (Worksheet) xlWorkBook.Worksheets[1]; //select the second sheet Excel.Worksheet sheet2 = (Worksheet) xlWorkBook.Worksheets[2];