Managing Worksheets in Ruby
Aspose.Cells - Managing Worksheets
Adding Worksheets to a New Excel File
To add Worksheet to a new Excel file using Aspose.Cells Java for Ruby, simply call add_worksheet method of MangingWorksheets module.
Ruby Code
def add_worksheet()
# Instantiating a Workbook object
workbook = Rjb::import('com.aspose.cells.Workbook').new
# Adding a new worksheet to the Workbook object
worksheets = workbook.getWorksheets()
sheet_index = worksheets.add()
worksheet = worksheets.get(sheet_index)
# Setting the name of the newly added worksheet
worksheet.setName("My Worksheet")
# Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(@data_dir + "book.out.xls")
puts "Sheet added successfully."
end
Adding Worksheets to a Designer Spreadsheet
The process of adding worksheets to a designer spreadsheet is entirely same as that of the above approach except that the Excel file is already created and we need to open that Excel file first before adding worksheet to it.
Ruby Code
def add_worksheet_to_designer_spreadsheet()
# Creating a file stream containing the Excel file to be opened
fstream = IO.sysopen(@data_dir + 'book1.xls', "w")
# Instantiating a Workbook object with the stream
workbook = Rjb::import('com.aspose.cells.Workbook').new(fstream)
# Adding a new worksheet to the Workbook object
worksheets = workbook.getWorksheets()
sheet_index = worksheets.add()
worksheet = worksheets.get(sheet_index)
# Setting the name of the newly added worksheet
worksheet.setName("My Worksheet")
# Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(@data_dir + "book1.out.xls")
end
Accessing Worksheets using Sheet Name
To access worksheet by sheet name using Aspose.Cells Java for Ruby, simply call get_worksheet method of MangingWorksheets module.
Ruby Code
def get_worksheet()
# Creating a file stream containing the Excel file to be opened
fstream = IO.sysopen(@data_dir + 'book1.xls', "w")
# Instantiating a Workbook object with the stream
workbook = Rjb::import('com.aspose.cells.Workbook').new(fstream)
# Accessing a worksheet using its sheet name
worksheet = workbook.getWorksheets().get("Sheet1")
puts worksheet.to_string
end
Removing Worksheets using Sheet Name
To remove worksheet by sheet name using Aspose.Cells Java for Ruby, simply call remove_worksheet_by_name method of MangingWorksheets module.
Ruby Code
def remove_worksheet_by_name()
# Creating a file stream containing the Excel file to be opened
fstream = IO.sysopen(@data_dir + 'book1.xls', "w")
# Instantiating a Workbook object with the stream
workbook = Rjb::import('com.aspose.cells.Workbook').new(fstream)
# Removing a worksheet using its sheet name
workbook.getWorksheets().removeAt("Sheet1")
# Saving the Excel file
workbook.save(@data_dir + "book.out.xls")
# Print Message
puts "Sheet removed successfully."
end
Removing Worksheets using Sheet Index
To remove worksheet by sheet index using Aspose.Cells Java for Ruby, simply call remove_worksheet_by_index method of MangingWorksheets module.
Ruby Code
def remove_worksheet_by_index()
# Creating a file stream containing the Excel file to be opened
fstream = IO.sysopen(@data_dir + 'book1.xls', "w")
# Instantiating a Workbook object with the stream
workbook = Rjb::import('com.aspose.cells.Workbook').new(fstream)
# Removing a worksheet using its sheet name
workbook.getWorksheets().removeAt(0)
# Saving the Excel file
workbook.save(@data_dir + "book.out.xls")
# Print Message
puts "Sheet removed successfully."
end
Download Running Code
Download Managing Worksheets (Aspose.Cells) from any of the below mentioned social coding sites: