This article implements the use of Java to get objects from the database and into the collection,
Then output to Excel, and set the style
1 Packagecom.webwork;2 3 ImportJava.io.File;4 Importjava.io.IOException;5 Importjava.util.List;6 7 ImportOrg.jdom.output.Format;8 9 ImportJXL. Workbook;Ten ImportJxl.write.Label; One ImportJxl.write.WritableCellFormat; A ImportJxl.write.WritableFont; - ImportJxl.write.WritableSheet; - ImportJxl.write.WritableWorkbook; the Importjxl.write.WriteException; - Importjxl.write.biff.RowsExceededException; - - Public classJxls { + Public Static voidMain (string[] args) { - Try { + NewJxls (). Writeexcel (); A}Catch(rowsexceededexception e) { at e.printstacktrace (); -}Catch(WriteException e) { - e.printstacktrace (); -}Catch(IOException e) { - e.printstacktrace (); - } in } - Public voidWriteexcel ()throwsrowsexceededexception, WriteException, ioexception{ to NewJdbc (). Addlist (); +list< student> stulists =jdbc.getstulist (); -Writableworkbook book =NULL; the *Book = Workbook.createworkbook (NewFile ("E:/io/stuinfo.xls")); $ Panax NotoginsengWritablesheet sheet = book.createsheet ("Studentinfo", 0); - the /* + * Format setting style A */ the //Set Font +Writablefont font1 =NewWritablefont (writablefont.arial,18, writablefont.bold); -Writablefont Font2 =NewWritablefont (writablefont.arial, 13, writablefont.bold); $ //Formatting cells $Writablecellformat FORMAT1 =NewWritablecellformat (font1); -Writablecellformat FORMAT2 =NewWritablecellformat (font2); -Writablecellformat FORMAT3 =NewWritablecellformat (); the //Set Style -Format1.setalignment (Jxl.format.Alignment.CENTRE);//Horizontal AlignmentWuyiFormat1.setverticalalignment (Jxl.format.VerticalAlignment.CENTRE);//Vertical Alignment theFormat1.setbackground (Jxl.format.Colour.LIGHT_BLUE);//Set Background color -Format1.setborder (Jxl.format.Border.ALL, Jxl.format.BorderLineStyle.THIN);//Set Border Wu - About //Merge cell First row $Sheet.mergecells (0, 0, 3, 0); -Label label =NewLabel (0, 0, "yc95 Class information Sheet", FORMAT1); - Sheet.addcell (label); - A //set a small title per column +String [] str = {"id", "name", "Sex", "age"}; the for(inti = 0; i < str.length; i++) { -Sheet.addcell (NewLabel (i, 1, Str[i])); $Sheet.setcolumnview (I, 20);//Set column widths the } the the for(inti = 2; I < stulists.size (); i++) { theStudent s =Stulists.get (i); - //System.out.println (s); inLabel Label1 =NewLabel (0, I, S.getid ()); theLabel Label2 =NewLabel (1, I, S.getname ()); theLabel label3 =NewLabel (2, I, S.getsex ()); AboutLabel Label4 =NewLabel (3, I, S.getage ()); the Sheet.addcell (Label1); the Sheet.addcell (label2); the Sheet.addcell (LABEL3); + Sheet.addcell (label4); - } the book.write ();Bayi book.close (); theSystem.out.println ("Insert Excel data successfully!!!" "); the } -}
Here's what you get in Excel
After outputting to Excel, the corresponding is to fetch data from Excel
1 Packagecom.webwork;2 3 ImportJava.io.File;4 Importjava.io.IOException;5 Importjava.util.List;6 7 ImportJXL. Cell;8 ImportJXL. Sheet;9 ImportJXL. Workbook;Ten Importjxl.read.biff.BiffException; One A Public classReadexcel { - Public Static voidMain (string[] args) { -List<student> stulists =jdbc.getstulist (); theWorkbook book =NULL; - Try { -Book = Workbook.getworkbook (NewFile ("E:/io/stuinfo.xls")); -}Catch(biffexception e) { + e.printstacktrace (); -}Catch(IOException e) { + e.printstacktrace (); A } atSheet[] Sheets =book.getsheets (); - for(inti = 0; i < sheets.length; i++) { -Sheet Sheet =Sheets[i]; - intRows=sheet.getrows (); - for(intj = 1; J < rows; J + +) { -Cell[] Cells=Sheet.getrow (j); in for(intk = 0; K < Cells.length; k++) { -Cell cell =Cells[k]; toString ID =cell.getcontents (); +System.out.print (id+ "\ T"); - } the * System.out.println (); $ }Panax Notoginseng - } the + A the } +}
The results of the output are as follows:
Java output and input from Excel