Ejemplo n.º 1
0
        public static void CopyCellStyle(Worksheet worksheet, string column1, int row1, string column2, int row2)
        {
            var cell1 = CellHelper.GetCell(worksheet, column1, row1);
            var cell2 = CellHelper.GetCell(worksheet, column2, row2);

            cell2.StyleIndex = cell1.StyleIndex;
        }
Ejemplo n.º 2
0
        public static void CopyCellStyle(Worksheet worksheet, string column1, int row1, Cell cell)
        {
            var cellFrom = CellHelper.GetCell(worksheet, column1, row1);

            cell.StyleIndex = cellFrom.StyleIndex;
        }
Ejemplo n.º 3
0
        public static void MergeTwoCells(Worksheet worksheet, string cell1Name, string cell2Name)
        {
            if (worksheet == null || string.IsNullOrEmpty(cell1Name) || string.IsNullOrEmpty(cell2Name))
            {
                return;
            }

            // Добавить код на случай объедения более двух ячеек
            CellHelper.CreateSpreadsheetCellIfNotExist(worksheet, cell1Name);
            CellHelper.CreateSpreadsheetCellIfNotExist(worksheet, cell2Name);

            MergeCells mergeCells;

            if (worksheet.Elements <MergeCells>().Count() > 0)
            {
                mergeCells = worksheet.Elements <MergeCells>().First();
            }
            else
            {
                mergeCells = new MergeCells();

                // Insert a MergeCells object into the specified position.
                if (worksheet.Elements <CustomSheetView>().Count() > 0)
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements <CustomSheetView>().First());
                }
                else if (worksheet.Elements <DataConsolidate>().Count() > 0)
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements <DataConsolidate>().First());
                }
                else if (worksheet.Elements <SortState>().Count() > 0)
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements <SortState>().First());
                }
                else if (worksheet.Elements <AutoFilter>().Count() > 0)
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements <AutoFilter>().First());
                }
                else if (worksheet.Elements <Scenarios>().Count() > 0)
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements <Scenarios>().First());
                }
                else if (worksheet.Elements <ProtectedRanges>().Count() > 0)
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements <ProtectedRanges>().First());
                }
                else if (worksheet.Elements <SheetProtection>().Count() > 0)
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements <SheetProtection>().First());
                }
                else if (worksheet.Elements <SheetCalculationProperties>().Count() > 0)
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements <SheetCalculationProperties>().First());
                }
                else
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements <SheetData>().First());
                }
            }

            // Create the merged cell and append it to the MergeCells collection.
            MergeCell mergeCell = new MergeCell()
            {
                Reference = new StringValue(cell1Name + ":" + cell2Name)
            };

            mergeCells.Append(mergeCell);

            worksheet.Save();
        }