public void NegativeRowNumberIsInvalid()
        {
            var ws = new XLWorkbook().AddWorksheet("Sheet1") as XLWorksheet;

            var row = new XLRow(ws, -1);

            Assert.IsFalse(row.RangeAddress.IsValid);
        }
Esempio n. 2
0
        // load sheet into grid
        private void LoadSheet(C1FlexGrid flex, XLSheet sheet, bool fixedCells)
        {
            // account for fixed cells
            int frows = flex.Rows.Fixed;
            int fcols = flex.Cols.Fixed;

            // copy dimensions
            flex.Rows.Count = sheet.Rows.Count + frows;
            flex.Cols.Count = sheet.Columns.Count + fcols;

            // initialize fixed cells
            if (fixedCells && frows > 0 && fcols > 0)
            {
                flex.Styles.Fixed.TextAlign = TextAlignEnum.CenterCenter;
                for (int r = 1; r < flex.Rows.Count; r++)
                {
                    flex[r, 0] = r;
                }
                for (int c = 1; c < flex.Cols.Count; c++)
                {
//					string hdr = string.Format("{0}", (char)('A' + c - 1));
//					flex[0, c] = hdr;
                    flex[0, c] = c;
                }
            }

            // set default properties
            flex.Font             = sheet.Book.DefaultFont;
            flex.Rows.DefaultSize = C1XLBook.TwipsToPixels(sheet.DefaultRowHeight);
            flex.Cols.DefaultSize = C1XLBook.TwipsToPixels(sheet.DefaultColumnWidth);

            // prepare to convert styles
            _styles = new Hashtable();

            // set row/column properties
            for (int r = 0; r < sheet.Rows.Count; r++)
            {
                // size/visibility
                Row   fr = flex.Rows[r + frows];
                XLRow xr = sheet.Rows[r];
                if (xr.Height >= 0)
                {
                    fr.Height = C1XLBook.TwipsToPixels(xr.Height);
                }
                fr.Visible = xr.Visible;

                // style
                CellStyle cs = StyleFromExcel(flex, xr.Style);
                if (cs != null)
                {
                    //cs.DefinedElements &= ~StyleElementFlags.TextAlign; // << need to fix the grid
                    fr.Style = cs;
                }
            }
            for (int c = 0; c < sheet.Columns.Count; c++)
            {
                // size/visibility
                Column   fc = flex.Cols[c + fcols];
                XLColumn xc = sheet.Columns[c];
                if (xc.Width >= 0)
                {
                    fc.Width = C1XLBook.TwipsToPixels(xc.Width);
                }
                fc.Visible = xc.Visible;

                // style
                CellStyle cs = StyleFromExcel(flex, xc.Style);
                if (cs != null)
                {
                    //cs.DefinedElements &= ~StyleElementFlags.TextAlign; // << need to fix the grid
                    fc.Style = cs;
                }
            }

            // load cells
            for (int r = 0; r < sheet.Rows.Count; r++)
            {
                for (int c = 0; c < sheet.Columns.Count; c++)
                {
                    // get cell
                    XLCell cell = sheet.GetCell(r, c);
                    if (cell == null)
                    {
                        continue;
                    }

                    // apply content
                    flex[r + frows, c + fcols] = cell.Value;

                    // apply style
                    CellStyle cs = StyleFromExcel(flex, cell.Style);
                    if (cs != null)
                    {
                        flex.SetCellStyle(r + frows, c + fcols, cs);
                    }
                }
            }
        }
        //===========================================================================================
        #region ** Save a C1FlexGrid into an XLSheet
        private void SaveSheet(C1FlexGrid flex, XLSheet sheet, bool fixedCells)
        {
            // account for fixed cells
            int frows = flex.Rows.Fixed;
            int fcols = flex.Cols.Fixed;

            if (fixedCells)
            {
                frows = fcols = 0;
            }

            // copy dimensions
            int lastRow = flex.Rows.Count - frows - 1;
            int lastCol = flex.Cols.Count - fcols - 1;

            if (lastRow < 0 || lastCol < 0)
            {
                return;
            }
            XLCell cell = sheet[lastRow, lastCol];

            // set default properties
            sheet.Book.DefaultFont   = flex.Font;
            sheet.DefaultRowHeight   = C1XLBook.PixelsToTwips(flex.Rows.DefaultSize);
            sheet.DefaultColumnWidth = C1XLBook.PixelsToTwips(flex.Cols.DefaultSize);

            // prepare to convert styles
            _styles = new Hashtable();

            // set row/column properties
            for (int r = frows; r < flex.Rows.Count; r++)
            {
                // size/visibility
                Row   fr = flex.Rows[r];
                XLRow xr = sheet.Rows[r - frows];
                if (fr.Height >= 0)
                {
                    xr.Height = C1XLBook.PixelsToTwips(fr.Height);
                }
                xr.Visible = fr.Visible;

                // style
                XLStyle xs = StyleFromFlex(fr.Style);
                if (xs != null)
                {
                    xr.Style = xs;
                }
            }
            for (int c = fcols; c < flex.Cols.Count; c++)
            {
                // size/visibility
                Column   fc = flex.Cols[c];
                XLColumn xc = sheet.Columns[c - fcols];
                if (fc.Width >= 0)
                {
                    xc.Width = C1XLBook.PixelsToTwips(fc.Width);
                }
                xc.Visible = fc.Visible;

                // style
                XLStyle xs = StyleFromFlex(fc.Style);
                if (xs != null)
                {
                    xc.Style = xs;
                }
            }

            // load cells
            for (int r = frows; r < flex.Rows.Count; r++)
            {
                for (int c = fcols; c < flex.Cols.Count; c++)
                {
                    // get cell
                    cell = sheet[r - frows, c - fcols];

                    // apply content
                    cell.Value = flex[r, c];

                    // apply style
                    XLStyle xs = StyleFromFlex(flex.GetCellStyle(r, c));
                    if (xs != null)
                    {
                        cell.Style = xs;
                    }
                }
            }
        }