Beispiel #1
0
        /**
         * Creates a comment.
         * @param anchor the client anchor describes how this comment is attached
         *               to the sheet.
         * @return the newly Created comment.
         */
        public IComment CreateCellComment(IClientAnchor anchor)
        {
            XSSFClientAnchor ca    = (XSSFClientAnchor)anchor;
            XSSFSheet        sheet = (XSSFSheet)GetParent();

            //create comments and vmlDrawing parts if they don't exist
            CommentsTable  comments = sheet.GetCommentsTable(true);
            XSSFVMLDrawing vml      = sheet.GetVMLDrawing(true);

            NPOI.OpenXmlFormats.Vml.CT_Shape vmlShape = vml.newCommentShape();
            if (ca.IsSet())
            {
                // convert offsets from emus to pixels since we get a DrawingML-anchor
                // but create a VML Drawing
                int    dx1Pixels = ca.Dx1 / Units.EMU_PER_PIXEL;
                int    dy1Pixels = ca.Dy1 / Units.EMU_PER_PIXEL;
                int    dx2Pixels = ca.Dx2 / Units.EMU_PER_PIXEL;
                int    dy2Pixels = ca.Dy2 / Units.EMU_PER_PIXEL;
                String position  =
                    ca.Col1 + ", " + dx1Pixels + ", " +
                    ca.Row1 + ", " + dy1Pixels + ", " +
                    ca.Col2 + ", " + dx2Pixels + ", " +
                    ca.Row2 + ", " + dy2Pixels;
                vmlShape.GetClientDataArray(0).SetAnchorArray(0, position);
            }
            String ref1 = new CellReference(ca.Row1, ca.Col1).FormatAsString();

            if (comments.FindCellComment(ref1) != null)
            {
                throw new ArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref1);
            }

            return(new XSSFComment(comments, comments.NewComment(ref1), vmlShape));
        }
