The example of this article tells the method of dividing the Excel file in accordance with certain rules. Share it for everyone for your reference. The specifics are as follows:
At present, an Excel document needs to read it and divides it according to certain rules. Each record of the segmentation needs to create an excel document separately and write it. It is necessary to ensure the consistency of the cell format format.
Package Edu.bjut.zhutong.excelparser; Import Java.iO.File; Import Java.fileInputStream; Import Java.filenotfoundexception; Import IoException; Import java.io.inputstream; Import jxl.cell; Import jxl.celltype; Import JXL.SHEET; Import JXL.WORKBOOK; Import JXL.Format.alignment; Import JXL.Format.Border; Import JXL.Format.borderlineSESTyle; Import l.Format.Colour; Import jxl.Format.Verticalignment; Import jxl.read.biffException; Import jxl.write.label; Import JXL.Writ3 TableSheet; Import JXL.Write.writableworkbook; Import jxl.write.writeException; Import JXL.Write.biff.RowSexceException; Public Class Excelparser {Public Static Void Main (String [] ARGS) {workbook wb = null; try {// After the ORKBOOK object INPUTSTREAM is = New FileInputStream ("C: /Excel/excel.xls"); wb = workbook.getworkbook (is); // Get the first worksheet object sheet sheet = wb.getSheet (0); // The number of lines and columns of the worksheet int ROWS = Sheet.getrows (); int cols = sheet.getcolumns (); System.out.println ("Total" + ROWS + "line"); System.out.println (" A total of " + COLS +" column "); int counter = 0; // The work table line bands int FileCounts = 1; // The number of excel documents created by the creation (counter <ROWS-1) {// get the counter line All cell cell [] rowcells = sheet.getrow (countre); cell cell0 = rowcells [0]; // The type if (cell0.gettype () == celltype.label) {System.out. Println ("Analysis of" + filecounts + "files ...."); // Create a new excel document file file = new file ("C:/Excel/Excel" + FileCounts + ".xls"); wwb = workbook.createworkbook (file); // Set the worksheet of the Excel document wingableSheet Ws = wwb.createSheet ("sheet1", 0); // The first line merges 0 to 8 of WS.Mergecells (0, 0 , 8, 0); // Set the column width WS.SetColumnView (6, 10); ws.setColumnView (7, 45); ws.setcolumnView (8, 27); // In the table in the table, first write the title for (int k = 0; k <roucells.length; k ++) {// Create the WritableFont object to format the font. Writablefont WF = New Writablefont (Writablefont.createfont ("Songti"), 20, WritableFont.bold, False); // Use WritableFont to create a unit formatting object Writablellformat WCF = N ewwritablellFormat (wf); // Setting level alignment WCF .setalignment (alignment.centre); // Set vertical alignment method wcf.setVerticalignment (verticalignment.centre); // Set borders and color wcf.SetBorder (border.ALL, BorderLinest yle.thin, colorblack); Cell cell = rowcells [K]; Label Label = New Label (K, 0, Cell.GetContents (), WCF); // Add cells to ws.addcell (label) in the table; // Set the first line of ws.setRowView (0, 30*20, false);} // Write data to the newly built table, and write the header for (int C = 0; c <cols; c ++) {string color = sheet.getcell ( c, 1) .getConttents (); Writablefont WF = New Writablefont (WritableFont.createfont ("Song style"), 12, WritableFont.bold, FALSE); ABLELLFORMAT (WF); WCF.Setalignment (alignment.centre) ; WCF.SetVerticalignment (verticalign.centre); WCF.SetBorder (border.all, borderLinestyle.thin, colorblack); WS.Addcell Con, wcf); ws.setrowView (1, 1, 18*20, false);} int Rowcounts = 1; // Used to traverse 50 counters ++; // Move the cursor to the next line if (counter == 1) // To the third line, the second line does not need to handle the counter = 2; int Rowindex = 2; // The cursor of each Excel document rowcells = sheet.getRow (countr); cell0 = rowcells [0]; space (cell0.gettype ( ) == Celltype.number && Countr <ROWS-1) {rowcells = sheet.getRow (countre); for (int k = 0; k <roucells.length; k ++) {ingablefont wf = new WR. iTablefont (WritableFont.createfont (" Song style "), 12, writablefont.no_bold, false); WritablecellForformat WCF = New WritablellFormat (WF); wcf.setalignment (alignment.centre); WCF.SetVertical LIGNMENT (VerticalIlign.centre); wcf.setBorder (border.all, borderLineStyle. Thin, colorblack); label label = new label (k, rowindex, rowcells [k] .GetContents (), WCF); ws.addcell (label);} // The border used to handle the remarks {WritableFont w f = New Writablefont (Writablefont.createfont ("Song style"), 12, writablefont.no_bold, false); writeCellformat WCF = New WritablellFormat (WF); WCF.Setalignm ENT (Alignment.centre); WCF.SetVerticalignment (verticalignment. typere); WCF. SetBorder (Border.ALL, BorderLinestyle.thin, Colour.black); Label Label = New Label (8, Rowindex, "", WCF); ws.addcell (label);} , 18*20, false );; counter ++; cell0 = sheet.getRow (countrow [0];} wwb.write (); wwb.close (); The end of the line ... " );} Catch (FILENOTFOUNDEXCEPTION E) {e.printstacktrace ();} Catch (biffexception E) {e.printstacktrace ();} Catch (IOEXCEPTION E) {E.printstac ktrace ();} Catch (RowsexcededException E) {e. Printstacktrace ();} Catch (WriteException E) {e.printstacktrace ();} Finally {wb.close (); // Close the Workbook object}}}}}}It is hoped that this article is helpful to everyone's Java program design.