EasyExcel導出創建Excel下拉框


話不多說,上才藝。

下面代碼粘貼即用

 /**
     *
     * 導出表格帶下拉框
     */
    @GetMapping("exportBox")
    public void export(HttpServletResponse response)
        throws IOException
    {
        
        String fileName = "模板.xls";
        
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 設置背景顏色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        // 設置頭字體
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short)14);
        // 字體加粗
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        // 設置頭居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        
        // 內容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 設置內容字體
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontHeightInPoints((short)12);
        contentWriteFont.setFontName("宋體");
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        // 設置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 設置 垂直居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 設置單元格格式為 文本
        contentWriteCellStyle.setDataFormat((short)49);
        
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
            new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        
        // 假數據 實際開發中一般是從數據庫中查詢
        List<Employee> objects = new ArrayList<>();
        for (int i = 0; i < 3; i++)
        {
            Employee employee = new Employee();
            employee.setSchool(i + "大學");
            employee.setName(i + "RR");
            objects.add(employee);
        }
        
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        // 設置表名,引腳名,文件格式,list數據
        EasyExcel.write(response.getOutputStream(), Employee.class)
            .registerWriteHandler(horizontalCellStyleStrategy)
            .registerWriteHandler(new SpinnerWriteHandler())
            .sheet("模板")
            .doWrite(objects);
        
    }
controller
package com.temporary.handle;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Han
 * @Description
 * @date 2022/3/21
 */
public class SpinnerWriteHandler implements SheetWriteHandler
{
    
    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder)
    {
        
    }
    
    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder)
    {
        String[] ageTypes = new String[] {"0 - 14", "15 - 25", "26 - 50", "51 - ~"};
        String[] schoolTypes = new String[] {"清華大學", "北京大學", "鄭州大學", "南京大學"};
        Map<Integer, String[]> mapDropDown = new HashMap<>();
        // 這里的key值 對應導出列的順序 從0開始
        mapDropDown.put(1, ageTypes);
        mapDropDown.put(2, schoolTypes);
        Sheet sheet = writeSheetHolder.getSheet();
        /// 開始設置下拉框
        DataValidationHelper helper = sheet.getDataValidationHelper();// 設置下拉框
        for (Map.Entry<Integer, String[]> entry : mapDropDown.entrySet())
        {
            /*** 起始行、終止行、起始列、終止列 **/
            CellRangeAddressList addressList = new CellRangeAddressList(1, 1000, entry.getKey(), entry.getKey());
            /*** 設置下拉框數據 **/
            DataValidationConstraint constraint = helper.createExplicitListConstraint(entry.getValue());
            DataValidation dataValidation = helper.createValidation(constraint, addressList);
            /*** 處理Excel兼容性問題 **/
            if (dataValidation instanceof XSSFDataValidation)
            {
                dataValidation.setSuppressDropDownArrow(true);
                dataValidation.setShowErrorBox(true);
            }
            else
            {
                dataValidation.setSuppressDropDownArrow(false);
            }
            sheet.addValidationData(dataValidation);
        }
        
    }
}
Handle

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM