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

时间:2013-04-15 20:41:16

标签: c# excel c#-4.0 excel-interop

我有一个带有一些值的List,我还有一个TextBox,我必须写一个数字,然后我需要构建一个带有许多Sheets的Excel,其值来自List。换句话说,例如:List有1000个值,然后我在TextBox中输入100,所以我需要生成一个包含多个工作表的Excel文件,因为值在List中迭代在TextBox中输入的值,在这种情况下将是一个包含10张纸的Excel文件,每张纸包含100个单元格。很明显?我如何使用Microsoft.Office.Interop.Excel执行此操作?

1 个答案:

答案 0 :(得分:0)

对于工作表:

//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