Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (309)
Collections:
Other Resources:
Example of Excel Label Format with jxl.jar
Where can I find an example Java code that uses jxl.jar to add label cell format to a new Excel file?
✍: FYIcenter.com
You can follow these suggestions and example to add label cell format to a new Excel file with jxl.jar:
The previous tutorial illustrates the fundamentals of generating an Excel compatible spreadsheet using the JExcelApi. However, as it stands Excel will render the data in the default font, and will display the numbers to 3 decimal places. In order to supply formatting information to Excel, we must make use of the overloaded constructor, which takes an additional object containing the cell's formatting information (both the font and the style).
The code fragment below illustrates creating a label cell for an arial 10 point font.
// Create a cell format for Arial 10 point font WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10); WritableCellFormat arial10format = new WritableCellFormat (arial10font); // Create the label, specifying content and format Label label2 = new Label(1,0, "Arial 10 point label", arial10format); sheet.addCell(label2);
Cell formats objects are shared, so many cells may use the same format object, eg.
Label label3 = new Label(2, 0, "Another Arial 10 point label", arial10format); sheet.addCell(label3);
This creates another label, with the same format, in cell C1.
Because cell formats are shared, it is not possible to change the contents of a cell format object. (If this were permitted, then changing the contents of the object could have unforeseen repurcussions on the look of the rest of the workbook). In order to change the way a particular cell is displayed, the API does allow you to assign a new format to an individual cell.
The constructors for the WritableFont object have many overloads. By way of example, the code fragment below creates a label in 16 point Times, bold italic and assigns it to position D1.
// Create a cell format for Times 16, bold and italic WritableFont times16font = new WritableFont(WritableFont.TIMES, 16, WritableFont.BOLD, true); WritableCellFormat times16format = new WritableCellFormat (times16font); // Create the label, specifying content and format Label label4 = new Label(3,0, "Times 16 bold italic label", times16format); sheet.addCell(label4);
Here is the complete source code of example program JxlAddLabelFormat.java:
// Copyright (c) FYIcenter.com import jxl.write.*; // Example of adding formatting properties for labels in a new Excel file public class JxlAddLabelFormat { public static void main(String [] args) throws Exception { jxl.write.WritableWorkbook workbook = jxl.Workbook.createWorkbook(new java.io.File("output.xls")); jxl.write.WritableSheet sheet = workbook.createSheet("First Sheet", 0); // Create a cell format for Arial 10 point font WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10); WritableCellFormat arial10format = new WritableCellFormat (arial10font); // Create the label, specifying content and format Label label2 = new Label(1,0, "Arial 10 point label", arial10format); sheet.addCell(label2); Label label3 = new Label(2, 0, "Another Arial 10 point label", arial10format); sheet.addCell(label3); // Create a cell format for Times 16, bold and italic WritableFont times16font = new WritableFont(WritableFont.TIMES, 16, WritableFont.BOLD, true); WritableCellFormat times16format = new WritableCellFormat (times16font); // Create the label, specifying content and format Label label4 = new Label(3,0, "Times 16 bold italic label", times16format); sheet.addCell(label4); workbook.write(); workbook.close(); } }
You can compile and run the above example in a command window as shown below:
C:\fyicenter>c:\local\jdk-1.8.0\bin\javac -cp .;\fyicenter\jexcelapi\jxl.jar JxlAddLabelFormat.java C:\fyicenter>c:\local\jdk-1.8.0\bin\java -cp .;\fyicenter\jexcelapi\jxl.jar JxlAddLabelFormat
The output file, output.xls, is ready for Microsoft Excel to open.
⇒ Example of Excel Number Format with jxl.jar
⇐ Example of Creating Excel File with jxl.jar
2018-02-21, 2336🔥, 0💬
Popular Posts:
JRE 8 rt.jar is the JAR file for JRE 8 RT (Runtime) libraries. JRE (Java Runtime) 8 is the runtime e...
How to download and install JDK (Java Development Kit) 1.3? If you want to write Java applications, ...
commons-collections4-4.4 -sources.jaris the source JAR file for Apache Commons Collections 4.2, whic...
JRE 8 deploy.jar is the JAR file for JRE 8 Java Control Panel and other deploy tools. JRE (Java Runt...
JDK 11 jdk.xml.dom.jmod is the JMOD file for JDK 11 XML DOM module. JDK 11 XML DOM module compiled c...