擅长使用Arraylist我不了解for循环

时间:2019-01-28 15:18:35

标签: c# excel arraylist

我想将我的数组列表导出到Excel。我将名称为_myMAXIMUM的数组添加到了数组列表中。

list.Add(_myMAXIMUM);

我的数组myMAXIMUM具有双精度值->

  • 34.43
  • 234.34
  • 234.5667
  • 3.6 ...

所以我的问题是我不了解循环。我无法编程循环或foreach将我的arraylist的所有值导出到excel。我搜索了解决方案,但是它不起作用...我是C#的新手,因此对我来说很难。我认为代码的另一部分应该没问题。预先谢谢你。

这是我的目标

enter image description here

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using System.Runtime.InteropServices;
//using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;

System.Collections.ArrayList list = new System.Collections.ArrayList();

                        list.Add(_myMAXIMUM);
                        list.Add(_myMINIMUM);
                        list.Add(_mymin);
                        list.Add(_mymax);
                        list.Add(_myminID);
                        list.Add(_mymaxID);
                        list.Add(_myminSubID);
                        list.Add(_mymaxSubID);
                        int numberOfRows = 7;
                        int numofcolumns = 8;
                        Excel.Application xlApp = null;
                        Excel.Workbook xlWorkBook = null;       
                        xlApp = new Excel.Application();
                        xlWorkBook = xlApp.Workbooks.Add();
                        Excel.Worksheet newWorksheet = null;
                        newWorksheet = (Excel.Worksheet)xlApp.Application.Worksheets.Add();
                        xlApp.ScreenUpdating = false;
                        Excel.Range excelRange = newWorksheet.UsedRange;
                        for (int row = 0; row < numberOfRows; row++)
                        {
                            for (int col = 0; col < numofcolumns; col++)
                            {

                                excelRange.Cells.set_Item(row, col,list);
                            }
                        }
                        xlApp.ScreenUpdating = true;
                        // Save it as .xls
                        newWorksheet.SaveAs("D:\\Datenverarbeitung", Excel.XlFileFormat.xlExcel7);
                        xlApp.ScreenUpdating = true;
                        // Clean up
                        xlWorkBook.Close();
                        xlApp.Quit();
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                        xlApp.ScreenUpdating = true;

1 个答案:

答案 0 :(得分:0)

您根本不需要嵌套的For循环。只需致电:

excelRange.Cells.set_Item(Type.Missing, list);

在此处查看PunkUnicorn的答案:Excel.Worksheet.Cells[row,col] = "=Formula" vs. Range.set_Value(Missing.Value, arrayFormulas)