添加项目到多行Excel c#

本文关键字:Excel 项目 添加 | 更新日期: 2023-09-27 18:13:50

我有一个windows窗体应用程序。在这个应用程序中,我想导出我的一些数据在我的表格excel。我已经弄清楚了如何创建一个Excel文件,并存储了一些值。然而,有一件事我不确定的是,如果有一种方法来添加项目excel行使用for循环(找出行和列根据你有多少数据)?因为如果你想添加一个项目列表,比如一个一个地添加1000个项目,这是非常低效的。

例如:每次只添加一项到特定单元格中,我们可以这样做:

xlSomeDetail.Cells[1,1] = "Teacher Id";
xlSomeDetail.Cells[1,2] = "first name";
xlSomeDetail.Cells[1,3] = "Last Name";
xlSomeDetail.Cells[1,4] = "Email";
xlSomeDetail.Cells[1,5] = "Salary";

现在,如果我想要这样做,而不是逐个添加行,有没有类似的东西?

//declare aRange variable
Excel.Range aRange;
 aRange = (Excel.Range)xlSomeDetail.get_Range("A1", "M1");
 //something like this? I am not sure 
for (int i = 1; i< aRange.Rows; i++{
      xlSomeDetail.Cell[1, i+ 1] = //somestring?
}

有更好的方法吗?

添加项目到多行Excel c#

尝试创建多个Xl对象在此之前,根据您的需求将数据拆分为多个数据表

         [DllImport("user32")]
           private static extern bool GetWindowThreadProcessId(int hWnd, out int id);

           for(i=0;i<2;i++)
                    {
                     oXL = new Microsoft.Office.Interop.Excel.Application();
                                    oXL.SheetsInNewWorkbook = 1;
                                    oXL.Visible = false;
                                    //Get a new workbook.
                                    oWB = (Excel.Workbook)(oXL.Workbooks.Add(Type.Missing));
      oSheet = (Excel.Worksheet)oWB.Worksheets.get_Item(1);+
             oSheet.Name = "Summary Report";//Change the name as per your requirment
                             ExcelRowsCount = 2;
                      for (int RowsCount = 0; RowsCount < dt.Rows.Count; RowsCount++)
                                {
                     for (int ColumnsCount = 0; ColumnsCount < dt.Columns.Count; ColumnsCount++)
                                    {
                                        if (RowsCount.Equals(0) && ColumnsCount.Equals(0))
                                            oSheet.Cells[2, 1] = dt.Rows[RowsCount][ColumnsCount].ToString();
                                        else
                                            oSheet.Cells[ExcelRowsCount + RowsCount, ColumnsCount + 1] = dt.Rows[RowsCount][ColumnsCount].ToString();
                                    }
oXL.Visible = false;
        oXL.UserControl = false;
         oXL.DisplayAlerts = false;
        oXL.ActiveWorkbook.SaveAs(FileName, Excel.XlFileFormat.xlWorkbookNormal, "", "", false, false, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
         oWB.Close(false, Type.Missing, Type.Missing);
         oXL.Workbooks.Close();
         oXL.Quit();
           int ExcelID;
                        GetWindowThreadProcessId(oXL.Hwnd, out ExcelID);
                        Process XLProcess = Process.GetProcessById(ExcelID);
                        Marshal.ReleaseComObject(oSheet);
                        Marshal.ReleaseComObject(oWB);
                        Marshal.ReleaseComObject(oXL);
                        XLProcess.Kill();
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                                } 
                }