Gestione dei fogli di lavoro in Ruby

Aspose.Cells - Gestione fogli di lavoro

Aggiunta di fogli di lavoro a un nuovo file Excel

Per aggiungere un foglio di lavoro a un nuovo file Excel utilizzandoAspose.Cells Java per Rubino , chiama semplicementeadd_worksheet metodo diGestione dei fogli di lavoro modulo.

Codice Rubino

 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 

Aggiunta di fogli di lavoro a un foglio di calcolo di Designer

Il processo di aggiunta di fogli di lavoro a un foglio di calcolo del designer è del tutto uguale a quello dell’approccio precedente, tranne per il fatto che il file Excel è già stato creato e dobbiamo prima aprire quel file Excel prima di aggiungervi il foglio di lavoro.

Codice Rubino

 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  

Accesso ai fogli di lavoro utilizzando il nome del foglio

Per accedere al foglio di lavoro per nome del foglio utilizzandoAspose.Cells Java per Rubino , chiama semplicementeget_worksheet metodo diGestione dei fogli di lavoro modulo.

Codice Rubino

 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

Rimozione di fogli di lavoro utilizzando il nome del foglio

Per rimuovere il foglio di lavoro per nome del foglio utilizzandoAspose.Cells Java per Rubino , chiama semplicementeremove_worksheet_by_name metodo diGestione dei fogli di lavoro modulo.

Codice Rubino

 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

Rimozione di fogli di lavoro utilizzando l’indice dei fogli

Per rimuovere il foglio di lavoro dall’indice del foglio utilizzandoAspose.Cells Java per Rubino , chiama semplicementeremove_worksheet_by_index metodo diGestione dei fogli di lavoro modulo.

Codice Rubino

 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 

Scarica il codice in esecuzione

Scaricamento**Gestione fogli di lavoro (Aspose.Cells)**da uno qualsiasi dei siti di social coding sotto indicati: