We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Add/remove single cell or multiple cells in .NET

Is there any possibility to add or remove a single cell with Aspose.Cells, like: right click in Excel => Insert… => Shift cells right/down? AFIAK, I can only add and remove entire rows or columns.

I want to generate a worksheet containing multiple ‘tables’ with a variable number of rows/columns, and I run into trouble when these tables are placed above or beside each other if I can only add/remove columns and rows.


Well, you may try to use Cells.InsertRange() to insert your desired range of cells for your needs. Similarly you may try to use Cells.DeleteRange() to remove your desired range of cells accordingly. The range/cell area can involve a single cell or multiple cells. See the sample code segments for your reference:
Sample code:

            Cells sourceCells = workbook.Worksheets[0].Cells;

            //Define cell area, e.g B2  where to insert the range of cell(s).         
            CellArea ca = new CellArea();
            ca.StartRow = 1;
            ca.EndRow = 1;
            ca.StartColumn = 1;
            ca.EndColumn = 1;

            //Insert a single cell.
            sourceCells.InsertRange(ca, 1, ShiftType.Down, true);

Workbook document = new Workbook(strFileName);
Worksheet w = document.Worksheets[“Sheet2”];
//Delete the range, i.e., B2:G6 and shifts the cells upwards.
w.Cells.DeleteRange(1, 1, 5, 6, ShiftType.Up);

Hope, this helps a bit.

Ok, thank you Amjad. That should do the trick. I couldn’t find an example, and overlooked the InsertRange function.



Good to know that your issue is sorted out by the suggested code segments. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.