Jump to content


- - - - -

How can you Insert / Delete Rows Programmatically


Do you wish to have worksheets that can be easily updated to keep up with your constantly changing business needs?

Using <strong><a href="http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/index.html">Aspose.Cells APIs</a></strong>, you can <em><strong>quickly insert new rows, columns, cells, and worksheets — and just as easily delete them </strong></em>— calling a few methods only and your task is performed with excellence.

While you are creating a new worksheet from scratch or working with an existing worksheet, you might need to add extra rows / columns into your worksheet to accommodate more data or for your specific need.

Alternatively, it can also be required to delete rows / columns from specified positions / locations in the worksheet. To fulfill these requirements, <a href="http://www.aspose.com/products/file-format-components/aspose.cells-for-.net-and-java/default.aspx"><strong>Aspose.Cells</strong></a> provides simplest set of APIs that can easily perform your desired task within no time.

There are two methods that Aspose.Cells offers in this regard i.e., <em><strong>InsertRows</strong></em> and <em><strong>DeleteRows</strong></em>, these two methods are optimized related performance and efficient enough to do the job very quickly.


So if you are in need to insert some sets of rows or remove a number of rows, it is recommended that you should always use InsertRows and DeleteRows methods instead of repeatedly using InsertRow and DeleteRow methods in a loop.

<a href="http://www.aspose.com/products/file-format-components/aspose.cells-for-.net-and-java/default.aspx"><strong>Aspose.Cells</strong></a> works in the same way as <em><strong>Microsoft Excel</strong></em> does. When rows or columns are added, the contents in the worksheet are shifted to downwards or right side but if rows or columns are removed, the contents in the worksheet will be shifted to upwards or left side. Moreover, the references in other worksheets are updated accordingly upon insertion / deletion of rows.

Following example shows the usage of InsertRows and DeleteRows methods

<strong>[C#]</strong>

//Instantiate a Workbook object.
Workbook workbook = new Workbook();
//Load a template file.
workbook.Open(\\"d:\\test\\MyBook.xls\\");
//Get the first worksheet in the book.
Worksheet sheet = workbook.Worksheets[0];
//Insert 10 rows at row index 2 (insertion starts at 3rd row)
sheet.Cells.InsertRows(2, 10);
//Delete 5 rows now. (8th row - 12th row)
sheet.Cells.DeleteRows(7, 5);
//Save the excel file.
workbook.Save(\\"d:\\test\\out_MyBook.xls\\");

<strong>[VB]</strong>

\\'Instantiate a Workbook object.
Dim workbook As Workbook = New Workbook
\\'Load a template file.
workbook.Open(\\"d:\test\MyBook.xls\\")
\\'Get the first worksheet in the book.
Dim sheet As Worksheet = workbook.Worksheets(0)
\\'Insert 10 rows at row index 2 (insertion starts at 3rd row)
sheet.Cells.InsertRows(2, 10)
\\'Delete 5 rows now. (8th row - 12th row)
sheet.Cells.DeleteRows(7, 5)
\\'Save the excel file.
workbook.Save(\\"d:\test\out_MyBook.xls\\")

<strong>[Java]</strong>

//Instantiate a Workbook object.
Workbook workbook = new Workbook();
//Load a template file.
workbook.open(\\"d:\\test\\MyBook.xls\\");
//Get the first worksheet in the book.
Worksheet sheet = workbook.getWorksheets().getSheet(0);
//Insert 10 rows at row index 2 (insertion starts at 3rd row)
sheet.getCells().insertRows(2, 10);
//Delete 5 rows now. (8th row - 12th row)
sheet.getCells().deleteRows(7, 5,true);
//Save the excel file.
workbook.save(\\"d:\\test\\out_MyBook.xls\\");

<strong>About Aspose.Cells for .NET</strong>

- <a href="http://www.aspose.com/community/files/51/aspose.cells/default.aspx"><strong>Download evaluation version</strong></a> of Aspose.Cells for .NET.
- <a href="http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/index.html"><strong>Online documentation</strong></a> of Aspose.Cells for .NET.
- <a href="http://www.aspose.com/demos/aspose.cells/default.aspx"><strong>Demos</strong></a> of Aspose.Cells for .NET.
- Post your technical questions/queries to <strong><a href="http://www.aspose.com/community/forums/aspose.cells-for-.net-java-and-reporting-services/19/showforum.aspx">Aspose.Cells for .NET Forum</a></strong>.


<strong>Contact Information</strong>
Suite 119, 272 Victoria Avenue
Chatswood, NSW, 2067
Australia
<a href="http://www.extreme-vb.net/wp-admin/%5C%5C%22http://www.aspose.com/community/forums/aspose.cells-for-.net-java-and-reporting-services/19/showforum.aspx%5C%5C%22"><strong>Aspose - The .NET and Java component publisher</strong></a>
<a href="mailto:sales@aspose.com">sales@aspose.com</a>
Phone: 888.277.6734
Fax: 866.810.9465l


0 Comments