Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Filling excel cells with strings using an array or list

6 REPLIES 6
Reply
Message 1 of 7
dem1st
653 Views, 6 Replies

Filling excel cells with strings using an array or list

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);
                }

 

 

 

 

Labels (4)
6 REPLIES 6
Message 2 of 7
ridaabderrahmane
in reply to: dem1st

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

Message 3 of 7
dem1st
in reply to: ridaabderrahmane

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]?

Message 4 of 7
ridaabderrahmane
in reply to: dem1st

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.

Message 5 of 7
dem1st
in reply to: ridaabderrahmane

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.

Message 6 of 7
genosyde
in reply to: dem1st

_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;
}

Message 7 of 7
mhannonQ65N2
in reply to: dem1st

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

_elementsDB[row-1].Id

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Rail Community


Autodesk Design & Make Report