Example #1
0
        public void TestReadComments()
        {
            HSSFWorkbook wb = HSSFTestDataSamples.OpenSampleWorkbook("SimpleWithComments.xls");

            NPOI.SS.UserModel.Sheet sheet = wb.GetSheetAt(0);

            Cell    cell;
            Row     row;
            Comment comment;

            for (int rownum = 0; rownum < 3; rownum++)
            {
                row     = sheet.GetRow(rownum);
                cell    = row.GetCell(0);
                comment = cell.CellComment;
                Assert.IsNull(comment, "Cells in the first column are not commented");
                Assert.IsNull(sheet.GetCellComment(rownum, 0));
            }

            for (int rownum = 0; rownum < 3; rownum++)
            {
                row     = sheet.GetRow(rownum);
                cell    = row.GetCell(1);
                comment = cell.CellComment;
                Assert.IsNotNull(comment, "Cells in the second column have comments");
                Assert.IsNotNull(sheet.GetCellComment(rownum, 1), "Cells in the second column have comments");

                Assert.AreEqual(HSSFSimpleShape.OBJECT_TYPE_COMMENT, ((HSSFSimpleShape)comment).ShapeType);
                Assert.AreEqual("Yegor Kozlov", comment.Author);
                Assert.IsFalse(comment.String.String == string.Empty, "cells in the second column have not empyy notes");
                Assert.AreEqual(rownum, comment.Row);
                Assert.AreEqual(cell.ColumnIndex, comment.Column);
            }
        }
Example #2
0
        public void TestShiftWithComments()
        {
            HSSFWorkbook wb = HSSFTestDataSamples.OpenSampleWorkbook("comments.xls");

            NPOI.SS.UserModel.Sheet sheet = wb.GetSheet("Sheet1");
            Assert.AreEqual(3, sheet.LastRowNum);

            // Verify comments are in the position expected
            Assert.IsNotNull(sheet.GetCellComment(0, 0));
            Assert.IsNull(sheet.GetCellComment(1, 0));
            Assert.IsNotNull(sheet.GetCellComment(2, 0));
            Assert.IsNotNull(sheet.GetCellComment(3, 0));

            String comment1 = sheet.GetCellComment(0, 0).String.String;

            Assert.AreEqual(comment1, "comment top row1 (index0)\n");
            String comment3 = sheet.GetCellComment(2, 0).String.String;

            Assert.AreEqual(comment3, "comment top row3 (index2)\n");
            String comment4 = sheet.GetCellComment(3, 0).String.String;

            Assert.AreEqual(comment4, "comment top row4 (index3)\n");

            // Shifting all but first line down to Test comments shifting
            sheet.ShiftRows(1, sheet.LastRowNum, 1, true, true);
            MemoryStream outputStream = new MemoryStream();

            wb.Write(outputStream);

            // Test that comments were shifted as expected
            Assert.AreEqual(4, sheet.LastRowNum);
            Assert.IsNotNull(sheet.GetCellComment(0, 0));
            Assert.IsNull(sheet.GetCellComment(1, 0));
            Assert.IsNull(sheet.GetCellComment(2, 0));
            Assert.IsNotNull(sheet.GetCellComment(3, 0));
            Assert.IsNotNull(sheet.GetCellComment(4, 0));

            String comment1_shifted = sheet.GetCellComment(0, 0).String.String;

            Assert.AreEqual(comment1, comment1_shifted);
            String comment3_shifted = sheet.GetCellComment(3, 0).String.String;

            Assert.AreEqual(comment3, comment3_shifted);
            String comment4_shifted = sheet.GetCellComment(4, 0).String.String;

            Assert.AreEqual(comment4, comment4_shifted);

            // Write out and read back in again
            // Ensure that the changes were persisted
            wb    = new HSSFWorkbook(new MemoryStream(outputStream.ToArray()));
            sheet = wb.GetSheet("Sheet1");
            Assert.AreEqual(4, sheet.LastRowNum);

            // Verify comments are in the position expected after the shift
            Assert.IsNotNull(sheet.GetCellComment(0, 0));
            Assert.IsNull(sheet.GetCellComment(1, 0));
            Assert.IsNull(sheet.GetCellComment(2, 0));
            Assert.IsNotNull(sheet.GetCellComment(3, 0));
            Assert.IsNotNull(sheet.GetCellComment(4, 0));

            comment1_shifted = sheet.GetCellComment(0, 0).String.String;
            Assert.AreEqual(comment1, comment1_shifted);
            comment3_shifted = sheet.GetCellComment(3, 0).String.String;
            Assert.AreEqual(comment3, comment3_shifted);
            comment4_shifted = sheet.GetCellComment(4, 0).String.String;
            Assert.AreEqual(comment4, comment4_shifted);
        }