简体   繁体   English

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

[英]Add List or array with LoadFromCollection to row range using EPPLUS

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 aa row. 尝试使用ExcelRange和LoadFromCollection使用EPPLUS将列表或数组添加到excel中的行,但是填充的是列而不是行。 code : 代码:

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? 同样的事情发生,A列从上到下填充而不是从左到右填充行,我做错了什么? is there another function to do this? 还有其他功能吗?

Thanks and regards 谢谢并恭祝安康

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. 我尝试使用foreach循环遍历rng对象,但只有一个类能够运行,并且与上面遇到的问题完全相同。 It must not treat each cell in range as distinct object that can be accessed. 它不能将范围内的每个单元格视为可以访问的不同对象。 Method 方法

在此输入图像描述

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知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM