1

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 populate 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);
            }
fysharp
  • 101
  • 7

1 Answers1

1

When you use the method .ToElements() it returns a IList<Element> that you can convert it later, you cannot directly assign its result to List<Element>, you have to use LINQ to convert, using .ToElements().ToList()

If you don't have it yet, be sure to add using System.Linq; to the top of your code.

Anyway, there's no need to convert to List<Element> here, try the code below:

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(); // <---- IF YOU ALREADY DECLARED _elementsDB BEFORE, YOU CAN'T HAVE THIS HERE TOGETHER

List <string> _elementsNameList = new List<string>(); // <-- YOU'VE MISSED () HERE
foreach (Element e in _elementsDB)
{
    _elementsNameList.Add(e.Name); // <-- HERE YOU HAVE TO ADD IT TO THE LIST, NOT ASSIGN TO THE LIST, YOU CANNOT ASSIGN A string TO A List<string>, YOU HAVE TO ADD
}

//Sorting your list would be
_elementsNameList = _elementsNameList.OrderBy(q => q).ToList();

//...... Write here if you have any code before writing the Excel

try
{
    WriteXLS("YOU EXCEL FILE PATH HERE", "YOUR WORKSEET NAME HERE", _elementsNameList, 2); // <--- METHOD SOWN IN THE CODE BELOW
}
catch(Exception e)
{
    TaskDialog.Show("Error", e.Message);
}

For writing to an existing Excel file you can use the method bellow:

private void WriteXLS(string filePath, string workSheetName, List<string> elementsNamesList, int startRow = 1, int col = 1)
{
    FileInfo existingFile = new FileInfo(filePath);
    using (ExcelPackage package = new ExcelPackage(existingFile))
    {
        ExcelWorksheet ws = GetWorkSheet(package, workSheetName);
        int maxRows = elementsNamesList.Count;
        for (int row = startRow; row <= maxRows; row++)
        {
            ws.Cells[row, col].Value = elementsNamesList[row];
        }
    }
}

And, before running it, be sure to have you Excel file closed, it doesn't work if the file is opened.