-3

I want to export my arraylist to excel. I added my arrays for example with the name _myMAXIMUM to my arraylist.

list.Add(_myMAXIMUM);

My array myMAXIMUM has double values ->

  • 34.43
  • 234.34
  • 234.5667
  • 3.6 ...

So my problem is i dont understand the loop. I can not program a loop or a foreach to export all my values of my arraylist to excel. I searched for solutions but it does not work ... i am new in c# so its very hard for me. The other part of my code should be ok i think. Thank you in advance.

This is my target

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;
Ph MO
  • 19
  • 1
  • 6
  • 1
    if you dont understand how for loop works then you need to read more books first and try again. You won't get far without it. – Steve Jan 28 '19 at 15:27
  • Either create Non-CLS base 1 arrays with Array.CreateInstance and dump immediately to an Excel sheet(fast), or by looping through Excel cells one at a time you can allocate each value from the array: foreach arr in List -> write downwards in Excel column. You cannot just export an arraylist of arrays. You must export each array one at a time.... – MacroMarc Jan 28 '19 at 15:27
  • Also, your "xlApp.ScreenUpdating = true;" at the end will error out, since you've closed and Released the xlApp object. – Frank Ball Jan 28 '19 at 15:51

1 Answers1

0

You don't need the nested For loops at all. Just call:

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

See PunkUnicorn's answer here: Excel.Worksheet.Cells[row,col] = "=Formula" vs. Range.set_Value(Missing.Value, arrayFormulas)

Frank Ball
  • 1,039
  • 8
  • 15