NPOI.XSSF.UserModel.XSSFWorkbook.RemoveSheetAt C# (CSharp) Method

RemoveSheetAt() public method

public RemoveSheetAt ( int index ) : void
index int
return void
        public void RemoveSheetAt(int index)
        {
            ValidateSheetIndex(index);
            OnSheetDelete(index);

            XSSFSheet sheet = (XSSFSheet)GetSheetAt(index);
            RemoveRelation(sheet);
            sheets.RemoveAt(index);

            // only Set new sheet if there are still some left
            if (sheets.Count == 0)
            {
                return;
            }

            // the index of the closest remaining sheet to the one just deleted
            int newSheetIndex = index;
            if (newSheetIndex >= sheets.Count)
            {
                newSheetIndex = sheets.Count - 1;
            }

            // adjust active sheet
            int active = ActiveSheetIndex;
            if (active == index)
            {
                // Removed sheet was the active one, reset active sheet if there is still one left now
                SetActiveSheet(newSheetIndex);
            }
            else if (active > index)
            {
                // Removed sheet was below the active one => active is one less now
                SetActiveSheet(active - 1);
            }

        }

Usage Example

Esempio n. 1
0
        public void Bug47813()
        {
            XSSFWorkbook wb1 = XSSFTestDataSamples.OpenSampleWorkbook("47813.xlsx");

            Assert.AreEqual(3, wb1.NumberOfSheets);
            Assert.IsNotNull(wb1.GetCalculationChain());

            Assert.AreEqual("Numbers", wb1.GetSheetName(0));
            //the second sheet is of type 'chartsheet'
            Assert.AreEqual("Chart", wb1.GetSheetName(1));
            Assert.IsTrue(wb1.GetSheetAt(1) is XSSFChartSheet);
            Assert.AreEqual("SomeJunk", wb1.GetSheetName(2));

            wb1.RemoveSheetAt(2);
            Assert.AreEqual(2, wb1.NumberOfSheets);
            Assert.IsNull(wb1.GetCalculationChain());

            XSSFWorkbook wb2 = (XSSFWorkbook)XSSFTestDataSamples.WriteOutAndReadBack(wb1);

            Assert.AreEqual(2, wb2.NumberOfSheets);
            Assert.IsNull(wb2.GetCalculationChain());

            Assert.AreEqual("Numbers", wb2.GetSheetName(0));
            Assert.AreEqual("Chart", wb2.GetSheetName(1));

            wb2.Close();
            wb1.Close();
        }
All Usage Examples Of NPOI.XSSF.UserModel.XSSFWorkbook::RemoveSheetAt