POI are two ways to export Excel,

Currently I only two, after other methods to master the update again.

The first: use the code to draw Excel, and then export.

Advantages: nothing advantages;

Disadvantages:

  1, a large amount of code difficult to maintain. A little one change will change a lot of code, the so-called pull situation as a whole.

  2, style is difficult to control, like where the need for bold, where the need for background color and the like, it is more difficult to describe.

Steps:

  1, POM added POI dependent, as:

    

 

   2, create a Workbook object, open up its stack memory, the code:

Workbook workbook = new XSSFWorkbook();

  3, create a form, you can create multiple forms, create a form where only the code:

Sheet sheet = workbook.createSheet();

  4, set the name to the form, the following code first parameter is the first of several forms of meaning, if you create two forms, is now set to give the name of the second form, the first argument should be set to 1 because the index is calculated starting from 0. We are now set to form a first name, Code:

workbook.setSheetName (0, "Give me a name it"); // form name

  5, create a row, we are here to header example, because the method of creating all the rows are the same. Parameter represents the creation of the first few lines, we are here to create the first line, the parameter to 0, the same is because the index is counted from zero. Code:

Headrow sheet.createRow = Row (0); // Create a first row

  6, create a cell to create a cell here only because the method of creating all the cells are the same, only different parameters to it. Code:

HeadCell = headRow.createCell the Cell (0); // create the first cell of the first row

  7, to set the cell style code:

HeadCellStyle = workbook.createCellStyle the CellStyle (); // Create cell style objects 
the Font font = workbook.createFont ();
font.setBold ( to true ); // whether bold 
font.setFontHeightInPoints (( Short ) 14); // font size 
cellStyle.setFont (font);
cellStyle.setAlignment (HorizontalAlignment.CENTER); // horizontally centered     
cellStyle.setVerticalAlignment (VerticalAlignment.CENTER); // vertically centered 
headCell.setCellStyle (headCellStyle); // to set a good object to a pattern of cells

  8, to insert a cell value, the code:

headCell.setCellValue ( "content of the cells");

  9, set the column width, the code:

sheet.setColumnWidth(1, headCell.getStringCellValue().getBytes().length * 256);// 设置列宽

  10, merge cells, I have to merge nine cells in the first row, the code is as follows:

sheet.addMergedRegion ( new new CellRangeAddress (0, 0, 0,. 8)); // merge four parameters as header interpretation order are: start to merge row index, the row index ended, the starting column index, ending column index

  11, a line-height, the code:

headRow.setHeightInPoints ((2 * sheet.getDefaultRowHeightInPoints ())); // set the row height

  12, the above 11 steps are the most basic form creation excel, creating row, cell created, merged, or the like intervening value, particularly to look like painted using the above method can be free to play. Then write about how the stream and prompted to download the browser:

try {
     Workbook Workbook = new new XSSFWorkbook ();
     // draw step of these steps is omitted Excel more than 11 steps 
    File File = new new File (System.getProperty ( "java.io.tmpdir") + File.separator + "filename" + ".xlsx"); // prompted to download the file 
    OutputStream OUT = new new FileOutputStream (file);
    workbook.write(out);
    out.flush();
    out.close();
    return fileEntity(file, file.getName());
} catch (FileNotFoundException e) {
    e.printStackTrace ();
} catch (IOException e) {
    e.printStackTrace ();
}                

  13, the browser displays the following information (here with the Google browser exported):

   14. The method so far completed a share.

The second: use a template exporting Excel.

advantage:

  1, the form template styles are provided, does not need to adjust themselves. Even if you want to modify the style, use Microsoft tools and then replace or modify the template to WPS project, no code control.

  2, hands animation table or the like, streamlined code, more energy can be used to fill the data.

Disadvantages: currently not found.

Steps:

  1, POM added POI dependent, as:

    

   2, the need to export the Excel file template under the web project, as:

   3, get a template file, and rename the file name based on business requirements, code:

1 String path = ActionUtil.getRequest () getServletContext () getRealPath ( "/");.. // get the template file 
2 String filePath = path + "excelTemplate \\ template filename .xlsx" ;
 3 FileInputStream tps = new new FileInputStream ( new new file (filePath));
 4 file file = new new file (System.getProperty ( "java.io.tmpdir") + File.separator + "to reset the file name" + ".xlsx"); // prompted to download file 
. 5 the OutputStream OUT = new new a FileOutputStream (file);

  4, see the code:

Workbook = XSSFWorkbook new new XSSFWorkbook (); // create a new workspace Excel 
Workbook = new new XSSFWorkbook (tps); // copy the template to the new Excel

  5, data to form-filling, here we fill data to the first form, the code:

. 1 Sheet Sheet workbook.getSheetAt = (0); // get the first sheet 
2 Row row_01 sheet.getRow = (. 1); // get the first row 
. 3 row_01.getCell (. 1) .setCellValue ( "I the content of cell "); // to the first line of the second cell to insert the value

  6, filling method as in Step 5, to different data rows and cells can be inserted as needed. Then start output and generate Excel files, code:

1 workbook.write(out);
2 out.flush();
3 out.close();
4 return fileEntity(file, file.getName());

  7, step 6 of the code needed to handle exceptions, this we should all know how to deal with it! I do not know look at a method step 12 words. The method of downloading tips and a browser is the same as step 13 shown in FIG. Method two share so far been completed.

how about it? The overall look is not down Method Two more simple? Anyway, I personally biased in favor of the second method! About API URLs related to the POI me share to you here, after all, just a blog post is not possible to cover all of the API methods.

For more exploration POI, see the official website of the API: http://poi.apache.org/components/spreadsheet/quick-guide.html

 

Guess you like

Origin www.cnblogs.com/ywy8/p/11718467.html