How to export complex multi-header and multi-sheet page excel template? Here comes the Java EasyPoi instance

One: Introduction

In the recent development, I encountered the problem of complex multi-header, multi-sheet, multi-picture excel export.

The framework used in this article to operate Excel is EasyPoi

The function of EasyPoi is like the name easy, the main function is easy, so that a person who has never been in contact with poi can easily write Excel export, Excel template export, Excel import, Word template export, through simple annotations and template language (familiar with expression grammar), complete the previous complex writing

Official homepage:  www.wupaas.com/easypoi.htm…

API documentation:  doc.wupaas.com/docs/easypo…

Two: Introduction to EasyPoi template commands

Templates are a simple way to deal with complex Excel. Complex Excel styles can be edited directly in Excel, which perfectly avoids the minefield of coding styles. At the same time, the support of commands also improves the effectiveness of templates.

The commands and functions supported by EasyPoi are listed below,  the most important thing is the usage of various fe

  • space split
  • Trinocular operation { {test ? obj:obj2}}
  • n: Indicates that this cell is a numeric type { {n:}}
  • le: stands for length { {le:()}} use { {le:() > 8 ? obj1 : obj2}} in if/else
  • fd: format time { {fd:(obj;yyyy-MM-dd)}}
  • fn: format number { {fn:(obj;###.00)}}
  • fe: traverse data, create row
  • !fe: traverse data without creating row
  • $fe: Move down and insert, move the current line and the following lines down. size() line, and then insert
  • #fe: Horizontal traversal
  • v_fe: horizontal traversal value
  • !if: delete the current column { {!if:(test)}}
  • Single quotes represent constant values'' such as '1', then the output is 1
  • &NULL& space
  • &INDEX& indicates the sequence number in the loop, automatically added
  • ]] newline character multi-line traversal export
  • sum: statistics
  • cal: base +-X% calculation
  • dict: dictionary
  • i18n: Internationalization

Three: design ideas

1. According to the instructions of the easypoi template, design and export the data structure, and fill it into the Excel template.

2. Query data, and preprocess the sheet pages and cells designed into the list that needs to be traversed:

Move down the corresponding cells in the template table and merge the cells that need to be merged

//move cell down
sheet.shiftRows(startRow, endRow, n);
copy code
//Merge Cells
sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol));
copy code

3. Create template export parameters

//Create template export parameters
TemplateExportParams params = new TemplateExportParams(dealTemplatePath, true, sheetNames);
copy code

4. Generate and export workbook

//Generate and export workbook
Workbook workbook = templateExcelExportHelper.createExcelByTemplate(getMinioInputStream(BUCKET_NAME, dealTemplatePath), params, sheetMap);
copy code

5. Process the workbook generated by easypoi

/**
 * Set the outer border
 * @param workbook worksheet
 * @param i sheet subscript
 */
public static void setSheetBorder(Workbook workbook, int i) {
    CellRangeAddress cellRangeAddress = new CellRangeAddress(0, workbook.getSheetAt(i).getLastRowNum(),0,
            workbook.getSheetAt(i).getRow(0).getPhysicalNumberOfCells() -1);
    RegionUtil.setBorderBottom(BorderStyle.MEDIUM,cellRangeAddress, workbook.getSheetAt(i));
    RegionUtil.setBorderLeft(BorderStyle.MEDIUM,cellRangeAddress, workbook.getSheetAt(i));
    RegionUtil.setBorderRight(BorderStyle.MEDIUM,cellRangeAddress, workbook.getSheetAt(i));
    RegionUtil.setBorderTop(BorderStyle.MEDIUM,cellRangeAddress, workbook.getSheetAt(i));
}
copy code

6. Output the file and delete the template file generated by preprocessing

//Output file stream to Servlet
        setExportExcelFormat(response, workbook, exportExcelName);

        //Delete the generated preprocessing template
        deleteTemplate(dealTemplatePath);
copy code
/**
 * Output file stream
 *
 * @param response
 * @param workbook
 * @param fileName
 * @throws Exception
 */
public void setExportExcelFormat(HttpServletResponse response, Workbook workbook, String fileName) throws Exception {
    ServletOutputStream outStream = null;
    String fileNameURL = URLEncoder.encode(fileName, "UTF-8");
    response.setContentType("octets/stream");
    response.setHeader("Content-disposition", "attachment;filename=" + fileNameURL + ";"
            + "filename*=utf-8''" + fileNameURL);
    response.addHeader("Pargam", "no-cache");
    response.addHeader("Cache-Control", "no-cache");
    try {
        outStream = response.getOutputStream();
        workbook.write(outStream);
    } finally {
        outStream.close();
    }
}
copy code

Four: template example

Five: Screenshot of the preprocessing template

Five: Export screenshots

Guess you like

Origin blog.csdn.net/m0_48922996/article/details/125817696