Example #1
0
        private static void FormatSheetForSet2(Excel.Worksheet sheet, SetViewModel obj)
        {
            // formatting All sheet
            sheet.PageSetup.PrintGridlines = false;
            sheet.PageSetup.Orientation    = Excel.XlPageOrientation.xlLandscape;
            sheet.PageSetup.PaperSize      = Excel.XlPaperSize.xlPaperA4;
            sheet.PageSetup.RightFooter    = "Дата: &DD Стр &PP из &NN";
            sheet.PageSetup.RightHeader    = "Исследование " + obj.Set.First().Project + ", сет № " + obj.Set.First().Set + " - " + obj.Set.First().TestMethod +
                                             " - " + obj.Set.First().AB;
            sheet.PageSetup.Zoom         = false;
            sheet.PageSetup.LeftHeader   = "НИИ Антимикробной химиотерапии";
            sheet.PageSetup.TopMargin    = 50;
            sheet.PageSetup.BottomMargin = 50;
            sheet.PageSetup.HeaderMargin = 20;
            sheet.PageSetup.FooterMargin = 20;
            sheet.PageSetup.RightMargin  = 10;
            sheet.PageSetup.LeftMargin   = 50;
            sheet.PageSetup.Order        = Excel.XlOrder.xlOverThenDown;

            // Formatting Set Number
            sheet.Range[sheet.Cells[1, 1], sheet.Cells[1, 3]].Merge();
            FormatHeaderText1(sheet.Range[sheet.Cells[1, 1], sheet.Cells[1, 3]]);
            // Formatting Footer cell
            sheet.Range[sheet.Cells[3 + obj.Set.First().MOList.Count + obj.Set.First().ControlMOList.Count, 1], sheet.Cells[3 + obj.Set.First().MOList.Count + obj.Set.First().ControlMOList.Count, 3]].Merge();
            FormatHeaderText1(sheet.Range[sheet.Cells[1, 1], sheet.Cells[1, 3]]);

            //Formatting table with MO
            FormatTableCells2(sheet.Range[sheet.Cells[1, 1], sheet.Cells[3 + obj.Set.First().MOList.Count + obj.Set.First().ControlMOList.Count, obj.Set.Count + 3]]);

            //Formatting AB Header
            sheet.Range[sheet.Cells[1, 1], sheet.Cells[1, obj.Set.Count() + 3]].RowHeight   = 18;
            sheet.Range[sheet.Cells[2, 1], sheet.Cells[2, obj.Set.Count() + 3]].RowHeight   = 80;
            sheet.Range[sheet.Cells[2, 4], sheet.Cells[2, obj.Set.Count() + 3]].Orientation = 90;

            sheet.Range[sheet.Cells[obj.Set.First().MOList.Count + obj.Set.First().ControlMOList.Count + 3
                                    , 4], sheet.Cells[obj.Set.First().MOList.Count + obj.Set.First().ControlMOList.Count + 3, obj.Set.Count() + 3]].RowHeight = 80;
            sheet.Range[sheet.Cells[obj.Set.First().MOList.Count + obj.Set.First().ControlMOList.Count + 3
                                    , 4], sheet.Cells[obj.Set.First().MOList.Count + obj.Set.First().ControlMOList.Count + 3, obj.Set.Count() + 3]].Orientation = 90;

            sheet.Range[sheet.Cells[2, 1], sheet.Cells[obj.Set.First().MOList.Count + obj.Set.First().ControlMOList.Count + 3, 1]].ColumnWidth = 5;
            sheet.Range[sheet.Cells[2, 2], sheet.Cells[obj.Set.First().MOList.Count + obj.Set.First().ControlMOList.Count + 3, 2]].ColumnWidth = 9;
            sheet.Range[sheet.Cells[2, 3], sheet.Cells[obj.Set.First().MOList.Count + obj.Set.First().ControlMOList.Count + 3, 3]].ColumnWidth = 15;

            FormatHeaderControlMOText2(sheet.Range[sheet.Cells[obj.Set.First().MOList.Count + 3, 1], sheet.Cells[obj.Set.First().MOList.Count + obj.Set.First().ControlMOList.Count + 3, 3 + obj.Set.Count]]);



            sheet.Cells[obj.Set.First().MOList.Count + obj.Set.First().ControlMOList.Count + 7, 2] = "Проверил:";
        }
