Java generates a simple Excel workbook

Source: Internet
Author: User

Objective:

  The code is based on the actual needs, last week to finish a transfer of external movie Coupon Interface Project, this week the product also Excel table, the general content is: The ticket belongs to the cinema, image URL, and other information produced as an Excel table, the data of each synchronization came to him analysis.

JXL: Full name JAvaexcelapi, is a Java open source class library used to read and write modified Excel, which is relatively simple to use.

Jar Package: http://www.andykhan.com/jexcelapi/download.html

Here is just finished a simple can not be any more simple demo, no additional style, work, style this weekend to adjust slowly

Demo Code:

PackageCom.java.jexcel;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.io.OutputStream;ImportJava.util.Calendar;ImportJava.util.Date;ImportJxl. Workbook;ImportJxl.write.DateFormats;ImportJxl.write.DateTime;ImportJxl.write.Label;ImportJxl.write.Number;ImportJxl.write.WritableCellFormat;ImportJxl.write.WritableSheet;ImportJxl.write.WritableWorkbook;/***@authorLiuyt * @date 2014-11-20 pm 5:04:48 * Bolgshttp://www.cnblogs.com/liuyitian/*/PublicClassSimpleexceldemo {/*** Java generates Excel workbook files to local directory *@paramFileName *@return*@throwsException*/Publicstatic string Exportexcel (String fileName)ThrowsException {//First, create a file that holds the Excel table.New File ("d:\\data\\" + FileName + ". xls");//Create a file output stream for writing out a table to a local folder outputstream out =NewFileOutputStream (file);//Create a read-write workbook (equivalent to creating an Excel table. Abstract class, cannot be created with new) Writableworkbook workbook =Workbook.createworkbook (out);//Create a new page (an Excel file can contain multiple pages oh.) Parameter one: The name of this page; parameter two: number of pages, first page 0) writablesheet sheet = workbook.createsheet ("Simple Excel Production", 0);/*** Create a label, equivalent to a cell, used to populate this page * (parameter one: column coordinates, 0 start; parameter two: row coordinates, 0 start; parameter three: cell contents; parameter four: optional, one format tool-later) * So when the line coordinates are 0, it can be understood as the table header.*/Label cell =New Label (0, 0, "name");//1 rows 1 Columns//Add cells to this page (there should be a "step one" cell in the top left corner of the table)Sheet.addcell (cell);//Bang make the table first. Cell =New Label (1, 0, "age");//1 Rows 2 columnsSheet.addcell (cell); Cell =New Label (2, 0, "birthday");//1 Rows 3 ColumnsSheet.addcell (cell); Cell =New Label (3, 0, "blog address");//1 Rows 4 ColumnsSheet.addcell (cell);/*** Next fill the content, remember the table header settings Oh, don't confuse. * Cell is not only lable one type, there are three other kinds: number,boolean,datetime. Are the strings, numbers, booleans, and time formats individually designed so many types of redundancy? No, you don't care when you write, but when you read an Excel into the program, there are no these types to help you judge,. Various conversion Exceptions:*/Cell =New Label (0, 1, "Liu Day");//2 rows 1 ColumnsSheet.addcell (cell); Number age =New Number (1, 1, 23);//2 Rows 2 columns (age: Number Type)Sheet.addcell (age);/*** For date format, you can write directly to a java.util.Date type of time, but the format may not be what you want * JXL yourself have cell formatting class: Writablecellformat, it can set the cell background color, text wrapping, border and style, etc. * You can also pass in a DisplayFormat (display form) to construct a format tool class*/Writablecellformat WF =NewWritablecellformat (DATEFORMATS.FORMAT1); Calendar C =Calendar.getinstance (); C.set (1991, 5, 16); Date d =C.gettime (); DateTime Birthday =New DateTime (2, 1, D, WF);//2 Rows 3 Columns (Birthday: datetime type)Sheet.addcell (birthday); Cell =New Label (3, 1, "http://www.cnblogs.com/liuyitian/");//2 Rows 4 ColumnsSheet.addcell (cell); Workbook.write ();// write Excel worksheet Workbook.close (); // close the Excel worksheet and also close the IO stream, and do not forget. return "write complete, it is so simple, you try it yourself? ";} /** simple test **/public static voidtry {System.out.println ( Simpleexceldemo.exportexcel ("Simple Excel Production" catch (Exception e) {System.out.println ("Write Failed" +e.getmessage ()); } }} 

After work, hurry to withdraw.

  PS: Welcome reprint, Reprint please indicate source:http://www.cnblogs.com/liuyitian/p/4111451.html

                          Writing is not easy, inevitably there are omissions and mistakes, please also be generous, please recommend

                                         Learn a little bit more everyday code less knock a little bit

Java generates a simple Excel workbook

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.