Issue with exporting shedule were Element ID is messing

-its exporting but the element ID is coming but not aligned to the same row of the element, i tried with the AI but still unable to fix

private void ExportButton_Click(object sender, RoutedEventArgs e)
{
    ViewSchedule schedule = GetSelectedSchedule();
    if (schedule == null)
    {
        File.AppendAllText(@"C:\Temp\IMPexcel.log", "Export failed: No valid schedule selected\n");
        return;
    }

    SaveFileDialog saveDialog = new SaveFileDialog
    {
        Filter = "Excel Files (*.xlsx)|*.xlsx",
        DefaultExt = "xlsx",
        FileName = TruncateSheetName(schedule.Name) + ".xlsx"
    };
    if (saveDialog.ShowDialog() != true)
    {
        File.AppendAllText(@"C:\Temp\IMPexcel.log", "Export cancelled: No file selected\n");
        return;
    }

    try
    {
        using (var workbook = new XLWorkbook())
        {
            var worksheet = workbook.Worksheets.Add(TruncateSheetName(schedule.Name));
            TableData tableData = schedule.GetTableData();
            TableSectionData bodyData = tableData.GetSectionData(SectionType.Body);

            int headerRow = 3; // Header at row 3 (A3, B3, ...)
            int startRow = 4;  // Data starts at row 4 (A4, B4, ...)

            // Write headers: Element ID and parameter names
            worksheet.Cell(headerRow, 1).Value = "Element ID";
            int headerCols = bodyData?.NumberOfColumns ?? 0;
            for (int col = 0; col < headerCols; col++)
            {
                var field = schedule.Definition.GetField(col);
                string paramName = field?.GetName() ?? "";
                worksheet.Cell(headerRow, col + 2).Value = paramName;
            }
            worksheet.Row(headerRow).Style.Font.Bold = true;

            // Get all elements in the schedule, in schedule order
            var elements = new FilteredElementCollector(_doc, schedule.Id)
                .WhereElementIsNotElementType()
                .ToElements();

            int nRows = bodyData?.NumberOfRows ?? 0;
            for (int i = 0; i < nRows && i < elements.Count; i++)
            {
                var elem = elements[i];
                worksheet.Cell(startRow + i, 1).Value = elem.Id.IntegerValue;
                for (int j = 0; j < headerCols; j++)
                {
                    worksheet.Cell(startRow + i, j + 2).Value = schedule.GetCellText(SectionType.Body, i, j);
                }
            }

            worksheet.Columns().AdjustToContents();
            workbook.SaveAs(saveDialog.FileName);
            File.AppendAllText(@"C:\Temp\IMPexcel.log", $"Export completed: {saveDialog.FileName}\n");
            MessageBox.Show("File exported successfully!", "Export Complete", MessageBoxButton.OK, MessageBoxImage.Information);
            this.Close();
        }
    }
    catch (Exception ex)
    {
        File.AppendAllText(@"C:\Temp\IMPexcel.log", $"Export error: {ex.Message}\n");
    }
}
1 Like

@vikas.kpoojary

i can`t help directly. just cross referencing to a similar issue. I had also to import/export data based on GUIDs.

How to extract Revit ID from schedule xls? - Revit API - pyRevit Forums

1 Like

I believe schedules return elements via collectors in order of elementid, whereas if you sort your revit schedule that will modify the order elements will be returned.

If you want elementids but the same order im fairly sure you need to programatically create filtering/sorting logic. Hope to be proven wrong though, as it would make my bim-linkesque tools much more streamlined at work!

yes, that’s correct
if i remove sort in schedule it works fine. but i am not able to fix, my brain is lagging now😜

it was useful thanks,

1 Like

Hi,

there’s a workaround by temporarily using tablesectionData.RemoveRow(nIndex) and FilteredElementCollector(document, scheduleleview.Id) , but beware of elements dependent on other elements

4 Likes