【问题标题】:Arraylist to excel i dont understand the for-loopArraylist to excel 我不明白 for 循环
【发布时间】:2023-03-28 22:29:01
【问题描述】:

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

list.Add(_myMAXIMUM);

我的数组 myMAXIMUM 有双值 ->

  • 34.43
  • 234.34
  • 234.5667
  • 3.6 ...

所以我的问题是我不理解循环。我无法编写循环或 foreach 来将我的数组列表的所有值导出到 excel。我搜索了解决方案,但它不起作用......我是 c# 的新手,所以对我来说很难。我认为我的代码的另一部分应该没问题。提前谢谢你。

这是我的目标

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;

【问题讨论】:

  • 如果您不了解 for 循环的工作原理,那么您需要先阅读更多书籍,然后再试一次。没有它,你不会走得太远。
  • 要么使用 Array.CreateInstance 创建非 CLS 基数为 1 的数组并立即转储到 Excel 工作表(快速),或者通过一次一个循环遍历 Excel 单元格,您可以从数组中分配每个值: foreach arr in List -> 在 Excel 列中向下写入。您不能只导出数组的arraylist。您必须一次导出一个数组......
  • 另外,你的“xlApp.ScreenUpdating = true;”最后会出错,因为您已经关闭并释放了 xlApp 对象。

标签: c# excel arraylist


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多