Example #1
0
        /**
         * Copies a sheet from a read-only version to the writable version.
         * Performs shallow copies
         */
        public void copySheet()
        {
            shallowCopyCells();

            // Copy the column formats
            foreach (ColumnInfoRecord cv in fromColumnFormats)
            {
                toColumnFormats.Add(cv);
            }

            // Copy the merged cells
            Range[] merged = fromMergedCells.getMergedCells();

            for (int i = 0; i < merged.Length; i++)
            {
                toMergedCells.add(new SheetRangeImpl((SheetRangeImpl)merged[i], toSheet));
            }

            try
            {
                RowRecord row    = null;
                RowRecord newRow = null;
                for (int i = 0; i < fromRows.Length; i++)
                {
                    row = fromRows[i];

                    if (row != null &&
                        (!row.isDefaultHeight() ||
                         row.isCollapsed()))
                    {
                        newRow = toSheet.getRowRecord(i);
                        newRow.setRowDetails(row.getRowHeight(),
                                             row.matchesDefaultFontHeight(),
                                             row.isCollapsed(),
                                             row.getOutlineLevel(),
                                             row.getGroupStart(),
                                             row.getStyle());
                    }
                }
            }
            catch (RowsExceededException e)
            {
                // Handle the rows exceeded exception - this cannot occur since
                // the sheet we are copying from will have a valid number of rows
                Assert.verify(false);
            }

            // Copy the horizontal page breaks
            toRowBreaks = new ArrayList(fromRowBreaks);

            // Copy the vertical page breaks
            toColumnBreaks = new ArrayList(fromColumnBreaks);

            // Copy the data validations
            if (fromDataValidation != null)
            {
                toDataValidation = new DataValidation
                                       (fromDataValidation,
                                       toSheet.getWorkbook(),
                                       toSheet.getWorkbook(),
                                       toSheet.getWorkbook().getSettings());
            }

            // Copy the charts
            sheetWriter.setCharts(fromSheet.getCharts());

            // Copy the drawings
            foreach (object o in fromDrawings)
            {
                if (o is CSharpJExcel.Jxl.Biff.Drawing.Drawing)
                {
                    WritableImage wi = new WritableImage
                                           ((CSharpJExcel.Jxl.Biff.Drawing.Drawing)o,
                                           toSheet.getWorkbook().getDrawingGroup());
                    toDrawings.Add(wi);
                    toImages.Add(wi);
                }

                // Not necessary to copy the comments, as they will be handled by
                // the deep copy of the individual cells
            }

            // Copy the workspace options
            sheetWriter.setWorkspaceOptions(fromWorkspaceOptions);

            // Copy the environment specific print record
            if (fromPLSRecord != null)
            {
                toPLSRecord = new PLSRecord(fromPLSRecord);
            }

            // Copy the button property set
            if (fromButtonPropertySet != null)
            {
                toButtonPropertySet = new ButtonPropertySetRecord(fromButtonPropertySet);
            }

            // Copy the hyperlinks
            foreach (WritableHyperlink hyperlink in fromHyperlinks)
            {
                WritableHyperlink hr = new WritableHyperlink(hyperlink, toSheet);
                toHyperlinks.Add(hr);
            }
        }
