Hantera arbetsblad i Ruby

Aspose.Cells - Hantera arbetsblad

Lägga till kalkylblad till en ny Excel-fil

För att lägga till kalkylblad i en ny Excel-fil medAspose.Cells Java för Ruby , ring helt enkeltadd_worksheet metod avHantera arbetsblad modul.

Ruby kod

 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 

Lägga till kalkylblad till ett designerkalkylblad

Processen att lägga till kalkylblad till ett designerkalkylblad är helt samma som ovanstående tillvägagångssätt förutom att Excel-filen redan är skapad och vi måste öppna den Excel-filen först innan vi lägger till kalkylblad till den.

Ruby kod

 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  

Få åtkomst till kalkylblad med hjälp av arbetsbladsnamn

För att komma åt kalkylblad efter arknamn med hjälp avAspose.Cells Java för Ruby , ring helt enkeltget_worksheet metod avHantera arbetsblad modul.

Ruby kod

 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

Ta bort kalkylblad med Sheet Name

För att ta bort kalkylblad efter arknamn medAspose.Cells Java för Ruby , ring helt enkeltremove_worksheet_by_name metod avHantera arbetsblad modul.

Ruby kod

 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

Ta bort kalkylblad med Sheet Index

För att ta bort kalkylblad för ark index medAspose.Cells Java för Ruby , ring helt enkeltremove_worksheet_by_index metod avHantera arbetsblad modul.

Ruby kod

 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 

Ladda ner Running Code

Ladda ner**Hantera arbetsblad (Aspose.Cells)**från någon av nedan nämnda webbplatser för social kodning: