How to create Apache POI Excel View Configuration in Spring Boot

前端 未结 1 1042
北海茫月
北海茫月 2021-02-02 04:44

I have problem when i want to export my data to excel using spring boot web.

I am using Thymeleaf as template engine (auto configured by spring boot). But when I add Xm

相关标签:
1条回答
  • 2021-02-02 05:01

    With Spring Boot you don't need any extra configuration to generate an excel file.

    Create a controller returning a ModelAndView with an AbstractExcelView:

    @Controller
    public class MyController {
    
        @RequestMapping(value="/myexcel", method=RequestMethod.GET)
        public ModelAndView getMyData(HttpServletRequest request, HttpServletResponse response) throws SQLException{
            Map<String, Object> model = new HashMap<String, Object>();
            //Sheet Name
            model.put("sheetname", "TestSheetName");
            //Headers List
            List<String> headers = new ArrayList<String>();
            headers.add("Column1");
            headers.add("Column2");
            headers.add("Column3");
            model.put("headers", headers);
            //Results Table (List<Object[]>)
            List<List<String>> results = new ArrayList<List<String>>();
            List<String> l1 = new ArrayList<String>();
            l1.add("A1");
            l1.add("B1");
            l1.add("C1");
            results.add(l1);
            List<String> l2 = new ArrayList<String>();
            l2.add("A2");
            l2.add("B2");
            l2.add("C2");
            results.add(l2);
            model.put("results",results);
            response.setContentType( "application/ms-excel" );
            response.setHeader( "Content-disposition", "attachment; filename=myfile.xls" );         
            return new ModelAndView(new MyExcelView(), model);
        }
    

    Then build your AbstractExcelView like:

    public class MyExcelView extends AbstractExcelView
    {
        @SuppressWarnings("unchecked")
        protected void buildExcelDocument(Map<String, Object> model,
                HSSFWorkbook workbook,
                HttpServletRequest request,
                HttpServletResponse response)
        {
            //VARIABLES REQUIRED IN MODEL
            String sheetName = (String)model.get("sheetname");
            List<String> headers = (List<String>)model.get("headers");
            List<List<String>> results = (List<List<String>>)model.get("results");
            List<String> numericColumns = new ArrayList<String>();
            if (model.containsKey("numericcolumns"))
                numericColumns = (List<String>)model.get("numericcolumns");
            //BUILD DOC
            HSSFSheet sheet = workbook.createSheet(sheetName);
            sheet.setDefaultColumnWidth((short) 12);
            int currentRow = 0;
            short currentColumn = 0;
            //CREATE STYLE FOR HEADER
            HSSFCellStyle headerStyle = workbook.createCellStyle();
            HSSFFont headerFont = workbook.createFont();
            headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            headerStyle.setFont(headerFont); 
            //POPULATE HEADER COLUMNS
            HSSFRow headerRow = sheet.createRow(currentRow);
            for(String header:headers){
                HSSFRichTextString text = new HSSFRichTextString(header);
                HSSFCell cell = headerRow.createCell(currentColumn); 
                cell.setCellStyle(headerStyle);
                cell.setCellValue(text);            
                currentColumn++;
            }
            //POPULATE VALUE ROWS/COLUMNS
            currentRow++;//exclude header
            for(List<String> result: results){
                currentColumn = 0;
                HSSFRow row = sheet.createRow(currentRow);
                for(String value : result){//used to count number of columns
                    HSSFCell cell = row.createCell(currentColumn);
                    if (numericColumns.contains(headers.get(currentColumn))){
                        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                        cell.setCellValue(NumUtils.extractDoubleOrZero(value));
                    } else {
                        HSSFRichTextString text = new HSSFRichTextString(value);                
                        cell.setCellValue(text);                    
                    }
                    currentColumn++;
                }
                currentRow++;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题