Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将数据插入Excel工作表_C#_Excel - Fatal编程技术网

C# 如何将数据插入Excel工作表

C# 如何将数据插入Excel工作表,c#,excel,C#,Excel,我有一个文件Excel包含4张工作表,我如何在不使用OLEDB的情况下将数据写入工作表2,并且我想逐个单元格写入数据。。。 有人能帮我吗 我有这样一个代码: Excel._Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; privat

我有一个文件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();
        }

要使用工作表2,您必须使用此

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
这意味着xWorkSheet对象指向sheet2,添加到单元格的值将添加到sheet2

你可以用这个给单元格加值

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

要使用工作表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,您可以使用工作簿对象中“工作表”属性上的索引来访问不同的工作表:

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

使用Office Interop,您可以使用工作簿对象中“工作表”属性上的索引来访问不同的工作表:

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

他上面使用的也是Office Interop的一部分。他上面使用的也是Office Interop的一部分