Java operation export Excel (jxl export WritableWorkbook) jxl merged cells, cell settings, cell centering, font, size, line break, merge line, column width, automatic line wrap height, specify a specific string style, etc.

new WritableCellFormat().setWrap(true);//Auto wrap by adjusting width and height

1.1 Requirements Description
MS spreadsheet (Excel) is an important member of Office and a common format for saving statistical data. As an office document, it is bound to involve the exchange of electronic documents. Excel is a very common file format in enterprises, and it is more convenient to print and manage. In a Java application, generating part of the data in Excel format is an important means of seamless connection with other systems.

1.2 Commonly used open source tools for Excel development
In the open source world, there are two sets of relatively influential APIs available, one is POI and the other is jExcelAPI.

1.3 Advantages and disadvantages of comparing open source tools
1.3.1 Advantages and disadvantages of Jxl
   The characteristics of Jxl are described as follows:        

 ● Support all versions of Excel 95-2000     

Generate Excel 2000 standard format     
Support font, number, date operations     
Can modify cell properties     
Support images and charts        
It should be said that the above functions can roughly meet our needs. The most important thing is that this set of API is pure Java and does not depend on the Windows system. Even if it runs under Linux, it can also process Excel files correctly. It should also be noted that this API has limited support for graphics and charts, and only recognizes the PNG format.

1.3.2 Poi advantages and disadvantages
Jakarta's POI Project and Java Excel API can be said to be neck and neck in the open source world, but each has its own advantages and disadvantages. Poi has some small bugs in some details and does not support writing pictures (poi can actually write Pictures, but not as convenient as jxl, more troublesome), other aspects are quite good; and JXL provides support for pictures (but only supports pictures in png format), the problem is that the support for formulas is not very good, but it still provides Added simple formula reading support. So what kind of third-party plug-in to use in your project is completely determined by your application. If your software has a considerable relationship with finance, it is recommended to use POI Project. As far as my current project is concerned, calculation formulas are not used, and pictures may need to be exported, so my choice is JXL.

1.4 Performance comparison and final selection
1.4.1 Memory consumption: (from the network)
Let’s talk about the memory consumption of the JVM virtual machine.
The data volume is 3000 pieces of data, each with 60 columns. The memory size of the JVM virtual machine is 64M.
Use POI: run to 2800 The memory overflow is reported around the article.
Use JXL: all 3000 entries come out, and there is still 21M space in the memory.
It is conceivable that there is still a big gap in memory consumption.
Maybe it is because JXL is recycling resources. It's pretty good.

1.4.2 Speed ​​efficiency (read excel data) (from the network)
file POI loading time consumption POI total time consumption JXL loading time consumption Jxl total time consumption  

File size 57KB 1172 ms 1172 ms 1265 ms 2250 ms

File size 652KB 2297 ms 2313 ms 4406 ms 9750 ms

File size 2.24M 3109ms 3140ms 16313ms 37453ms

1.4.3 Write excel speed and efficiency
    jxl inserts data faster than poi

1.4.4 Function comparison
Compared with the functions provided, JXL is relatively weak. So if the functions to be realized are more complicated, you can consider using POI, but if you only want to generate some large amounts of data, you can consider using JXL or CSV It is also a good choice, but CSV is not a real excel, but jxl inserts data faster than poi.

Writablesheet_jxl adds borders, jxl merges cells, cell settings, cell centering, font, size, line break, merged rows, column width, specifying a specific string style, etc.

public void export() throws Exception
    {         String title = "test.xls";         String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());         String xlsfile = "test_" + time + ".xls";         try         {             // Construct the full path of the temporary file             String fullFileName = System.getProperty("java.io.tmpdir") + "/" + xlsfile;             // Create the Excel file             java.io.OutputStream os = new FileOutputStream(fullFileName);             / /Create a writable excel file object             WritableWorkbook workbook = Workbook.createWorkbook(os);             //Use the first worksheet, name it "title"             WritableSheet sheet = workbook.createSheet(title, 0);





            








            
            //Set the height of the first row to 14
            sheet.setRowView(0, 300);
            sheet.setRowView(1, 300);
            //Set the width of the first column to 28
            sheet.setColumnView(0, 28);
            
            / /Set the font style
            //The first parameter indicates the selected font
            //The second parameter indicates the font size
            //The third parameter indicates the bold style, there are two styles of BOLD and NORMAL
            //The fourth parameter indicates whether italic , here true means italic
            // The fifth parameter indicates the underline style
            // The sixth parameter indicates the color style
            WritableFont bold = new WritableFont(WritableFont.createFont("Arial"), 11, WritableFont.BOLD, false, UnderlineStyle. NO_UNDERLINE, Colour.WHITE);
            WritableCellFormat wcfFormat = new WritableCellFormat(bold);
            //Custom cell background color "#2f75b5"
            Color color = Color.decode("#2f75b5");
            workbook.setColourRGB(Colour.ORANGE, color.getRed(), color.getGreen(), color.getBlue());
            Color backgroundColour = Colour.ORANGE;
            wcfFormat.setBackground (backgroundColour);
            
            wcfFormat.setBorder(Border.ALL, BorderLineStyle.THIN, jxl.format.Colour.WHITE);//Set the border style and color
            wcfFormat.setAlignment(jxl.format.Alignment.CENTRE);//In the cell The content is centered horizontally
            wcfFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//The content in the cell is vertically centered
            //The cell is in string format! The first represents the number of columns, the second represents the number of rows, the third represents the content to be written, and the fourth represents the font format (0 represents the first row or column of excel) // Label label =  
new Label(0, 0, "Load device", wcfFormat); //(0,0) here means the first row and first column of the table
// sheet.addCell(label);
            
            //Merge cells, the merge can be either horizontal or vertical
            //The first data here represents the second column, the second data represents the first row, the The three data represent the fourth column, and the fourth data represents the second row
            //sheet.mergeCells(1, 0, 3, 1);
            sheet.mergeCells(0, 0, 0, 1);
            Label label = new Label( 0, 0, "Load device", wcfFormat);
            sheet.addCell(label);

            //关闭对象,释放资源
            workbook.write();
            workbook.close();
            os.close();
        }
        catch (Exception e)
        {
            PContext.printStackTrace(e);
        }
        
        
        xlsfile = java.net.URLEncoder.encode(xlsfile, "utf-8");
        HttpServletRequest request = HttpContext.getRequest();
        HttpServletResponse response = HttpContext.getResponse();
        response.sendRedirect(request.getContextPath()
                + "/servlet/HttpExporter?filename=" + xlsfile);
    }

Guess you like

Origin blog.csdn.net/xiansibao/article/details/129320761