Imposta Formati Condizionali di file Excel e ODS.

Introduzione

La formattazione condizionale è una funzionalità avanzata di Microsoft Excel che consente di applicare formati a una cella o a un intervallo di celle e far sì che tale formattazione cambi a seconda del valore della cella o del valore di una formula. Ad esempio, è possibile rendere una cella in grassetto solo quando il suo valore supera i 500. Quando il valore della cella soddisfa la condizione, il formato specificato viene applicato alla cella. Se il valore della cella non soddisfa la condizione di formattazione, viene utilizzata la formattazione predefinita della cella. In Microsoft Excel, seleziona Formato, quindi Formattazione condizionale per aprire il dialogo della Formattazione condizionale.

Aspose.Cells supporta l’applicazione della formattazione condizionale alle celle in fase di esecuzione. Questo articolo spiega come farlo. Spiega anche come calcolare il colore utilizzato da Excel per la formattazione condizionale basata sulla scala cromatica.

Applicare la formattazione condizionale

Aspose.Cells supporta la formattazione condizionale in diversi modi:

  • Utilizzando il foglio di calcolo del designer
  • Utilizzando il metodo di copia.
  • Creare la formattazione condizionale in fase di esecuzione.

Utilizzo del foglio di calcolo del designer

I programmatori possono creare un foglio di calcolo del designer che contiene la formattazione condizionale in Microsoft Excel e quindi aprire quel foglio di calcolo con Aspose.Cells. Aspose.Cells carica e salva il foglio di calcolo del designer, mantenendo qualsiasi impostazione della formattazione condizionale.

Utilizzando il metodo di copia

Aspose.Cells consente ai programmatori di copiare le impostazioni della formattazione condizionale da una cella a un’altra nel foglio di lavoro chiamando il metodo Range.Copy().

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "Book1.xlsx", FileMode.Open);
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Copying conditional format settings from cell "A1" to cell "B1"
//worksheet.CopyConditionalFormatting(0, 0, 0, 1);
int TotalRowCount = 0;
for (int i = 0; i < workbook.Worksheets.Count; i++)
{
Worksheet sourceSheet = workbook.Worksheets[i];
Range sourceRange = sourceSheet.Cells.MaxDisplayRange;
Range destRange = worksheet.Cells.CreateRange(sourceRange.FirstRow + TotalRowCount, sourceRange.FirstColumn,
sourceRange.RowCount, sourceRange.ColumnCount);
destRange.Copy(sourceRange);
TotalRowCount = sourceRange.RowCount + TotalRowCount;
}
// Saving the modified Excel file
workbook.Save(dataDir + "output.xls");
// Closing the file stream to free all resources
fstream.Close();

Applicare la formattazione condizionale in fase di esecuzione

Aspose.Cells consente di aggiungere e rimuovere la formattazione condizionale in fase di esecuzione. Di seguito sono riportati degli esempi di codice su come impostare la formattazione condizionale:

  1. Istanziare un foglio di lavoro.
  2. Aggiungere un formato condizionale vuoto.
  3. Impostare l’intervallo a cui dovrebbe essere applicata la formattazione.
  4. Definire le condizioni di formattazione.
  5. Salvare il file.

Dopo questo esempio ci sono diversi altri esempi più piccoli che mostrano come applicare impostazioni del carattere, impostazioni dei bordi e schemi.

Microsoft Excel 2007 ha aggiunto formattazioni condizionali più avanzate che Aspose.Cells supporta anche. Gli esempi qui illustrano come utilizzare formattazioni semplici, gli esempi di Microsoft Excel 2007 mostrano come applicare formattazioni condizionali più avanzate.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string filePath = dataDir + "Book1.xlsx";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
// Adds an empty conditional formatting
int index = sheet.ConditionalFormattings.Add();
FormatConditionCollection fcs = sheet.ConditionalFormattings[index];
// Sets the conditional format range.
CellArea ca = new CellArea();
ca.StartRow = 0;
ca.EndRow = 0;
ca.StartColumn = 0;
ca.EndColumn = 0;
fcs.AddArea(ca);
ca = new CellArea();
ca.StartRow = 1;
ca.EndRow = 1;
ca.StartColumn = 1;
ca.EndColumn = 1;
fcs.AddArea(ca);
// Adds condition.
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "=A2", "100");
// Adds condition.
int conditionIndex2 = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100");
// Sets the background color.
FormatCondition fc = fcs[conditionIndex];
fc.Style.BackgroundColor = Color.Red;
// Saving the Excel file
workbook.Save(dataDir + "output.xls");

Imposta Carattere

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting the font weight to bold
style.Font.IsBold = true;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

Imposta Bordo

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
public static void Run()
{
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
// Adds an empty conditional formatting
int index = sheet.ConditionalFormattings.Add();
FormatConditionCollection fcs = sheet.ConditionalFormattings[index];
// Sets the conditional format range.
CellArea ca = new CellArea();
ca.StartRow = 0;
ca.EndRow = 5;
ca.StartColumn = 0;
ca.EndColumn = 3;
fcs.AddArea(ca);
// Adds condition.
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100");
// Sets the background color.
FormatCondition fc = fcs[conditionIndex];
fc.Style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Dashed;
fc.Style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Dashed;
fc.Style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Dashed;
fc.Style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Dashed;
fc.Style.Borders[BorderType.LeftBorder].Color = Color.FromArgb(0, 255, 255);
fc.Style.Borders[BorderType.RightBorder].Color = Color.FromArgb(0, 255, 255);
fc.Style.Borders[BorderType.TopBorder].Color = Color.FromArgb(0, 255, 255);
fc.Style.Borders[BorderType.BottomBorder].Color = Color.FromArgb(255, 255, 0);
workbook.Save(dataDir + "output.xlsx");
}

Imposta Schema

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
// Adds an empty conditional formatting
int index = sheet.ConditionalFormattings.Add();
FormatConditionCollection fcs = sheet.ConditionalFormattings[index];
// Sets the conditional format range.
CellArea ca = new CellArea();
ca.StartRow = 0;
ca.EndRow = 5;
ca.StartColumn = 0;
ca.EndColumn = 3;
fcs.AddArea(ca);
// Adds condition.
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100");
FormatCondition fc = fcs[conditionIndex];
fc.Style.Pattern = BackgroundType.ReverseDiagonalStripe;
fc.Style.ForegroundColor = Color.FromArgb(255, 255, 0);
fc.Style.BackgroundColor = Color.FromArgb(0, 255, 255);
workbook.Save(dataDir + "output.xlsx");

Argomenti avanzati