New Article - How can you Insert / Delete Rows Programmatically

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Do you wish to have worksheets that can be easily updated to keep up with your constantly changing business needs?

Using http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/index.html Aspose.Cells APIs , you can quickly insert new rows, columns, cells, and worksheets — and just as easily delete them — 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, http://www.aspose.com/products/file-format-components/aspose.cells-for-.net-and-java/default.aspx Aspose.Cells 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., InsertRows and DeleteRows , 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.

http://www.aspose.com/products/file-format-components/aspose.cells-for-.net-and-java/default.aspx Aspose.Cells works in the same way as Microsoft Excel 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

Code:
 [C#] 

//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\\");

 [VB] 

\\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\\")

 [Java] 

//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\\");

About Aspose.Cells for .NET

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


Contact Information
Suite 119, 272 Victoria Avenue
Chatswood, NSW, 2067
Australia
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 Aspose - The .NET and Java component publisher
mailto:sales@aspose.com sales@aspose.com
Phone: 888.277.6734
Fax: 866.810.9465l

Click here to view the article
 
Back
Top