Configuración de números
Configuración de formatos de visualización de Numbers y fechas
Una característica muy fuerte de Microsoft Excel es que permite a los usuarios configurar los formatos de visualización de valores numéricos y fechas. Sabemos que los datos numéricos se pueden utilizar para representar diferentes valores, incluidos valores decimales, monetarios, porcentuales, fraccionarios o contables, etc. Todos estos valores numéricos se muestran en diferentes formatos según el tipo de información que representan. Del mismo modo, existen muchos formatos en los que se puede mostrar una fecha o una hora. Aspose.Cells admite esta funcionalidad y permite a los desarrolladores configurar cualquier formato de visualización para un número o una fecha.
Configuración de formatos de visualización en Microsoft Excel
Para establecer formatos de visualización en Microsoft Excel:
- Haga clic derecho en cualquier celda.
- SeleccioneFormato Cells. Aparecerá un cuadro de diálogo que se utiliza para configurar los formatos de visualización de cualquier tipo de valor.
En el lado izquierdo del cuadro de diálogo, hay muchas categorías de valores comoGeneral, Número, Divisa, Contabilidad, Fecha, Hora, **Porcentaje,**etc. Aspose.Cells admite todos estos formatos de visualización.
Uso de formatos numéricos integrados
Aspose.Cells ofrece algunos formatos de números incorporados para configurar los formatos de visualización de los números y las fechas. Todos los formatos de números incorporados reciben valores numéricos únicos. Los desarrolladores pueden asignar cualquier valor numérico deseado alNúmero metodo de laEstilo objeto para aplicar el formato de visualización. Este enfoque es rápido. Los formatos de números incorporados admitidos por Aspose.Cells se enumeran a continuación.
Valor | Escribe | cadena de formato |
---|---|---|
0 | General | General |
1 | Decimal | 0 |
2 | Decimal | 0.00 |
3 | Decimal | # ,##0 |
4 | Decimal | # ,##0.00 |
5 | Divisa | $#,##0;$-#,##0 |
6 | Divisa | $#,##0;[Rojo]$-#,##0 |
7 | Divisa | $#,##0.00;$-#,##0.00 |
8 | Divisa | $#,##0.00;[Rojo]$-#,##0.00 |
9 | Porcentaje | 0% |
10 | Porcentaje | 0.00% |
11 | Científico | 0.00E+00 |
12 | Fracción | # ?/? |
13 | Fracción | # / |
14 | Fecha | m/d/aa |
15 | Fecha | d-mmm-aaa |
16 | Fecha | d-mmm |
17 | Fecha | mmm-aaa |
18 | Hora | h:mm AM/PM |
19 | Hora | h:mm:ss AM/PM |
20 | Hora | mmm |
21 | Hora | h: mm: ss |
22 | Hora | m/d/aa h:mm |
37 | Divisa | # ,##0;-#,##0 |
38 | Divisa | # ,##0;[Rojo]-#,##0 |
39 | Divisa | # ,##0.00;-#,##0.00 |
40 | Divisa | # ,##0.00;[Rojo]-#,##0.00 |
41 | Contabilidad | _ * #,##0_ ;_ * “_ ;_ @_ |
42 | Contabilidad | _ $* #,##0_ ;_ $* “_ ;_ @_ |
43 | Contabilidad | _ * #,##0.00_ ;_ * “??_ ;_ @_ |
44 | Contabilidad | _ $* #,##0.00_ ;_ $* “??_ ;_ @_ |
45 | Hora | mm: ss |
46 | Hora | h :mm:ss |
47 | Hora | mm:ss.0 |
48 | Científico | ## 0.0E+00 |
49 | Texto | @ |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Adding the current system date to "A1" cell | |
worksheet.getCells().get("A1").putValue(DateTime.getNow()); | |
// Getting the Style of the A1 Cell | |
Style style = worksheet.getCells().get("A1").getStyle(); | |
// Setting the display format to number 15 to show date as "d-mmm-yy" | |
style.setNumber(15); | |
// Applying the style to the A1 cell | |
worksheet.getCells().get("A1").setStyle(style); | |
// Adding a numeric value to "A2" cell | |
worksheet.getCells().get("A2").putValue(20); | |
// Getting the Style of the A2 Cell | |
style = worksheet.getCells().get("A2").getStyle(); | |
// Setting the display format to number 9 to show value as percentage | |
style.setNumber(9); | |
// Applying the style to the A2 cell | |
worksheet.getCells().get("A2").setStyle(style); | |
// Adding a numeric value to "A3" cell | |
worksheet.getCells().get("A3").putValue(2546); | |
// Getting the Style of the A3 Cell | |
style = worksheet.getCells().get("A3").getStyle(); | |
// Setting the display format to number 6 to show value as currency | |
style.setNumber(6); | |
// Applying the style to the A3 cell | |
worksheet.getCells().get("A3").setStyle(style); | |
// Saving the Excel file | |
workbook.save("book1.out.xls"); |
Uso de formatos de números personalizados
Para definir su propia cadena de formato personalizado para configurar el formato de visualización, utilice elDisfraz. Este enfoque no es tan rápido como el uso de formatos preestablecidos, pero es más flexible.
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Adding the current system date to "A1" cell | |
worksheet.getCells().get("A1").putValue(DateTime.getNow()); | |
// Getting the Style of the A1 Cell | |
Style style = worksheet.getCells().get("A1").getStyle(); | |
// Setting the custom display format to show date as "d-mmm-yy" | |
style.setCustom("d-mmm-yy"); | |
// Applying the style to the A1 cell | |
worksheet.getCells().get("A1").setStyle(style); | |
// Adding a numeric value to "A2" cell | |
worksheet.getCells().get("A2").putValue(20); | |
// Getting the Style of the A2 Cell | |
style = worksheet.getCells().get("A2").getStyle(); | |
// Setting the custom display format to show value as percentage | |
style.setCustom("0.0%"); | |
// Applying the style to the A2 cell | |
worksheet.getCells().get("A2").setStyle(style); | |
// Adding a numeric value to "A3" cell | |
worksheet.getCells().get("A3").putValue(2546); | |
// Getting the Style of the A3 Cell | |
style = worksheet.getCells().get("A3").getStyle(); | |
// Setting the custom display format to show value as currency | |
style.setCustom( "£#,##0;[Red]$-#,##0"); | |
// Applying the style to the A3 cell | |
worksheet.getCells().get("A3").setStyle(style); | |
// Saving the Excel file | |
workbook.save("book1.out.xls"); |