我有一个文件Excel包含4个表格,如何在不使用OLEDB的情况下将数据写入表格2,我想逐个单元格地写入数据... 任何人都可以帮助我吗?
我有这样的代码:
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();
}
答案 0 :(得分:0)
对于使用工作表2,您将不得不使用此..
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
这意味着xWorkSheet对象指向sheet2,并且您添加到单元格的值将添加到sheet2。
您可以使用此功能向单元格添加值
xlWorkSheet.Cells[1, 1] = "YourValue(Could be of any data type)";
答案 1 :(得分:-1)
使用Office-Interop,您可以使用Workbook对象中Worksheet属性的索引访问不同的工作表:
//select the first sheet
Excel.Worksheet sheet1 = (Worksheet) xlWorkBook.Worksheets[1];
//select the second sheet
Excel.Worksheet sheet2 = (Worksheet) xlWorkBook.Worksheets[2];