ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.io.OutputStream;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportJXL. Sheet;ImportJXL. Workbook;Importjxl.format.Alignment;ImportJxl.format.Border;ImportJxl.format.BorderLineStyle;ImportJxl.format.CellFormat;ImportJxl.format.Colour;ImportJxl.write.Boolean;ImportJxl.write.Label;ImportJxl.write.Number;ImportJxl.write.NumberFormat;ImportJxl.write.WritableCellFormat;ImportJxl.write.WritableSheet;ImportJxl.write.WritableWorkbook; Public classExcel {/** * * @paramFilePath File Save path *@paramFileName file name*/ Public voidexcelexportutil (String filepath,string fileName) {//Excel File Table headerString[] title = {"Number", "Name", "Gender", "Age", "Add Time", "Test with decimal point", "Test font Color", "Get template, change font color", "Test cell merge"}; Try { //Create an Excel workbookWritableworkbook WWB; //Create a new JXL fileOutputStream OS =NewFileOutputStream (filepath+fileName); WWB=workbook.createworkbook (OS); //Add the first worksheet and set the first sheet nameWritablesheet sheet = wwb.createsheet ("user", 0); Label label; for(inti = 0; i < title.length; i++) { //indicates the location and contents of a cell in a child object of a Label objectLabel =NewLabel (i, 0, Title[i]); //add a defined cell to a worksheetSheet.addcell (label); } //Fill NumberNumber code =NewNumber (0,1,001); Sheet.addcell (code); //Fill NameLabel =NewLabel (1, 1, "Zhang San"); Sheet.addcell (label); //Add Gender (Bollean type: True male, false female)Boolean sex =NewBoolean (2,1,true); Sheet.addcell (Sex); //Add AgeNumber age =NewNumber (3,1,19); Sheet.addcell (age); //Add TimeSimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd"); String newdate= Sdf.format (NewDate ()); Label=NewLabel (4, 1, newdate); Sheet.addcell (label); /** With a decimal point type*/NumberFormat NF=NewNumberFormat ("#.###"); Writablecellformat WCF=NewWritablecellformat (NF); Number NB=NewNumber (5, 1, 2.456, WCF); Sheet.addcell (NB); /** Test cell style changes*/Writablecellformat WC=NewWritablecellformat (); //Set Centerwc.setalignment (alignment.centre); //set the background color of a cellWc.setbackground (colour.red); //Set Border linesWc.setborder (Border.all, Borderlinestyle.thin); Label=NewLabel (6, 1, "Font, color", WC); Sheet.addcell (label); /** * Define the public font format by getting a font style to act as a template first by Web.getsheet (0) to obtain the first sheet * and then get the Nineth column of the first sheet, section One line is the font "test font color"*/CellFormat CF= Wwb.getsheet (0). Getcell (6, 1). Getcellformat (); Label=NewLabel (7, 1, "through template", CF); Sheet.addcell (label); /** Merge cells via writablesheet.mergecells (int x,int y,int m,int n); * Indicates that it will be merged from column x+1, y+1 row to m+1 column, n+1 row*/Sheet.mergecells (8,1,10,1); Label=NewLabel (8,1, "Test cell merge"); Sheet.addcell (label); //Write DataWwb.write (); //CloseWwb.close (); } Catch(Exception e) {e.printstacktrace (); } } /** * * @paramFilePath File Save path *@paramFileName file name*/ Public voidreadexcel (String filepath,string fileName) {Try{File File=NewFile (Filepath+filename);//To create a file objectWorkbook WB = workbook.getworkbook (file);//get an Excel Workspace object from a file streamSheet Sheet= Wb.getsheet (0);//get page from workspace (Sheet)//gets the number of rows for the worksheet for use in the following loop intRowNum =sheet.getrows (); /** Cycle Print * i=1,i for 0 O'Clock printed table header*/ for(inti = 1; i < RowNum; i++) { /** Get the corresponding value Sheet.getcell (X, i). getcontents () * Gets the value of the x+1 column*/String Code= Sheet.getcell (0, i). getcontents (); String name= Sheet.getcell (1, i). getcontents (); String Sex= Sheet.getcell (2, i). getcontents (); String Age= Sheet.getcell (3, i). getcontents (); if(Sex.equals ("true") ) {sex= "Male"; }Else{Sex= "female"; } System.out.println ("Number" +code+ ", Name" +name+ ", Gender" +sex+ ", age" +Age ); } }Catch(Exception e) {e.printstacktrace (); } }}
Read and write Java Excel files