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