Package Com.tanzhou.aduio;//Initiate a packageImport Java.io.File;//Introduction Classimport Java.io.ioexception;import java.util.scanner;import jxl. Cell;import JXL. Sheet;import JXL. Workbook;import Jxl.format.UnderlineStyle;//import jxl.read.biff.BiffException;import jxl.write.dateformat;import jxl.write.datetime;import jxl.write.label;import Jxl.write.NumberFormat; Import Jxl.write.writablecellformat;import Jxl.write.WritableFont;//import jxl.write.WritableImage;import Jxl.write.writablesheet;import Jxl.write.WritableWorkbook;//import jxl.write.WriteException;//import jxl.write.biff.RowsExceededException; Public classTextoperatorexcel {//classes that manipulate Excel files /** * Generate an Excel file*/ Public Static voidWriteexcel (String fileName) {Writableworkbook WWB=NULL; Try { //Create a writable Workbook (Workbook) objectWWB = Workbook.createworkbook (NewFile (fileName)); } Catch(IOException e) {//Capturing stream ExceptionsE.printstacktrace (); } if(WWB! =NULL) { //Create a writable worksheet//Workbook's Createsheet method has two parameters, the first is the name of the worksheet, and the second is the position of the worksheet in the workbookWritablesheet ws = Wwb.createsheet ("Sheet1",0); for(inti =0; I <Ten; i++) {//loop to add cells for(intj =0; J <5; J + +) {Label LABELC=NewLabel (J, I,"This is the first"+ (i +1) +"Line, Section"+ (J +1) +"column"); Try{Ws.addcell (LABELC);//add a generated cell to a worksheet}Catch(Exception e) {//Catching exceptionsE.printstacktrace (); } } } Try{wwb.write ();//writing files from memoryWwb.close ();//writing files from memory}Catch(Exception e) {//Catching exceptionsE.printstacktrace (); }} System. out. println ("To generate an Excel file:"+filename+"Success!"); } /** * Write content to * @param fileName * @throws Exception*/ Public Static voidWritecontenttoexcel (String fileName) throws exception{File tempfile=NewFile (fileName); Writableworkbook Workbook=Workbook.createworkbook (tempfile); Writablesheet sheet= Workbook.createsheet ("Testcreateexcel",0); //Some temporary variables that are used to write to ExcelLabel l=NULL; Jxl.write.Number N=NULL; Jxl.write.DateTime D=NULL; //some of the predefined fonts and formats that are best in the same Excel do not have too many formatting glyph sizes Bold Italic underline colorWritablefont HeaderFont =NewWritablefont (Writablefont.arial, A, Writablefont.bold,false, Underlinestyle.no_underline, Jxl.format.Colour.BLUE); Writablecellformat Headerformat=NewWritablecellformat (HeaderFont); Writablefont Titlefont=NewWritablefont (Writablefont.arial,Ten, Writablefont.no_bold,false, Underlinestyle.no_underline, Jxl.format.Colour.RED); Writablecellformat TitleFormat=NewWritablecellformat (Titlefont); Writablefont Detfont=NewWritablefont (Writablefont.arial,Ten, Writablefont.no_bold,false, Underlinestyle.no_underline, Jxl.format.Colour.BLACK); Writablecellformat Detformat=NewWritablecellformat (Detfont); NumberFormat NF=NewNumberFormat ("0.00000");//the format used for numberWritablecellformat Priceformat =NewWritablecellformat (Detfont, NF); DateFormat DF=NewDateFormat ("YYYY-MM-DD");//used for the dateWritablecellformat DateFormat =NewWritablecellformat (Detfont, DF); L=NewLabel (0,0,"Excel file for testing", Headerformat);//create some cells and add them to sheetSheet.addcell (L); //Add title intcolumn=0; L=NewLabel (column++,2,"title", TitleFormat); Sheet.addcell (l); L=NewLabel (column++,2,"Date", TitleFormat); Sheet.addcell (l); L=NewLabel (column++,2,"Currency", TitleFormat); Sheet.addcell (l); L=NewLabel (column++,2,"Price", TitleFormat); Sheet.addcell (l); //Add Content intI=0; Column=0; L=NewLabel (column++, i+3,"title"+I, Detformat); Sheet.addcell (l); D=NewDateTime (column++, i+3,Newjava.util.Date (), DateFormat); Sheet.addcell (d); L=NewLabel (column++, i+3,"CNY", Detformat); Sheet.addcell (l); N=NewJxl.write.Number (column++, i+3,5.678, Priceformat); Sheet.addcell (n); I++; Column=0; L=NewLabel (column++, i+3,"title"+I, Detformat); Sheet.addcell (l); D=NewDateTime (column++, i+3,Newjava.util.Date (), DateFormat); Sheet.addcell (d); L=NewLabel (column++, i+3,"SGD", Detformat); Sheet.addcell (l); N=NewJxl.write.Number (column++, i+3,98832, Priceformat); Sheet.addcell (n); //set the width of a columncolumn=0; Sheet.setcolumnview (Column++, -); Sheet.setcolumnview (Column++, -); Sheet.setcolumnview (Column++,Ten); Sheet.setcolumnview (Column++, -); Workbook.write (); Workbook.close (); System. out. println ("Content Write"+filename+"Success"); } Public Static voidReadexcelinfo (String fileName) throws Exception {//How many rows of Excel files are availableWorkbook book = Workbook.getworkbook (NewFile (FileName));//construct a Workbook (Workbook) objectSheet Sheet = Book.getsheet (0); //get the first row of cells in the first column//get the first sheet object intColumnum = Sheet.getcolumns ();//get the number of columns intRowNum = Sheet.getrows ();//get the number of rowsSystem. out. println (Columnum); System. out. println (rownum); for(inti =0; i < rownum; i++)//loop to read and write { for(intj =0; J < Columnum; J + +) {Cell cell1=Sheet.getcell (J, I); String result=cell1.getcontents (); System. out. Print (result); System. out. Print ("\ t"); } System. out. println (); } book.close ();//Close (Workbook) object } Public Static voidMain (string[] args) {//Java program main entrance Try{System. out. println ("1. Create an Excel file, enter the Excel file name (including path and suffix)"); Scanner Scan=NewScanner (System.inch); Final String FileName= Scan.next ();//Get keyboard valueWriteexcel (FileName);//call the Generate Excel methodSystem. out. println ("2. Writing content to an Excel file"); Writecontenttoexcel (fileName);//call to write content to an Excel methodSystem. out. println ("3. Reading Excel Files"); Readexcelinfo (FileName); } Catch(Exception e) {//Catching exceptionsE.printstacktrace (); } }}
33rd section (java-Operations Excel to achieve enterprise-class batch processing big data)