예제 #1
0
        private void WriteTable(Excel.Workbook wb, RevitDataTable table)
        {
            int margin     = 1; // количество пустых строк между секциями
            int currentRow = 1;

            Excel.Worksheet ws;
            switch (opt.SplittingData)
            {
            case SplitDataOptions.NoSplit:
                ws      = wb.Sheets.Add();
                ws.Name = SafeName(table.Name);
                if (opt.ShowHeaders)
                {
                    FillHeader(ws, table);
                }
                currentRow = opt.ShowHeaders ? currentRow + 1 : currentRow;
                foreach (var sect in table.Sections)
                {
                    FillRange(ws, currentRow, 1, sect);
                    currentRow += sect.Height;
                }
                break;

            case SplitDataOptions.OneSheet:
                ws      = wb.Sheets.Add();
                ws.Name = SafeName(table.Name);
                if (opt.ShowHeaders)
                {
                    FillHeader(ws, table);
                }
                currentRow = opt.ShowHeaders ? currentRow + 1 : currentRow;
                foreach (var sect in table.Sections)
                {
                    Excel.Range range = ws.Range[ws.Cells[currentRow, 1], ws.Cells[currentRow, sect.Width]];
                    range.Merge();
                    range.Value          = sect.Name;
                    range.Font.Bold      = true;
                    range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightSlateGray);

                    FillRange(ws, currentRow + 1, 1, sect);
                    currentRow += sect.Height + 1 + margin;
                }
                break;

            case SplitDataOptions.MultipleSheet:
                currentRow = opt.ShowHeaders ? currentRow + 1 : currentRow;
                foreach (var sect in table.Sections)
                {
                    ws      = wb.Sheets.Add();
                    ws.Name = SafeName(table.Name + "_" + sect.Name);
                    if (opt.ShowHeaders)
                    {
                        FillHeader(ws, table);
                    }
                    FillRange(ws, currentRow, 1, sect);
                }
                break;
            }
        }
예제 #2
0
        private void FillHeader(Excel.Worksheet ws, RevitDataTable table)
        {
            var headerRow = table.Header;

            if (headerRow != null && headerRow.Width > 0)
            {
                Excel.Range range = ws.Range[ws.Cells[1, 1],
                                             ws.Cells[1, headerRow.Width]];
                range.Value          = headerRow.RowValues;
                range.Font.Bold      = true;
                range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
            }
        }
예제 #3
0
        private void ExcelExport(string path, List <RevitDataTable> tables)
        {
            Excel.Application excelApp = new Excel.Application();
            excelApp.DisplayAlerts = false;
            Excel.Workbook wb;
            string         savepath;

            switch (opt.SplittingFile)
            {
            case SplitFileOptions.MultipleFiles:
                foreach (var table in tables)
                {
                    wb = excelApp.Workbooks.Add();
                    WriteTable(wb, table);
                    savepath = Path.Combine(path, table.Name + ".xlsx");
                    SafeSave(wb, savepath);
                }
                break;

            case SplitFileOptions.OneFileMultiSheet:
                wb = excelApp.Workbooks.Add();
                foreach (var table in tables)
                {
                    WriteTable(wb, table);
                }
                SafeSave(wb, path);
                break;

            case SplitFileOptions.OneFileOneSheet:
                wb = excelApp.Workbooks.Add();
                RevitDataTable unitedTable;
                if (opt.MergeSections)
                {
                    unitedTable = RevitDataTable.Merge(tables);
                }
                else
                {
                    unitedTable = RevitDataTable.Join(tables);
                }
                WriteTable(wb, unitedTable);
                SafeSave(wb, path);
                break;
            }
            excelApp.Quit();
        }
예제 #4
0
        private RevitDataTable GetRevitDataTable(ViewSchedule schedule)
        {
            ScheduleDefinition scheduleDefinition = schedule.Definition;
            TableSectionData   bodyData           = schedule.GetTableData().GetSectionData(SectionType.Body);
            TableSectionData   headerData         = schedule.GetTableData().GetSectionData(SectionType.Header);

            RevitDataTable   dataTable   = new RevitDataTable(schedule.Name);
            RevitDataSection dataSection = new RevitDataSection("Без названия");

            dataTable.Sections.Add(dataSection);
            // заголовки
            int start_i;

            if (scheduleDefinition.ShowHeaders)
            {
                RevitDataRow header = new RevitDataRow(0);
                for (int col = 0; col < bodyData.NumberOfColumns; col++)
                {
                    RevitDataCell dataCell = new RevitDataCell(
                        schedule.GetCellText(SectionType.Body, 0, col),
                        bodyData.GetCellType(0, col),
                        bodyData.GetCellParamId(col));
                    header.Cells.Add(dataCell);
                }
                start_i          = 1;
                dataTable.Header = header;
            }
            else
            {
                start_i = 0;
            }
            //ищем секции
            for (int row = start_i; row < bodyData.NumberOfRows; row++)
            {
                if (bodyData.GetCellType(row, 0) == CellType.Text &&
                    bodyData.GetMergedCell(row, 0).Right == bodyData.LastColumnNumber)
                {
                    string header = bodyData.GetCellText(row, 0);
                    header      = string.IsNullOrEmpty(header) ? "Без названия" : header;
                    dataSection = new RevitDataSection(header);
                    dataTable.Sections.Add(dataSection);
                    continue;
                }

                RevitDataRow dataRow = new RevitDataRow(row);
                for (int col = 0; col < bodyData.NumberOfColumns; col++)
                {
                    RevitDataCell dataCell = new RevitDataCell(
                        schedule.GetCellText(SectionType.Body, row, col),
                        bodyData.GetCellType(row, col),
                        bodyData.GetCellParamId(col));
                    dataRow.Cells.Add(dataCell);
                }
                dataSection.Rows.Add(dataRow);
            }
            if (dataTable["Без названия"].Rows.Count == 0)
            {
                dataTable.Sections.Remove(dataTable["Без названия"]);
            }
            return(dataTable);
        }