Nomi delle celle
Creazione di un intervallo denominato
Utilizzando Microsoft Excel
I seguenti passaggi descrivono come dare un nome a una cella o a un intervallo di celle utilizzando Microsoft Excel. Questo metodo si applica a Microsoft Office Excel 2003, Microsoft Excel 97, 2000 e 2002.
- Seleziona la cella o l’intervallo di celle che desideri nominare.
- Fare clic sulla casella Nome all’estremità sinistra della barra della formula.
- Digita il nome delle celle.
- Premi INVIO.
Usare Aspose.Cells
Qui, utilizziamo l’API Aspose.Cells per svolgere il compito.
Aspose.Cells fornisce una classe, Workbook, che rappresenta un file di Microsoft Excel. La classe Workbook contiene una WorksheetCollection che consente l’accesso a ogni foglio di lavoro in un file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una raccolta Cells.
È possibile creare un intervallo nominato chiamando il metodo sovraccarico createRange della raccolta Cells. Una versione tipica del metodo createRange richiede i seguenti parametri:
- Nome della cella in alto a sinistra, il nome della cella in alto a sinistra nell’intervallo.
- Nome della cella in basso a destra, il nome della cella in basso a destra nell’intervallo.
Quando il metodo createRange viene chiamato, restituisce il nuovo intervallo nominato creato come un’istanza della classe Range.
L’esempio seguente mostra come creare un intervallo nominato di celle che si estende su B4:G14.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(CreateNamedRangeofCells.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Accessing the first worksheet in the Excel file | |
Worksheet sheet = worksheets.get(0); | |
Cells cells = sheet.getCells(); | |
// Creating a named range | |
Range namedRange = cells.createRange("B4", "G14"); | |
namedRange.setName("TestRange"); | |
// Saving the modified Excel file in default (that is Excel 2000) format | |
workbook.save(dataDir + "CNROfCells_out.xls"); | |
// Print message | |
System.out.println("Process completed successfully"); |
Accesso a Tutti gli Intervalli Nominati in un Foglio di Calcolo
Chiamare il metodo getNamedRanges della WorksheetCollection per ottenere tutti gli intervalli nominati in un foglio di calcolo. Il metodo getNamedRanges restituisce un array di tutti gli intervalli nominati nella WorksheetCollection.
L’esempio seguente mostra come accedere a tutti i nomi definiti in un libro.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(AccessAllNamedRanges.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Accessing the first worksheet in the Excel file | |
Worksheet sheet = worksheets.get(0); | |
Cells cells = sheet.getCells(); | |
// Getting all named ranges | |
Range[] namedRanges = worksheets.getNamedRanges(); | |
// Print message | |
System.out.println("Number of Named Ranges : " + namedRanges.length); |
Accedi a un intervallo nominato specifico
Chiamare il metodo getRangeByName della raccolta WorksheetCollection per ottenere un intervallo specificato per nome. Una tipica versione di getRangeByName richiede il nome dell’intervallo nominato e restituisce l’intervallo nominato specificato come un’istanza della classe Range.
L’esempio seguente mostra come accedere a un intervallo specifico tramite il relativo nome.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(AccessSpecificNamedRange.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Accessing the first worksheet in the Excel file | |
Worksheet sheet = worksheets.get(0); | |
Cells cells = sheet.getCells(); | |
// Getting the specified named range | |
Range namedRange = worksheets.getRangeByName("TestRange"); | |
// Print message | |
System.out.println("Named Range : " + namedRange.getRefersTo()); |
Identificare le Celle in un Intervallo Nominato
Usando Aspose.Cells, è possibile inserire dati nelle singole celle di un intervallo. Supponiamo di avere un intervallo di celle nominato, ad esempio A1:C4. Così la matrice avrebbe 4 * 3 = 12 celle e le celle dell’intervallo sono disposte in modo sequenziale. Aspose.Cells fornisce alcune utili proprietà della classe Range per accedere alle singole celle nell’intervallo. È possibile utilizzare i seguenti metodi per identificare le celle nell’intervallo:
- getFirstRow restituisce l’indice della prima riga nell’intervallo nominato.
- getFirstColumn restituisce l’indice della prima colonna nell’intervallo nominato.
L’esempio seguente mostra come inserire alcuni valori nelle celle di un intervallo specificato.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(IdentifyCellsinNamedRange.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Accessing the first worksheet in the Excel file | |
Worksheet sheet = worksheets.get(0); | |
Cells cells = sheet.getCells(); | |
// Getting the specified named range | |
Range range = worksheets.getRangeByName("TestRange"); | |
// Identify range cells. | |
System.out.println("First Row : " + range.getFirstRow()); | |
System.out.println("First Column : " + range.getFirstColumn()); | |
System.out.println("Row Count : " + range.getRowCount()); | |
System.out.println("Column Count : " + range.getColumnCount()); |
Inserimento dei dati nelle celle dell’intervallo nominato
Usando Aspose.Cells, è possibile inserire dati nelle singole celle di un intervallo. Supponiamo di avere un intervallo di celle nominato, ad esempio H1:J4. Quindi la matrice avrebbe 4 * 3 = 12 celle e le celle dell’intervallo sono disposte in modo sequenziale. Aspose.Cells fornisce alcune utili proprietà della classe Range per accedere alle singole celle nell’intervallo. È possibile utilizzare le seguenti proprietà per identificare le celle nell’intervallo:
- getFirstRow restituisce l’indice della prima riga nell’intervallo nominato.
- getFirstColumn restituisce l’indice della prima colonna nell’intervallo nominato.
L’esempio seguente mostra come inserire alcuni valori nelle celle di un intervallo specificato.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(InputDataInCellsInRange.class) + "data/"; | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet in the workbook. | |
Worksheet worksheet1 = workbook.getWorksheets().get(0); | |
// Create a range of cells and specify its name based on H1:J4. | |
Range range = worksheet1.getCells().createRange("H1:J4"); | |
range.setName("MyRange"); | |
// Input some data into cells in the range. | |
range.get(0, 0).setValue("USA"); | |
range.get(0, 1).setValue("SA"); | |
range.get(0, 2).setValue("Israel"); | |
range.get(1, 0).setValue("UK"); | |
range.get(1, 1).setValue("AUS"); | |
range.get(1, 2).setValue("Canada"); | |
range.get(2, 0).setValue("France"); | |
range.get(2, 1).setValue("India"); | |
range.get(2, 2).setValue("Egypt"); | |
range.get(3, 0).setValue("China"); | |
range.get(3, 1).setValue("Philipine"); | |
range.get(3, 2).setValue("Brazil"); | |
// Save the excel file. | |
workbook.save(dataDir + "IDICInRange_out.xls"); | |
// Print message | |
System.out.println("Process completed successfully"); |
Formato degli intervalli… Impostazione del colore di sfondo e degli attributi del carattere per un intervallo nominato
Per applicare la formattazione, definire un oggetto Style per specificare le impostazioni dello stile e applicarlo all’oggetto Range.
L’esempio seguente mostra come impostare un colore di riempimento solido (colore di ombreggiatura) con le impostazioni del carattere a un intervallo.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(FormatRanges1.class) + "data/"; | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet in the book. | |
Worksheet WS = workbook.getWorksheets().get(0); | |
// Create a named range of cells. | |
com.aspose.cells.Range range = WS.getCells().createRange(1, 1, 1, 17); | |
range.setName("MyRange"); | |
// Declare a style object. | |
Style stl; | |
// Create the style object with respect to the style of a cell. | |
stl = WS.getCells().get(1, 1).getStyle(); | |
// Specify some Font settings. | |
stl.getFont().setName("Arial"); | |
stl.getFont().setBold(true); | |
// Set the font text color | |
stl.getFont().setColor(Color.getRed()); | |
// To Set the fill color of the range, you may use ForegroundColor with | |
// solid Pattern setting. | |
stl.setBackgroundColor(Color.getYellow()); | |
stl.setPattern(BackgroundType.SOLID); | |
// Apply the style to the range. | |
for (int r = 1; r < 2; r++) { | |
for (int c = 1; c < 18; c++) { | |
WS.getCells().get(r, c).setStyle(stl); | |
} | |
} | |
// Save the excel file. | |
workbook.save(dataDir + "FormatRanges1_out.xls"); | |
// Print message | |
System.out.println("Process completed successfully"); |
Formato degli intervalli… Aggiunta di bordi a un intervallo nominato
È possibile aggiungere bordi a un intervallo di celle anziché a una singola cella. L’oggetto Range fornisce un metodo setOutlineBorders che richiede i seguenti parametri per aggiungere un bordo all’intervallo di celle:
- borderStyle: il tipo di bordo, selezionato dall’enumerazione CellBorderType.
- borderColor: il colore della linea del bordo, selezionato dall’enumerazione Color.
L’esempio seguente mostra come impostare un bordo di contorno a un intervallo.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(FormatRanges2.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Workbook object | |
// Obtaining the reference of the newly added worksheet | |
int sheetIndex = workbook.getWorksheets().add(); | |
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex); | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.setValue("Hello World From Aspose"); | |
// Creating a range of cells starting from "A1" cell to 3rd column in a | |
// row | |
Range range = worksheet.getCells().createRange("A1:C1"); | |
range.setName("MyRange"); | |
// Adding a thick outline border with the blue line | |
range.setOutlineBorders(CellBorderType.THICK, Color.getBlue()); | |
// Saving the Excel file | |
workbook.save(dataDir + "FormatRanges2_out.xls"); | |
// Print message | |
System.out.println("Process completed successfully"); |
Il seguente output verrebbe generato dopo l’esecuzione del codice precedente:
Applicare uno stile alle celle in un intervallo
A volte si desidera applicare uno stile alle celle in un Range. Per fare ciò, è possibile iterare sulle celle nell’intervallo e utilizzare il metodo Cell.setStyle per applicare lo stile alla cella.
L’esempio seguente mostra come applicare stili alle celle in un intervallo.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ConvertCellsAddresstoRangeorCellArea.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the newly added worksheet | |
int sheetIndex = workbook.getWorksheets().add(); | |
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex); | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.setValue("Hello World!"); | |
// Creating a range of cells based on cells Address. | |
Range range = worksheet.getCells().createRange("A1:F10"); | |
// Specify a Style object for borders. | |
Style style = cell.getStyle(); | |
// Setting the line style of the top border | |
style.setBorder(BorderType.TOP_BORDER, CellBorderType.THICK, Color.getBlack()); | |
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.THICK, Color.getBlack()); | |
style.setBorder(BorderType.LEFT_BORDER, CellBorderType.THICK, Color.getBlack()); | |
style.setBorder(BorderType.RIGHT_BORDER, CellBorderType.THICK, Color.getBlack()); | |
Iterator cellArray = range.iterator(); | |
while (cellArray.hasNext()) { | |
Cell temp = (Cell) cellArray.next(); | |
// Saving the modified style to the cell. | |
temp.setStyle(style); | |
} | |
// Saving the Excel file | |
workbook.save(dataDir + "CCAToROrCArea_out.xls"); |
Rimuovere un intervallo nominato
Aspose.Cells fornisce il metodo NameCollection.RemoveAt() per cancellare il nome dell’intervallo. Per cancellare i contenuti dell’intervallo, utilizzare il metodo Cells.ClearRange(). L’esempio seguente mostra come rimuovere un intervallo nominato con i relativi contenuti.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(RemoveANamedRange.class) + "data/"; | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get all the worksheets in the book. | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Get the first worksheet in the worksheets collection. | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Create a range of cells. | |
Range range1 = worksheet.getCells().createRange("E12", "I12"); | |
// Name the range. | |
range1.setName("MyRange"); | |
// Set the outline border to the range. | |
range1.setOutlineBorder(BorderType.TOP_BORDER, CellBorderType.MEDIUM, Color.fromArgb(0, 0, 128)); | |
range1.setOutlineBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.fromArgb(0, 0, 128)); | |
range1.setOutlineBorder(BorderType.LEFT_BORDER, CellBorderType.MEDIUM, Color.fromArgb(0, 0, 128)); | |
range1.setOutlineBorder(BorderType.RIGHT_BORDER, CellBorderType.MEDIUM, Color.fromArgb(0, 0, 128)); | |
// Input some data with some formattings into | |
// a few cells in the range. | |
range1.get(0, 0).setValue("Test"); | |
range1.get(0, 4).setValue("123"); | |
// Create another range of cells. | |
Range range2 = worksheet.getCells().createRange("B3", "F3"); | |
// Name the range. | |
range2.setName("testrange"); | |
// Copy the first range into second range. | |
range2.copy(range1); | |
// Remove the previous named range (range1) with its contents. | |
worksheet.getCells().clearRange(11, 4, 11, 8); | |
worksheets.getNames().removeAt(0); | |
// Save the excel file. | |
workbook.save(dataDir + "RANRange_out.xls"); | |
// Print message | |
System.out.println("Process completed successfully"); |
borderColors