public void TestShiftWithComments() { HSSFWorkbook wb = HSSFTestDataSamples.OpenSampleWorkbook("comments.xls"); NPOI.SS.UserModel.ISheet 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); }