Filling excel cells with strings using an array or list

dem1st
Enthusiast
Enthusiast

Filling excel cells with strings using an array or list

dem1st
Enthusiast
Enthusiast

The goal is to fill excel cells with elements names. Using EPPlus.

_elementsDB gives just "Autodesk.Revit.DB.Wall".

 

 

 

 

int col = 1;
for ( int row=2; row < _elementsDB.Count; row++ )
{
ws.Cells[row, col].Value = _elementsDB;
}

 

 

 

 

Tying to fill either an array or list. Nothing works.

 

 

 

 FilteredElementCollector collector = new FilteredElementCollector(doc);
    IList<Element> _elementsDB = collector.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements();
    List<Element> _elementsDB = collector.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements();

    //First Option
    string[] _elementsNameArr = new string[] { };
    foreach (Element e in _elementsDB)
    {

        _elementsNameArr = e.Name;
    }

    //Second Option
    List <string> _elementsNameList = new List<string>;
    foreach (Element e in _elementsDB)
    {

        _elementsNameList = e.Name;
    }

 

 

 

 

Also tried to create a sorted list, didn't work either. Shows up an excxeption System.Argument.Exception "A record with such a key already exists".

 

 

 

 

SortedList<string, Element> _elementNameSorted = new SortedList<string, Element>();
    
                foreach (Element e in _elementsDB)
                {
                    _elementNameSorted.Add(e.Name,e);
                }

 

 

 

 

0 Likes
Reply
701 Views
6 Replies
Replies (6)

ridaabderrahmane
Enthusiast
Enthusiast

Hi there, _elementsDB is a list of elements you cannot assign it to a the value of the cell, try instead:

int col = 1;
for ( int row=2; row < _elementsDB.Count; row++ )
{
ws.Cells[row, col].Value = _elementsDB[row-1].Name;
}

Hope this helps

dem1st
Enthusiast
Enthusiast

That made the difference, thanks a lot! Here's a screenshot:

dem1st_0-1634030225502.png

 

Could you give more sense about [row]?

To my understanding by adding [row] or [row-1] to _elementsDB now we operate not with a list but with a single particular element,  ( just like e in that for each statement (foreach Elelent e in _elementsDB) ), right?

 

Also, we did you use [row-1] and not [row]?

0 Likes

ridaabderrahmane
Enthusiast
Enthusiast

Hi in the excel sheets the rows starts from one. so the first row is number one where in the list of elements the first element is number zero so you will need to do [row-1] or [row-2] depending on what you need.

dem1st
Enthusiast
Enthusiast

Makes sense! How to write for statement the right way so that every new row we gets new ids and not just one (as of right now) ?

 

ICollection<ElementId> _elementsIds = collector.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElementIds();

int col2 = 2;
for (int row = 2; row < _elementsIds.Count; row++)
{
ws.Cells[row, col2].Value = _elementsIds;
}

 Indexing in this case is not applicable.

0 Likes

genosyde
Advocate
Advocate

_elementsIds is a Collection of class ElementID.
As above, do not directly assign values to cells.
Since you used the for statement, you can access it using the index of the collection array.
For example _elementsIds[row number]
Also, when using Excel, it is recommended to use the XML-based Excel API.
I use ClosedXML to create Excel-related programs.
If you use Microsoft's Excel API, you must carefully check whether the target target is X86 or X64.
There are many errors and it is more difficult to deal with.

int col2 = 2;
// Seeing that col2 starts at number 2, number 1 must be the column name.

for (int row = 2; row < _elementsIds.Count; row++)
{
ws.Cells[row, col2].Value = _elementsIds[row - 2].IntegerValue;
}

mhannonQ65N2
Advocate
Advocate

You could also just read the element's Id property.

_elementsDB[row-1].Id