Example #2
0
        private static object[,] PrepareListForSet2(SetViewModel obj)
        {
            var rowsCount    = obj.Set.First().MOList.Count + obj.Set.First().ControlMOList.Count + 3;
            var columnsCount = obj.Set.Count + 3;

            object[,] data = new object[rowsCount, columnsCount];

            data[0, 0] = "Сет № " + obj.Set.First().Set;
            data[1, 0] = "№";
            data[1, 1] = "Муз. №";
            data[1, 2] = "МО";


            for (int i = 0; i < obj.Set.Count; i++)
            {
                data[0, 3 + i] = obj.Set[i].AB;
                data[1, 3 + i] = obj.Set[i].MICList.First().ToString() + " - " + obj.Set[i].MICList.Last().ToString();
                data[obj.Set.First().MOList.Count + obj.Set.First().ControlMOList.Count + 2, 3 + i] = obj.Set.First().ControlMICList.First().ToString() + " - " +
                                                                                                      obj.Set[i].ControlMICList.Last().ToString();
            }

            for (int i = 0; i < obj.Set.First().MOList.Count; i++)
            {
                data[i + 2, 0] = obj.Set.First().MOList[i].Number;
                data[i + 2, 1] = obj.Set.First().MOList[i].MuseumNumber;
                data[i + 2, 2] = obj.Set.First().MOList[i].MO;
            }
            for (int i = 0; i < obj.Set.First().ControlMOList.Count; i++)
            {
                data[i + 2 + obj.Set.First().MOList.Count, 0] = obj.Set.First().ControlMOList[i].Number;
                data[i + 2 + obj.Set.First().MOList.Count, 1] = obj.Set.First().ControlMOList[i].MuseumNumber;
                data[i + 2 + obj.Set.First().MOList.Count, 2] = obj.Set.First().ControlMOList[i].MO;
            }


            return(data);
        }
Example #3
0
        public static string GetExcelDocumentSet(SetViewModel obj, string filePath, int setType)
        {
            Excel.Application ExcelApp;
            Excel.Worksheet   ExcelSheet;
            Excel.Workbook    ExcelWorkbook;
            Excel.Workbooks   ExcelWorkbooks;
            Excel.Range       ExcelRange;
            int     rowsCount;
            int     columnsCount;
            dynamic data;

            ExcelApp                = CreateExcelObj();
            ExcelWorkbooks          = ExcelApp.Workbooks;
            ExcelApp.ScreenUpdating = false;
            ExcelApp.DisplayAlerts  = false;
            ExcelWorkbook           = ExcelWorkbooks.Add();

            try
            {
                if (String.IsNullOrEmpty(filePath))
                {
                    filePath = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)).FullName;
                    //if (Environment.OSVersion.Version.Major >= 6)
                    //{
                    //    filePath = Directory.GetParent(filePath).FullName;
                    //}
                }
                filename = obj.Set.First().Project + " - Сет " + obj.Set.First().Set + " - " + obj.Set.First().TestMethod + ".xlsx";



                switch (setType)
                {
                case 1:

                    foreach (var itemSet in obj.Set)
                    {
                        ExcelSheet = ExcelWorkbook.Sheets.Add();

                        rowsCount    = itemSet.MOList.Count + 8 + itemSet.ControlMOList.Count + 1;
                        columnsCount = itemSet.MICList.Count + 5;

                        ExcelRange =
                            ExcelSheet.Range[ExcelSheet.Cells[1, 1], ExcelSheet.Cells[rowsCount, columnsCount]];
                        if (
                            itemSet.AB.Length > 30)
                        {
                            ExcelSheet.Name = itemSet.AB.Substring(0, 30).Replace("/", "|").Replace("\\", "|");
                        }
                        else
                        {
                            ExcelSheet.Name = itemSet.AB.Replace("/", "|").Replace("\\", "|");
                        }

                        data = PrepareListForSet1(itemSet);

                        ExcelRange.Value = data;
                        FormatSheetForSet1(ExcelSheet, itemSet);

                        Marshal.ReleaseComObject(ExcelRange);
                        Marshal.ReleaseComObject(ExcelSheet);
                    }
                    break;

                case 2:
                    ExcelSheet   = ExcelWorkbook.Sheets.Add();
                    rowsCount    = obj.Set.First().MOList.Count + obj.Set.First().ControlMOList.Count + 3;
                    columnsCount = obj.Set.Count + 3;

                    ExcelRange = ExcelSheet.Range[ExcelSheet.Cells[1, 1], ExcelSheet.Cells[rowsCount, columnsCount]];

                    ExcelSheet.Name = obj.Set.First().Project + " - Сет № " + obj.Set.First().Set;

                    data = PrepareListForSet2(obj);

                    ExcelRange.Value = data;
                    FormatSheetForSet2(ExcelSheet, obj);
                    Marshal.ReleaseComObject(ExcelRange);
                    Marshal.ReleaseComObject(ExcelSheet);
                    break;

                default:
                    break;
                }
                ExcelWorkbook.SaveAs();
                ExcelWorkbook.SaveAs(filePath + "\\" + filename, Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);


                //while (Marshal.ReleaseComObject(ExcelWorkbook) > 0)
                //{ }
                //while (Marshal.ReleaseComObject(ExcelWorkbooks) > 0)
                //{ }


                //ExcelApp.Quit();

                //while (Marshal.ReleaseComObject(ExcelApp) > 0)
                //{ }

                return(filePath + "\\" + filename);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                while (Marshal.ReleaseComObject(ExcelWorkbook) > 0)
                {
                }
                while (Marshal.ReleaseComObject(ExcelWorkbooks) > 0)
                {
                }


                ExcelApp.Quit();

                while (Marshal.ReleaseComObject(ExcelApp) > 0)
                {
                }
                GC.Collect();
            }
        }