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 Creating Excel File with jxl.jar
Where can I find an example Java code that uses jxl.jar to create a new Excel file?
✍: FYIcenter.com
You can follow these suggestions and example to create a new Excel file with jxl.jar:
1. Create a writable workbook file using the factory method on the Workbook class:
WritableWorkbook workbook = Workbook.createWorkbook(new File("output.xls"));
The API can also be used to send the workbook directly to an output stream eg. from a web server to the user's browser. If the HTTP header is set correctly, then this will launch Excel and display the generated spreadsheet.
2. Create a sheet (tab) for the workbook. Again, this is a factory method, which takes the name of the sheet and the position it will occupy in the workbook. The code fragment below creates a sheet called "First Sheet" at the first position -
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
3. Now all that remains is to add the cells into the worksheet. This is simply a matter of instantiating cell objects and adding them to the sheet. The following code fragment puts a label in cell A3, and the number 3.14159 in cell D5.
sheet.addCell(new Label(0, 2, "A label record")); sheet.addCell(new Number(3, 4, 3.1459));
Note that cell location index starts from 0. So location (0,2) is cell A3, and (3,4) is cell D5.
4. Once you have finished adding sheets and cells to the workbook, you call write() on the workbook, and then close the file. This final step generates the output file (output.xls in this case) which may be read by Excel. If you call close() without calling write() first, a completely empty file will be generated.
workbook.write(); workbook.close();
Here is the complete source code of example program JxlCreateExcel.java:
// Copyright (c) FYIcenter.com // Example of creating a new Excel file public class JxlCreateExcel { 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); sheet.addCell(new jxl.write.Label(0, 2, "A label record")); sheet.addCell(new jxl.write.Number(3, 4, 3.1459)); 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 JxlCreateExcel.java C:\fyicenter>c:\local\jdk-1.8.0\bin\java -cp .;\fyicenter\jexcelapi\jxl.jar JxlCreateExcel
The output file, output.xls, is ready for Microsoft Excel to open.
⇒ Example of Excel Label Format with jxl.jar
⇐ Java Source Code Example for jxl.jar
2018-02-21, 2268🔥, 0💬
Popular Posts:
JRE 5 sunjce_provider.jar is the JAR file for JRE 5 Sun JCE Provider, which provides implementations...
JDK 11 jdk.charsets.jmod is the JMOD file for JDK 11 Charsets module. JDK 11 Charsets module compile...
How to download and install JDK (Java Development Kit) 7? If you want to write Java applications, yo...
JDK 11 java.rmi.jmod is the JMOD file for JDK 11 RMI (Remote Method Invocation) module. JDK 11 RMI m...
Jaxen, Release 1.1.1, is an open source XPath library written in Java. It is adaptable to many diffe...