dynamic读取和写入Excel工作表

我正在尝试使用多个工作表创build一个新的Excel文件(#sheets =#NameList中的项目)。 然后,每张表都应该用NameList中的相应条目命名。 完成之后,我需要调用每张纸的方法。

我一直有的问题是:工作表不喜欢被设置为一个列表,并不会接受(Excel.Worksheet)工作簿中的variables.Sheets [variables]

我可以完美地写入一张纸,但任何尝试创build纸张都失败了。

public CreateExcelDoc excell_app { get; set; } public Excel.Workbook workbook { get; set; } public List<string> NameList = new List<string>(); public List<Excel.Worksheet> worksheets { get; set; } // tried: = new List<Excel.Worksheet>(); public void setupFile(int i) // this method errors on each of the 2 lines { worksheets[i] = (Excel.Worksheet)workbook.Worksheets.Add(); worksheets[i].Name = NameList[i]; } // this method is in a separate public class from CreateExcelDoc public class CreateExcelDoc { private Excel.Application app = null; Excel.Workbook workbook = null; Excel.Worksheet worksheet = null; private Excel.Range workSheet_range = null; public CreateExcelDoc() { try { app = new Excel.Application(); app.Visible = true; workbook = app.Workbooks.Add(1); //worksheets[0] = (Excel.Worksheet)workbook.Sheets[1]; //worksheet.Name = "t"; } catch (Exception e) { MessageBox.Show("Exception Occured while exporting to Excel doc. Error: " + e.ToString()); } finally { } } _Other Methods for writing to cells omitted_ }