Example #2
0
        /**
         * Check all the merged cells for borders.  If the merge record has
         * borders, then we need to rejig the cell formats to take account of this.
         * This is called by the write method of the WritableWorkbookImpl, so that
         * any new XFRecords that are created may be written out with the others
         */
        public void checkMergedBorders()
        {
            Range[]   mcells        = mergedCells.getMergedCells();
            ArrayList borderFormats = new ArrayList();

            for (int mci = 0; mci < mcells.Length; mci++)
            {
                Range    range    = mcells[mci];
                Cell     topLeft  = range.getTopLeft();
                XFRecord tlformat = (XFRecord)topLeft.getCellFormat();

                if (tlformat != null &&
                    tlformat.hasBorders() == true &&
                    !tlformat.isRead())
                {
                    try
                    {
                        CellXFRecord cf1         = new CellXFRecord(tlformat);
                        Cell         bottomRight = range.getBottomRight();

                        cf1.setBorder(Border.ALL, BorderLineStyle.NONE, Colour.BLACK);
                        cf1.setBorder(Border.LEFT,
                                      tlformat.getBorderLine(Border.LEFT),
                                      tlformat.getBorderColour(Border.LEFT));
                        cf1.setBorder(Border.TOP,
                                      tlformat.getBorderLine(Border.TOP),
                                      tlformat.getBorderColour(Border.TOP));

                        if (topLeft.getRow() == bottomRight.getRow())
                        {
                            cf1.setBorder(Border.BOTTOM,
                                          tlformat.getBorderLine(Border.BOTTOM),
                                          tlformat.getBorderColour(Border.BOTTOM));
                        }

                        if (topLeft.getColumn() == bottomRight.getColumn())
                        {
                            cf1.setBorder(Border.RIGHT,
                                          tlformat.getBorderLine(Border.RIGHT),
                                          tlformat.getBorderColour(Border.RIGHT));
                        }

                        int index = borderFormats.IndexOf(cf1);
                        if (index != -1)
                        {
                            cf1 = (CellXFRecord)borderFormats[index];
                        }
                        else
                        {
                            borderFormats.Add(cf1);
                        }
                        ((WritableCell)topLeft).setCellFormat(cf1);

                        // Handle the bottom left corner
                        if (bottomRight.getRow() > topLeft.getRow())
                        {
                            // Handle the corner cell
                            if (bottomRight.getColumn() != topLeft.getColumn())
                            {
                                CellXFRecord cf2 = new CellXFRecord(tlformat);
                                cf2.setBorder(Border.ALL, BorderLineStyle.NONE, Colour.BLACK);
                                cf2.setBorder(Border.LEFT,
                                              tlformat.getBorderLine(Border.LEFT),
                                              tlformat.getBorderColour(Border.LEFT));
                                cf2.setBorder(Border.BOTTOM,
                                              tlformat.getBorderLine(Border.BOTTOM),
                                              tlformat.getBorderColour(Border.BOTTOM));

                                index = borderFormats.IndexOf(cf2);
                                if (index != -1)
                                {
                                    cf2 = (CellXFRecord)borderFormats[index];
                                }
                                else
                                {
                                    borderFormats.Add(cf2);
                                }

                                sheet.addCell(new Blank(topLeft.getColumn(),
                                                        bottomRight.getRow(), cf2));
                            }

                            // Handle the cells down the left hand side (and along the
                            // right too, if necessary)
                            for (int i = topLeft.getRow() + 1; i < bottomRight.getRow(); i++)
                            {
                                CellXFRecord cf3 = new CellXFRecord(tlformat);
                                cf3.setBorder(Border.ALL, BorderLineStyle.NONE, Colour.BLACK);
                                cf3.setBorder(Border.LEFT,
                                              tlformat.getBorderLine(Border.LEFT),
                                              tlformat.getBorderColour(Border.LEFT));

                                if (topLeft.getColumn() == bottomRight.getColumn())
                                {
                                    cf3.setBorder(Border.RIGHT,
                                                  tlformat.getBorderLine(Border.RIGHT),
                                                  tlformat.getBorderColour(Border.RIGHT));
                                }

                                index = borderFormats.IndexOf(cf3);
                                if (index != -1)
                                {
                                    cf3 = (CellXFRecord)borderFormats[index];
                                }
                                else
                                {
                                    borderFormats.Add(cf3);
                                }

                                sheet.addCell(new Blank(topLeft.getColumn(), i, cf3));
                            }
                        }

                        // Handle the top right corner
                        if (bottomRight.getColumn() > topLeft.getColumn())
                        {
                            if (bottomRight.getRow() != topLeft.getRow())
                            {
                                // Handle the corner cell
                                CellXFRecord cf6 = new CellXFRecord(tlformat);
                                cf6.setBorder(Border.ALL, BorderLineStyle.NONE, Colour.BLACK);
                                cf6.setBorder(Border.RIGHT,
                                              tlformat.getBorderLine(Border.RIGHT),
                                              tlformat.getBorderColour(Border.RIGHT));
                                cf6.setBorder(Border.TOP,
                                              tlformat.getBorderLine(Border.TOP),
                                              tlformat.getBorderColour(Border.TOP));
                                index = borderFormats.IndexOf(cf6);
                                if (index != -1)
                                {
                                    cf6 = (CellXFRecord)borderFormats[index];
                                }
                                else
                                {
                                    borderFormats.Add(cf6);
                                }

                                sheet.addCell(new Blank(bottomRight.getColumn(),
                                                        topLeft.getRow(), cf6));
                            }

                            // Handle the cells along the right
                            for (int i = topLeft.getRow() + 1;
                                 i < bottomRight.getRow(); i++)
                            {
                                CellXFRecord cf7 = new CellXFRecord(tlformat);
                                cf7.setBorder(Border.ALL, BorderLineStyle.NONE, Colour.BLACK);
                                cf7.setBorder(Border.RIGHT,
                                              tlformat.getBorderLine(Border.RIGHT),
                                              tlformat.getBorderColour(Border.RIGHT));

                                index = borderFormats.IndexOf(cf7);
                                if (index != -1)
                                {
                                    cf7 = (CellXFRecord)borderFormats[index];
                                }
                                else
                                {
                                    borderFormats.Add(cf7);
                                }

                                sheet.addCell(new Blank(bottomRight.getColumn(), i, cf7));
                            }

                            // Handle the cells along the top, and along the bottom too
                            for (int i = topLeft.getColumn() + 1;
                                 i < bottomRight.getColumn(); i++)
                            {
                                CellXFRecord cf8 = new CellXFRecord(tlformat);
                                cf8.setBorder(Border.ALL, BorderLineStyle.NONE, Colour.BLACK);
                                cf8.setBorder(Border.TOP,
                                              tlformat.getBorderLine(Border.TOP),
                                              tlformat.getBorderColour(Border.TOP));

                                if (topLeft.getRow() == bottomRight.getRow())
                                {
                                    cf8.setBorder(Border.BOTTOM,
                                                  tlformat.getBorderLine(Border.BOTTOM),
                                                  tlformat.getBorderColour(Border.BOTTOM));
                                }

                                index = borderFormats.IndexOf(cf8);
                                if (index != -1)
                                {
                                    cf8 = (CellXFRecord)borderFormats[index];
                                }
                                else
                                {
                                    borderFormats.Add(cf8);
                                }

                                sheet.addCell(new Blank(i, topLeft.getRow(), cf8));
                            }
                        }

                        // Handle the bottom right corner
                        if (bottomRight.getColumn() > topLeft.getColumn() ||
                            bottomRight.getRow() > topLeft.getRow())
                        {
                            // Handle the corner cell
                            CellXFRecord cf4 = new CellXFRecord(tlformat);
                            cf4.setBorder(Border.ALL, BorderLineStyle.NONE, Colour.BLACK);
                            cf4.setBorder(Border.RIGHT,
                                          tlformat.getBorderLine(Border.RIGHT),
                                          tlformat.getBorderColour(Border.RIGHT));
                            cf4.setBorder(Border.BOTTOM,
                                          tlformat.getBorderLine(Border.BOTTOM),
                                          tlformat.getBorderColour(Border.BOTTOM));

                            if (bottomRight.getRow() == topLeft.getRow())
                            {
                                cf4.setBorder(Border.TOP,
                                              tlformat.getBorderLine(Border.TOP),
                                              tlformat.getBorderColour(Border.TOP));
                            }

                            if (bottomRight.getColumn() == topLeft.getColumn())
                            {
                                cf4.setBorder(Border.LEFT,
                                              tlformat.getBorderLine(Border.LEFT),
                                              tlformat.getBorderColour(Border.LEFT));
                            }

                            index = borderFormats.IndexOf(cf4);
                            if (index != -1)
                            {
                                cf4 = (CellXFRecord)borderFormats[index];
                            }
                            else
                            {
                                borderFormats.Add(cf4);
                            }

                            sheet.addCell(new Blank(bottomRight.getColumn(),
                                                    bottomRight.getRow(), cf4));

                            // Handle the cells along the bottom (and along the top
                            // as well, if appropriate)
                            for (int i = topLeft.getColumn() + 1;
                                 i < bottomRight.getColumn(); i++)
                            {
                                CellXFRecord cf5 = new CellXFRecord(tlformat);
                                cf5.setBorder(Border.ALL, BorderLineStyle.NONE, Colour.BLACK);
                                cf5.setBorder(Border.BOTTOM,
                                              tlformat.getBorderLine(Border.BOTTOM),
                                              tlformat.getBorderColour(Border.BOTTOM));

                                if (topLeft.getRow() == bottomRight.getRow())
                                {
                                    cf5.setBorder(Border.TOP,
                                                  tlformat.getBorderLine(Border.TOP),
                                                  tlformat.getBorderColour(Border.TOP));
                                }

                                index = borderFormats.IndexOf(cf5);
                                if (index != -1)
                                {
                                    cf5 = (CellXFRecord)borderFormats[index];
                                }
                                else
                                {
                                    borderFormats.Add(cf5);
                                }

                                sheet.addCell(new Blank(i, bottomRight.getRow(), cf5));
                            }
                        }
                    }
                    catch (WriteException e)
                    {
                        // just log e.ToString(), not the whole stack trace
                        //logger.warn(e.ToString());
                    }
                }
            }
        }