创build一个新的Excel工作表并命名

我只写了我的数据到1个Excel工作表,但我想创build一个新工作表并将其称为“xxx”并在其中写入数据。

这是我现在拥有的:

private static Microsoft.Office.Interop.Excel.ApplicationClass appExcel; private static Workbook newWorkbook_First = null; private static _Worksheet objsheet = null; excel_init("C:\\Users\\me\\Desktop\\excel2.xlsx"); for (int i = 1; i < WindowsXPLijst.Count; i++) { excel_setValue("B" + i, WindowsXPLijst[i]); } excel_close(); static void excel_init(String path) { appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass(); if (System.IO.File.Exists(path)) { // then go and load this into excel newWorkbook_First = appExcel.Workbooks.Open(path, true, true); objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet; } else { try { appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass(); appExcel.Visible = true; newWorkbook_First = appExcel.Workbooks.Add(1); objsheet = (Microsoft.Office.Interop.Excel.Worksheet)newWorkbook_First.Sheets[1]; } catch (Exception e) { Console.Write("Error"); } finally { } } } static void excel_setValue(string cellname, string value) { objsheet.get_Range(cellname).set_Value(Type.Missing, value); objsheet.get_Range(cellname).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); } 

我没有那么多改变,因为我不知道如何改变这个function:

 static void excel_setValue(string cellname, string value, string tab) { objsheet = tab // No ica what to exactly put here } { excel_setValue("B" + i, WindowsXPLijst[i], "xxx"); } 

这将在您的最后一张表格之后创build一张新的表格:

 Dim WS As Worksheet Set WS =Sheets.Add(After:=Sheets(Worksheets.count)) WS.name = "xxx" 

这个代码是在这里find的代码的精简版本