Nummerinställningar

Ställa in visningsformat för Numbers och datum

En mycket stark egenskap hos Microsoft Excel är att den tillåter användare att ställa in visningsformat för numeriska värden och datum. Vi vet att numeriska data kan användas för att representera olika värden inklusive decimal-, valuta-, procent-, bråk- eller redovisningsvärden, etc. Alla dessa numeriska värden visas i olika format beroende på vilken typ av information den representerar. På samma sätt finns det många format där ett datum eller en tid kan visas. Aspose.Cells stöder denna funktion och låter utvecklare ställa in valfritt visningsformat för ett nummer eller datum.

Ställa in visningsformat i Microsoft Excel

Så här ställer du in visningsformat i Microsoft Excel:

  1. Högerklicka på valfri cell.
  2. VäljFormat Cells. En dialogruta kommer att visas som används för att ställa in visningsformat av alla slags värden.

I den vänstra sidan av dialogrutan finns det många kategorier av värden somAllmän, siffra, Valuta, Bokföring, Datum, Tid, **Procentsats,**etc. Aspose.Cells stöder alla dessa visningsformat.

Använda inbyggda nummerformat

Aspose.Cells erbjuder några inbyggda talformat för att konfigurera visningsformaten för siffror och datum. Alla inbyggda talformat ges unika numeriska värden. Utvecklare kan tilldela vilket önskat numeriskt värde som helst tillsiffra metod förStil objekt för att tillämpa visningsformatet. Detta tillvägagångssätt är snabbt. De inbyggda talformaten som stöds av Aspose.Cells listas nedan.

Värde Typ Formatera sträng
0 Allmän Allmän
1 Decimal 0
2 Decimal 0.00
3 Decimal # ,##0
4 Decimal # ,##0.00
5 Valuta $#,##0;$-#,##0
6 Valuta $#,##0;[Röd]$-#,##0
7 Valuta $#,##0.00;$-#,##0.00
8 Valuta $#,##0.00;[Röd]$-#,##0.00
9 Procentsats 0%
10 Procentsats 0.00%
11 Vetenskaplig 0,00E+00
12 Fraktion # ?/?
13 Fraktion # /
14 Datum m/d/åå
15 Datum d-mmm-åå
16 Datum d-mmm
17 Datum mmm-åå
18 Tid h:mm AM/PM
19 Tid h:mm:ss AM/PM
20 Tid h:mm
21 Tid h:mm:ss
22 Tid m/d/åå h:mm
37 Valuta # ,##0;-#,##0
38 Valuta # ,##0;[Röd]-#,##0
39 Valuta # ,##0.00;-#,##0.00
40 Valuta # ,##0.00;[Röd]-#,##0.00
41 Bokföring _ * #,##0_ ;_ * “_ ;_ @_
42 Bokföring _ $* #,##0_ ;_ $* “_ ;_ @_
43 Bokföring _ * #,##0.00_ ;_ * “??_ ;_ @_
44 Bokföring _ $* #,##0.00_ ;_ $* “??_ ;_ @_
45 Tid mm:ss
46 Tid h :mm:ss
47 Tid mm:ss.0
48 Vetenskaplig ## 0,0E+00
49 Text @
// 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");

Använda anpassade nummerformat

För att definiera din egen anpassade formatsträng för att ställa in visningsformatet, användBeställnings. Detta tillvägagångssätt är inte lika snabbt som att använda förinställda format men det är mer flexibelt.

// 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");

Advance topics