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.
Java Operations Excel is the most commonly used is JXL, this first look at the JXL bar. First you can download the latest Jxl.jar to Http://www.andykhan.com/jexcelapi/download.html, which has its jar package and API, there is a small example, you can refer to.
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/ */ Public classSimpleexceldemo {/*** Java generates Excel workbook files to local directory *@paramFileName *@return * @throwsException*/ Public StaticString Exportexcel (String fileName)throwsException {//first create a file to save the Excel tableFile File =NewFile ("d:\\data\\" + FileName + ". xls"); //Create a file output stream for writing out tables to a local folderOutputStream 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, the first page is 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-after introduction) * So sit down. When labeled as 0, it can be understood as a table header oh. */Label Cell=NewLabel (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, first make the watch head.Cell =NewLabel (1, 0, "age");//1 rows 2 columnsSheet.addcell (cell); Cell=NewLabel (2, 0, "birthday");//1 Rows 3 columnsSheet.addcell (cell); Cell=NewLabel (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, because you don't care when you write, but when you read an Excel form into the program, there are no these types to help you judge,. Various conversion Exceptions: */Cell=NewLabel (0, 1, "Liu Day");//2 rows 1 ColumnsSheet.addcell (cell); number Age=NewNumber (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 Wrap, border, 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=NewDateTime (2, 1, D, WF);//2 rows 3 columns (Birthday: datetime type)Sheet.addcell (birthday); Cell=NewLabel (3, 1, "http://www.cnblogs.com/liuyitian/");//2 rows 4 columnsSheet.addcell (cell); Workbook.write (); //writing to an Excel worksheetWorkbook.close ();//close the Excel worksheet, and also close the IO stream, and do not forget. return"Write done, it is so simple, you try it yourself?" "; } /**Simple Test **/ Public Static voidMain (string[] args) {Try{System.out.println (Simpleexceldemo.exportexcel ("Simple Excel Production")); } Catch(Exception e) {System.out.println ("Write Failed" +e.getmessage ()); } }}
:
After work, hurry to withdraw.
< Span style= "color: #808000;" > PS: Welcome reprint, Reprint please indicate the source: http://www.cnblogs.com/liuyitian/p/4111003.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