using Jxl.rar
1. Locate the Jxl.jar package and import the program.
Find a dependent URL: Maven Warehouse
2. Read Excel
Public Static voidReadexcel ()throwsbiffexception, ioexception{//Create a list to store the read contentList List =NewArrayList (); Workbook RWB=NULL; Cell Cell=NULL; //Create an input streamInputStream stream =NewFileInputStream ("D:\\testjxl.xls"); //get the Excel file objectRWB =Workbook.getworkbook (stream); //gets the default first for the specified worksheet of a fileSheet Sheet = Rwb.getsheet (0); //Number of Rows (table header directory not required, starting from 1) for(inti=0; I<sheet.getrows (); i++){ //create an array to store the values for each columnstring[] str =Newstring[sheet.getcolumns ()]; //Number of columns for(intj=0; J<sheet.getcolumns (); J + +){ //get the value of row i, column JCell =Sheet.getcell (j,i); STR[J]=cell.getcontents (); } //Save the newly acquired column to listList.add (str); } for(intI=0;i<list.size (); i++) {string[] str=(string[]) list.get (i); for(intj=0;j<str.length;j++) {System.out.println (str[j]); } } }
3. Writing to Excel
Public Static voidWriteexcel () {string[] title= {"Number", "Product name", "Product price", "Product Quantity", "Production Date", "origin", "whether export"}; Try { //Get start time LongStart =System.currenttimemillis (); //the path of the output ExcelString FilePath = "D:\\testjxl.xls"; //Create an Excel workbookWritableworkbook WWB; //Create a new JXL file, which is generated under the D disk Testjxl.xlsOutputStream OS =NewFileOutputStream (FilePath); WWB=workbook.createworkbook (OS); //Add the first worksheet and set the first sheet nameWritablesheet sheet = wwb.createsheet ("Product List", 0); Label label; for(inti=0;i<title.length;i++){ //Label (x, Y, z) represents the cell's x+1 column, line y+1, content Z//indicates the location and contents of a cell in a child object of a Label objectLabel =NewLabel (i,0, Title[i]); Label=NewLabel (i, 0, Title[i], GetHeader ()); //add a defined cell to a worksheetSheet.addcell (label); } //The following is the fill data /** Save number to cell, need to use jxl.write.Number * must use its full path, otherwise an error will occur **/ //Fill Product numberJxl.write.Number number =NewJxl.write.Number (0,1,20071001); Sheet.addcell (number); //Populate product NameLabel =NewLabel (1, 1, "Golden Pigeon melon Seeds"); Sheet.addcell (label); /** Defines the public format for displaying amounts * JXL will automatically be rounded * For example 2.456 will be formatted as 2.46, 2.454 will be formatted as 2.45 * */Jxl.write.NumberFormat NF=NewJxl.write.NumberFormat ("#,###.00"); Jxl.write.WritableCellFormat WCF=NewJxl.write.WritableCellFormat (NF); //fill the price of the productJxl.write.Number NB =NewJxl.write.Number (2,1,200000.45, WCF); Sheet.addcell (NB); //number of products filledJxl.write.Number numb =NewJxl.write.Number (3,1,200); Sheet.addcell (numb); /** Define common formats for displaying dates * such as: YYYY-MM-DD hh:mm **/SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd"); String newdate= Sdf.format (NewDate ()); //Fill Production DateLabel =NewLabel (4,1, newdate); Sheet.addcell (label); //fill the OriginLabel =NewLabel (5,1, "Xi ' an, Shaanxi"); Sheet.addcell (label); /** Display Boolean value **/Jxl.write.Boolean BOOL=NewJxl.write.Boolean (6,1,true); Sheet.addcell (BOOL); /** Merge Cells * through Writablesheet.mergecells (int x,int y,int m,int n); X+1 column, y+1 row to m+1 column, n+1 row merge * **/Sheet.mergecells (0,3,2,3); Label=NewLabel (0,3, "merge three cells"); Sheet.addcell (label); /** * Define common font format * by getting the style of a font as a template * first through Web.getsheet (0) to obtain the A sheet * Then gets the second column of the first sheet, and the first line is the font for the "Product name" **/CellFormat CF= Wwb.getsheet (0). Getcell (1, 0). Getcellformat (); Writablecellformat WC=NewWritablecellformat (); //Set Centerwc.setalignment (alignment.centre); //Set Border linesWc.setborder (Border.all, Borderlinestyle.thin); //set the background color of a cellWc.setbackground (Jxl.format.Colour.RED); Label=NewLabel (1, 5, "Font", WC); Sheet.addcell (label); //Set FontJxl.write.WritableFont Wfont =NewJxl.write.WritableFont (Writablefont.createfont ("Official script"), 20); Writablecellformat Font=NewWritablecellformat (Wfont); Label=NewLabel (2,6, "Official script", font); Sheet.addcell (label); //Write DataWwb.write (); //Close FileWwb.close (); LongEnd =System.currenttimemillis (); System.out.println ("----The total time to complete the operation is:" + (End-start)/1000); } Catch(Exception e) {System.out.println ("---An exception occurred---"); E.printstacktrace (); } }
4.Excel style
Public StaticWritablecellformat GetHeader () {Writablefont font=NewWritablefont (Writablefont.times, ten, writablefont.bold);//Defining Fonts Try{font.setcolour (colour.blue);//Blue Font}Catch(WriteException E1) {//TODO automatically generates catch blocksE1.printstacktrace (); } Writablecellformat Format=NewWritablecellformat (font); Try{format.setalignment (Jxl.format.Alignment.CENTRE);//Center left and rightFormat.setverticalalignment (Jxl.format.VerticalAlignment.CENTRE);//Center up and downFormat.setborder (Border.all,borderlinestyle.thin,colour.black);//Black BorderFormat.setbackground (Colour.yellow);//Yellow background}Catch(WriteException e) {//TODO automatically generates catch blocksE.printstacktrace (); } returnformat; }
Problem succinct--java reading and writing Excel