Beispiel #2
0
        /// <summary>
        /// Removes the comment for this cell, if there is one.
        /// </summary>
        public void RemoveCellComment()
        {
            IComment comment = this.CellComment;

            if (comment != null)
            {
                CellAddress ref1 = new CellAddress(GetReference());
                XSSFSheet   sh   = (XSSFSheet)Sheet;
                sh.GetCommentsTable(false).RemoveComment(ref1);
                sh.GetVMLDrawing(false).RemoveCommentShape(RowIndex, ColumnIndex);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Removes the comment for this cell, if there is one.
        /// </summary>
        public void RemoveCellComment()
        {
            IComment comment = this.CellComment;

            if (comment != null)
            {
                String    ref1 = _cell.r;
                XSSFSheet sh   = (XSSFSheet)Sheet;
                sh.GetCommentsTable(false).RemoveComment(ref1);
                sh.GetVMLDrawing(false).RemoveCommentShape(RowIndex, ColumnIndex);
            }
        }
Beispiel #4
0
        public void RemoveCellComment()
        {
            if (this.CellComment == null)
            {
                return;
            }
            string    r     = this._cell.r;
            XSSFSheet sheet = (XSSFSheet)this.Sheet;

            sheet.GetCommentsTable(false).RemoveComment(r);
            sheet.GetVMLDrawing(false).RemoveCommentShape(this.RowIndex, this.ColumnIndex);
        }
Beispiel #5
0
        public IComment CreateCellComment(IClientAnchor anchor)
        {
            XSSFClientAnchor xssfClientAnchor = (XSSFClientAnchor)anchor;
            XSSFSheet        parent           = (XSSFSheet)this.GetParent();
            CommentsTable    commentsTable    = parent.GetCommentsTable(true);

            NPOI.OpenXmlFormats.Vml.CT_Shape vmlShape = parent.GetVMLDrawing(true).newCommentShape();
            if (xssfClientAnchor.IsSet())
            {
                string str = xssfClientAnchor.Col1.ToString() + ", 0, " + (object)xssfClientAnchor.Row1 + ", 0, " + (object)xssfClientAnchor.Col2 + ", 0, " + (object)xssfClientAnchor.Row2 + ", 0";
                vmlShape.GetClientDataArray(0).SetAnchorArray(0, str);
            }
            return((IComment) new XSSFComment(commentsTable, commentsTable.CreateComment(), vmlShape)
            {
                Column = xssfClientAnchor.Col1, Row = xssfClientAnchor.Row1
            });
        }
Beispiel #6
0
        /**
         * Creates a comment.
         * @param anchor the client anchor describes how this comment is attached
         *               to the sheet.
         * @return the newly Created comment.
         */
        public IComment CreateCellComment(IClientAnchor anchor)
        {
            XSSFClientAnchor ca    = (XSSFClientAnchor)anchor;
            XSSFSheet        sheet = (XSSFSheet)GetParent();

            //create comments and vmlDrawing parts if they don't exist
            CommentsTable  comments = sheet.GetCommentsTable(true);
            XSSFVMLDrawing vml      = sheet.GetVMLDrawing(true);

            NPOI.OpenXmlFormats.Vml.CT_Shape vmlShape = vml.newCommentShape();
            if (ca.IsSet())
            {
                String position =
                    ca.Col1 + ", 0, " + ca.Row1 + ", 0, " +
                    ca.Col2 + ", 0, " + ca.Row2 + ", 0";
                vmlShape.GetClientDataArray(0).SetAnchorArray(0, position);
            }
            String      ref1  = new CellReference(ca.Row1, ca.Col1).FormatAsString();
            XSSFComment shape = new XSSFComment(comments, comments.NewComment(ref1), vmlShape);

            return(shape);
        }
Beispiel #7
0
        public void TestCommentShowsBox()
        {
            XSSFWorkbook wb = new XSSFWorkbook();

            wb.CreateSheet();
            XSSFSheet          sheet   = (XSSFSheet)wb.GetSheetAt(0);
            XSSFCell           cell    = (XSSFCell)sheet.CreateRow(0).CreateCell(0);
            XSSFDrawing        drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();
            XSSFCreationHelper factory = (XSSFCreationHelper)wb.GetCreationHelper();
            XSSFClientAnchor   anchor  = (XSSFClientAnchor)factory.CreateClientAnchor();

            anchor.Col1 = cell.ColumnIndex;
            anchor.Col2 = cell.ColumnIndex + 3;
            anchor.Row1 = cell.RowIndex;
            anchor.Row2 = cell.RowIndex + 5;
            XSSFComment        comment = (XSSFComment)drawing.CreateCellComment(anchor);
            XSSFRichTextString str     = (XSSFRichTextString)factory.CreateRichTextString("this is a comment");

            comment.String   = str;
            cell.CellComment = comment;

            XSSFVMLDrawing vml       = sheet.GetVMLDrawing(false);
            CT_Shapetype   shapetype = null;
            ArrayList      items     = vml.GetItems();

            foreach (object o in items)
            {
                if (o is CT_Shapetype)
                {
                    shapetype = (CT_Shapetype)o;
                }
            }
            Assert.AreEqual(NPOI.OpenXmlFormats.Vml.ST_TrueFalse.t, shapetype.stroked);
            Assert.AreEqual(NPOI.OpenXmlFormats.Vml.ST_TrueFalse.t, shapetype.filled);

            using (MemoryStream ws = new MemoryStream())
            {
                wb.Write(ws);

                using (MemoryStream rs = new MemoryStream(ws.GetBuffer()))
                {
                    wb    = new XSSFWorkbook(rs);
                    sheet = (XSSFSheet)wb.GetSheetAt(0);

                    vml       = sheet.GetVMLDrawing(false);
                    shapetype = null;
                    items     = vml.GetItems();
                    foreach (object o in items)
                    {
                        if (o is CT_Shapetype)
                        {
                            shapetype = (CT_Shapetype)o;
                        }
                    }

                    //wb.Write(new FileStream("comments.xlsx", FileMode.Create));
                    //using (MemoryStream ws2 = new MemoryStream())
                    //{
                    //    vml.Write(ws2);
                    //    throw new System.Exception(System.Text.Encoding.UTF8.GetString(ws2.GetBuffer()));
                    //}

                    Assert.AreEqual(NPOI.OpenXmlFormats.Vml.ST_TrueFalse.t, shapetype.stroked);
                    Assert.AreEqual(NPOI.OpenXmlFormats.Vml.ST_TrueFalse.t, shapetype.filled);
                }
            }
        }