Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
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 (322)
Collections:
Other Resources:
Example of Excel Date Format with jxl.jar
Where can I find an example Java code that uses jxl.jar to add date cell format to a new Excel file?
✍: FYIcenter.com
You can follow these suggestions and example to add date cell format to a new Excel file with jxl.jar:
Dates are handled similarly to numbers, taking in a format compatible with that used by the java.text.SimpleDateFormat class. In addition, several predefined date formats are specified in the jxl.write.DateFormat class.
As a brief example, the below code fragment illustrates placing the current date and time in cell A7 using a custom format:
// Get the current date and time from the Calendar object Date now = Calendar.getInstance().getTime(); DateFormat customDateFormat = new DateFormat ("dd MMM yyyy hh:mm:ss"); WritableCellFormat dateFormat = new WritableCellFormat (customDateFormat); DateTime dateCell = new DateTime(0, 6, now, dateFormat); sheet.addCell(dateCell);
As with numbers, font information may be used to display the date text by using the overloaded constructors on WritableCellFormat.
Here is the complete source code of example program JxlAddDateFormat.java:
// Copyright (c) FYIcenter.com import java.util.*; import jxl.write.*; import jxl.write.Number; // Example of adding formatting properties for Dates in a new Excel file public class JxlAddDateFormat { 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); // Get the current date and time from the Calendar object Date now = Calendar.getInstance().getTime(); DateFormat customDateFormat = new DateFormat ("dd MMM yyyy hh:mm:ss"); WritableCellFormat dateFormat = new WritableCellFormat (customDateFormat); DateTime dateCellA7 = new DateTime(0, 6, now, dateFormat); sheet.addCell(dateCellA7); // 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); WritableCellFormat dateFormat16 = new WritableCellFormat (times16font, customDateFormat); DateTime dateCellA9 = new DateTime(0, 8, now, dateFormat16); sheet.addCell(dateCellA9); 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 JxlAddDateFormat.java C:\fyicenter>c:\local\jdk-1.8.0\bin\java -cp .;\fyicenter\jexcelapi\jxl.jar JxlAddDateFormat
The output file, output.xls, is ready for Microsoft Excel to open.
⇒ Example of Read Excel File with jxl.jar
⇐ Example of Excel Number Format with jxl.jar
2018-02-28, ∼4672🔥, 0💬
Popular Posts:
What Is HttpComponents httpcore-4.4.6.jar? HttpComponents httpcore-4.4.6.jar is the JAR file for Apa...
JLayer is a library that decodes/plays/converts MPEG 1/2/2.5 Layer 1/2/3 (i.e. MP3) in real time for...
How to display XML element type information with the jaxp\TypeInfoWriter.java provided in the Apache...
JDK 11 java.base.jmod is the JMOD file for JDK 11 Base module. JDK 11 Base module compiled class fil...
JDK 11 jdk.crypto.ec.jmod is the JMOD file for JDK 11 Crypto EC module. JDK 11 Crypto EC module comp...