Location>code7788 >text

Java Programming Guide: Advanced Techniques Explained - Programming Excel Cell Styles

Popularity:172 ℃/2024-07-25 12:19:32

Latest technical resources (recommended favorites)
/resources/

preamble

In Java development, handling Excel files is a common task. When dealing with Excel files, cells often need to be styled to meet specific needs and beautification requirements. By using the relevant libraries and APIs in Java, we can easily manipulate Excel files and set cell styles.

  • Text color
  • frame
  • Text Style
  • Text alignment and indentation
  • Text direction and angle
  • RichText Control
  • conditional format
  • Cell Styles

1. Text color

Text color is one of the basic appearance settings that helps to handle a variety of data situations, such as

  • Highlight important elements of the data, such as temperature values, which turn red as the temperature rises
  • Distinguish data such as departmental or regional sales

In Excel, you can use the "toolbar" or "set cell formatting" dialog box in the color palette to add text color, as shown below:

With the help of GcExcel, theIRange interfacesFont setColor maybeThemeColor property, as shown in the following code:

//Standard, system colors
("A1").getFont().setColor(());

//or theme color
("A1").getFont().setThemeColor(ThemeColor.Light1);

2. frame

Borders are another common formatting option, and with GcExcel, you can set them using theIRange interfacesBorders to set up.

("A1").getBorders().setLineStyle();
// Or
("A1").getCells().getBorders().setLineStyle()

3. Text Style

With GcExcel, it is possible to useIRange interface to set the font and style of the text:

//Set Bold
("A1").getFont().setBold(true);
//set italic
("A1").getFont().setItalic(true); //set italic(true); //set italic(true); //set italic(true)
//set underline
("A1").getFont().setUnderline();; //set underline

4. Text alignment and indentation

Text alignment and indentation are paragraph formatting attributes that are typically used to format the appearance of text throughout a paragraph or numbers in a table.

There are two types of text alignment in Excel:

  • Horizontal alignment, including the following options: left-aligned, center-aligned, right-aligned, and justified
  • Vertical alignment options: top, center and bottom

With GcExcel, it is possible to useIRange interfacesHorizontalAlignment and VerticalAlignment attribute to programmatically align the text, as shown below:

// Horizontal Alignment
("A1").setHorizontalAlignment();
//Vertical Alignment
("A1").setVerticalAlignment(); //Vertical Alignment

There are two types of indentation: left indentation and right indentation.

This can be done by using theIRange interface enablementAddIndent Set up and configureIndentLevel(which accepts an integer indicating the indentation level) to apply text indentation, as shown in the code below:

("A1:H7").setAddIndent(true);
("A1:A7").setIndentLevel(0);
("B1:B7").setIndentLevel(1);
("C1:C7").setIndentLevel(2);

5. Text direction and angle

The Text Orientation and Text Rotation settings in Excel help with language-specific style settings. Text Orientation configures the direction of writing - left to right (LTR) or right to left (RTL) and can be used for languages such as Arabic. Text Rotation sets the angle of the text and is particularly useful for vertical text such as CJK.

With GcExcel, you can useIRange interfacesReadingOrder property to set the text direction. It acceptsReadingOrder values in the enumeration, as shown below:

("A1").setReadingOrder();

You can combine the Orientation property with theIRange interface is used together to add angled text. It accepts integer values from -90 to 90 or 255 (for vertical text) as shown below:

("A1").setOrientation(45);

Click hereCheck out the online demo.

6. RichText Control

To include rich text in a cell in Excel, enter the cell in edit mode and then select a portion of the text to apply separate formatting, as shown below:

With GcExcel, it is possible to useIRichText cap (a poem)ITextRun object to configure RichText:

IRichText richText = ("A1").getRichText();
// Add the string "Documents" to the IRichText object and apply the formatting.
ITextRun run1 = ("Document"); ().
().setColor(()); ().

().setSize(20);

// Append the string "Solutions" to the IRichText object and apply formatting.
ITextRun run2 = ("Solutions"); ().
().setThemeFont(); ().

().setSize(30); ().setUnderline().
().setUnderline();

For more detailed information, please check outhelp file

Click hereCheck out the Demo demo.

7. conditional format

In a worksheet, Excel allows users to create conditional formatting rules for a single cell or a series of cells so that different formats are automatically applied to cells, rows, columns, or data throughout the worksheet. Conditional formatting can help users quickly visualize and analyze data, highlighting cells or values that meet specific conditions.

With GcExcel, it is possible to useIRange interfacesFormatConditions Set the conditional format rule set. For example, to apply conditional formatting to a unique value in a region, you need to add the AddUniqueValue rule to theFormatConditions in the collection, as shown in the following code:

IUniqueValues condition = ("A1:E7").getFormatConditions().addUniqueValues();
();
().setName("Arial");
().setColor(());

For more detailed information, please check outhelp file

Click hereCheck out the Demo demo.

8. Cell Styles

Excel provides a variety of built-in cell styles (such as "Good", "Bad", "Heading", "Title", etc.) in order to quickly set up according to specific data needs. Title", etc.), in order to quickly set the cell style according to specific data needs.

With GcExcel, you can use the workbook'sStyles The collection programmatically applies these quick styles to cells or cell regions and provides them as values to the Properties:

("A1").setStyle(().get("Bad"));

Click hereCheck out the Demo demo.

summarize

Above is the implementation of Excel cells with the help of Java content, in general, GcExcel not only provides powerful data management features, but also increased programmability, so that users can enhance the visual representation of their data to achieve more effective communication and analysis.

Extended Links:

Redis from Beginning to Practice

One lesson to take you through database transactions!

Chrome Developer Tools Tutorial

How to Add a JavaScript Excel Viewer to a Web Application

High Performance Rendering - Explaining the Benefits and Performance of HTML Canvas