如何从列表创建Excel文件使用Microsoft.Office.Interop.Excel

本文关键字:Excel 文件 Microsoft Interop Office 创建 列表 string | 更新日期: 2023-09-27 18:09:13

我有一个列表与一些值,我也有一个文本框,我必须写一个数字,然后我需要建立一个Excel与许多表的值来自列表。换句话说,例如:List有1000个值,然后我在TextBox中输入100个,所以我需要生成一个包含许多工作表的Excel文件,因为List中的值迭代在TextBox中输入的值。在这种情况下,将是一个包含10个工作表的Excel文件,每个工作表有100个单元格。很明显吗?我如何使用Microsoft.Office.Interop.Excel做到这一点?

如何从列表<string>创建Excel文件使用Microsoft.Office.Interop.Excel

工作表:

//get the first workbook in an application
Workbook WB = Application.Workbooks[0]; //Or any other workbook you preffer
Now loop the following for each list of strings you have (each list to a worksheet)
    Worksheet WS = (Worksheet)WB.Worksheets.Add(); //this command adds worksheets
    Range R = WS.Range["A1"];  //or any other cell you like
    //now for cells
    for (int i = 0; i < YourStringList.Count; i++) //I believe you can manage to separate the lists yourself
    {
        R.Offset[i, 0].Value = YourStringList[i];
    }
End of the loop