使用EPPLUS将LoadFromCollection的List或数组添加到行范围

时间:2022-01-21 16:44:13

Trying to add a List or array to a row in excel with EPPLUS using ExcelRange and LoadFromCollection but is filling a column instead of a a row. code :

尝试使用ExcelRange和LoadFromCollection使用EPPLUS将列表或数组添加到excel中的行,但是填充的是列而不是一行。代码:

List<string> cabeceras = new List<string>();
        cabeceras.Add("Fecha");
        foreach (ValidationParameterData estacion in informacionSeleccion.Parameters) {
            foreach (string parameter in estacion.Parameters) {

                cabeceras.Add(estacion.Station + parameter);
                cabeceras.Add("L");
            } 
        }
        string[] vals = cabeceras.ToArray();

        using (ExcelRange rng = ws.Cells[4,1,4,cabeceras.Count])
        {
            rng.Style.Font.Size = 16;
            rng.Style.Font.Bold = true;
            rng.LoadFromCollection(vals);//or cabeceras directly 
        }

tried with both array and list also with:

同时尝试使用数组和列表:

ExcelRange rng = ws.Cells["A4:M4"]  

same thing happens, column A is filled from top to bottom instead of filling the row from left to right, what am I doing wrong? is there another function to do this?

同样的事情发生了,A列从上到下填充而不是从左到右填充行,我做错了什么?还有其他功能吗?

Thanks and regards

感谢致敬

1 个解决方案

#1


1  

Here is how I solved the problem. See underneath this code for my trials and tribulations re the class Range.

这是我解决问题的方法。请参阅此代码的下方以了解我的试验和磨难。

using System;
using OfficeOpenXml;
using System.Collections.Generic;
using System.IO;

namespace eeplusPractice
{
    class Program
    {
        static void Main(string[] args)
        {
            FileInfo newFile = new FileInfo(@"C:\Users\Evan\Desktop\new.xlsx");
            ExcelPackage pkg = new ExcelPackage(newFile);
            ExcelWorksheet wrksheet = pkg.Workbook.Worksheets[0];

            List<string> cabeceras = new List<string>();

            for (int x = 1; x < 5; x++)
            {
                cabeceras.Add("HELLO");
            }

            int row = 4;

            for(int col = 1; col <= cabeceras.Count; col++)
            {
                int counter = col;
                wrksheet.Cells[row, col].Value = cabeceras[counter];
            }

            pkg.Save();
        }
    }
}

I tried to iterate through rng object using foreach loop but only one class was able to run and that did the same as the problem you encountered above. It must not treat each cell in range as distinct object that can be accessed. Method

我尝试使用foreach循环遍历rng对象,但只有一个类能够运行,并且与上面遇到的问题相同。它不能将范围内的每个单元格视为可以访问的不同对象。方法

使用EPPLUS将LoadFromCollection的List或数组添加到行范围

I may write a method LoadRangeHorizontal if I get the time, but for now up top should work OK.

如果我有时间,我可以写一个方法LoadRangeHorizo​​ntal,但是现在up应该可以正常工作。

GL lemme know if it works.

GL lemme知道它是否有效。

#1


1  

Here is how I solved the problem. See underneath this code for my trials and tribulations re the class Range.

这是我解决问题的方法。请参阅此代码的下方以了解我的试验和磨难。

using System;
using OfficeOpenXml;
using System.Collections.Generic;
using System.IO;

namespace eeplusPractice
{
    class Program
    {
        static void Main(string[] args)
        {
            FileInfo newFile = new FileInfo(@"C:\Users\Evan\Desktop\new.xlsx");
            ExcelPackage pkg = new ExcelPackage(newFile);
            ExcelWorksheet wrksheet = pkg.Workbook.Worksheets[0];

            List<string> cabeceras = new List<string>();

            for (int x = 1; x < 5; x++)
            {
                cabeceras.Add("HELLO");
            }

            int row = 4;

            for(int col = 1; col <= cabeceras.Count; col++)
            {
                int counter = col;
                wrksheet.Cells[row, col].Value = cabeceras[counter];
            }

            pkg.Save();
        }
    }
}

I tried to iterate through rng object using foreach loop but only one class was able to run and that did the same as the problem you encountered above. It must not treat each cell in range as distinct object that can be accessed. Method

我尝试使用foreach循环遍历rng对象,但只有一个类能够运行,并且与上面遇到的问题相同。它不能将范围内的每个单元格视为可以访问的不同对象。方法

使用EPPLUS将LoadFromCollection的List或数组添加到行范围

I may write a method LoadRangeHorizontal if I get the time, but for now up top should work OK.

如果我有时间,我可以写一个方法LoadRangeHorizo​​ntal,但是现在up应该可以正常工作。

GL lemme know if it works.

GL lemme知道它是否有效。