机动部队 發表於 2025-5-16 15:40:00

记录一个利用alibaba的easyexcel实现的简易excel导出工具(完整版)

<p>功能说明:</p>
<p>1、自定义表头、多级表头的表格导出</p>
<p>效果展示:</p>
<p><img src="https://img2024.cnblogs.com/blog/1269835/202505/1269835-20250516114149599-121314338.png" alt="" loading="lazy"></p>
<p>maven引入依赖:</p>
<div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>com.alibaba<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>easyexcel<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>2.2.6<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>工具类:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">com.alibaba.excel.EasyExcel;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.enums.CellDataTypeEnum;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.metadata.CellData;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.metadata.Head;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.write.metadata.style.WriteCellStyle;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.write.metadata.style.WriteFont;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.google.common.collect.Lists;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.heit.common.core.exception.ZtCommonException;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> lombok.SneakyThrows;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.collections4.CollectionUtils;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.lang3.ObjectUtils;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.poi.ss.usermodel.BorderStyle;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.poi.ss.usermodel.Cell;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.poi.ss.usermodel.HorizontalAlignment;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.poi.ss.usermodel.VerticalAlignment;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.servlet.http.HttpServletResponse;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.validation.constraints.NotNull;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.net.URLEncoder;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.ArrayList;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.HashMap;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.List;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.Map;

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* 导出数据
* author: 唐泽齐
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ExclUtil {

    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 导出excel
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> response
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> outFileName 导出文件名
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> headList    表头列表   分页名-&gt;合并列名-&gt;...-&gt;...-&gt;列名
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> dataList    数据列表
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
    @SneakyThrows
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> write(@NotNull HttpServletResponse response, String outFileName, List&lt;List&lt;String&gt;&gt; headList, List&lt;List&lt;Object&gt;&gt;<span style="color: rgba(0, 0, 0, 1)"> dataList) {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (ObjectUtils.isEmpty(response)) <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ZtCommonException("导出环境异常!"<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (ObjectUtils.isEmpty(outFileName)) <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ZtCommonException("未设置导出文件名!"<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (ObjectUtils.isEmpty(headList)) <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ZtCommonException("未设置导出表格式!"<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (ObjectUtils.isEmpty(dataList)) <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ZtCommonException("暂无可导出的数据!"<span style="color: rgba(0, 0, 0, 1)">);

      response.setContentType(</span>"application/vnd.ms-excel"<span style="color: rgba(0, 0, 0, 1)">);
      response.setCharacterEncoding(</span>"utf-8"<span style="color: rgba(0, 0, 0, 1)">);
      response.setHeader(</span>"Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(outFileName, "UTF-8").replaceAll("\\+", "%20") + ".xls"<span style="color: rgba(0, 0, 0, 1)">);
      response.setHeader(</span>"Access-Control-Expose-Headers", "Content-disposition"<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置单元格样式</span>
      HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> HorizontalCellStyleStrategy(getHeadStyle(), getContentStyle());
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 列宽策略设置</span>
      ExcelCellWidthStyleStrategy widthStyleStrategy = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ExcelCellWidthStyleStrategy();
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">导出数据</span>
<span style="color: rgba(0, 0, 0, 1)">      EasyExcel.write(response.getOutputStream()).
                registerWriteHandler(horizontalCellStyleStrategy)
                .registerWriteHandler(widthStyleStrategy)
                .head(headList)
                .sheet().doWrite(dataList);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String[] _formats = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> String[]{
            </span>"General"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"0"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"0.00"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"#,##0"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"#,##0.00"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"\"$\"#,##0_);(\"$\"#,##0)"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"\"$\"#,##0_);(\"$\"#,##0)"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"\"$\"#,##0.00_);(\"$\"#,##0.00)"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"\"$\"#,##0.00_);(\"$\"#,##0.00)"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"0%", "0.00%", "0.00E+00"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"# ?/?", "# ??/??"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"m/d/yy", "d-mmm-yy"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"d-mmm", "mmm-yy"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"h:mm AM/PM"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"h:mm:ss AM/PM"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"h:mm", "h:mm:ss"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"m/d/yy h:mm"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"reserved-0x17"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"reserved-0x18"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"reserved-0x19"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"reserved-0x1A"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"reserved-0x1B"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"reserved-0x1C"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"reserved-0x1D"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"reserved-0x1E"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"reserved-0x1F"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"reserved-0x20"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"reserved-0x21"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"reserved-0x22"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"reserved-0x23"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"reserved-0x24"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"#,##0_);(#,##0)"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"#,##0_);(#,##0)"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"#,##0.00_);(#,##0.00)"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"#,##0.00_);(#,##0.00)"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"_(* #,##0_);_(* (#,##0);_(* \"-\"_);_(@_)"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"_(\"$\"* #,##0_);_(\"$\"* (#,##0);_(\"$\"* \"-\"_);_(@_)"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"_(* #,##0.00_);_(* (#,##0.00);_(* \"-\"??_);_(@_)"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"_(\"$\"* #,##0.00_);_(\"$\"* (#,##0.00);_(\"$\"* \"-\"??_);_(@_)"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"mm:ss"<span style="color: rgba(0, 0, 0, 1)">,
            </span>":mm:ss"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"mm:ss.0"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"##0.0E+0"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"@" <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 文本格式</span>
<span style="color: rgba(0, 0, 0, 1)">    };


    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 标题样式
   *
   * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
   <span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> WriteCellStyle getHeadStyle() {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 头的策略</span>
      WriteCellStyle headWriteCellStyle = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> WriteCellStyle();
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 背景颜色
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">      headWriteCellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE1.getIndex());
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">      headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 字体</span>
      WriteFont headWriteFont = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> WriteFont();
      headWriteFont.setFontName(</span>"宋体");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置字体名字</span>
      headWriteFont.setFontHeightInPoints((<span style="color: rgba(0, 0, 255, 1)">short</span>) 10);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置字体大小</span>
      headWriteFont.setBold(<span style="color: rgba(0, 0, 255, 1)">true</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">字体加粗</span>
      headWriteCellStyle.setWriteFont(headWriteFont); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在样式用应用设置的字体;

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 样式</span>
      headWriteCellStyle.setBorderBottom(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置底边框;</span>
      headWriteCellStyle.setBottomBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置底边框颜色;</span>
      headWriteCellStyle.setBorderLeft(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置左边框;</span>
      headWriteCellStyle.setLeftBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置左边框颜色;</span>
      headWriteCellStyle.setBorderRight(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置右边框;</span>
      headWriteCellStyle.setRightBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置右边框颜色;</span>
      headWriteCellStyle.setBorderTop(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置顶边框;</span>
      headWriteCellStyle.setTopBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置顶边框颜色;</span>
<span style="color: rgba(0, 0, 0, 1)">
      headWriteCellStyle.setWrapped(</span><span style="color: rgba(0, 0, 255, 1)">true</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置自动换行;</span>
<span style="color: rgba(0, 0, 0, 1)">
      headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置水平对齐的样式为居中对齐;</span>
      headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置垂直对齐的样式为居中对齐;</span>
      headWriteCellStyle.setShrinkToFit(<span style="color: rgba(0, 0, 255, 1)">true</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置文本收缩至合适</span>

      <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> headWriteCellStyle;
    }


    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 内容样式
   *
   * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
   <span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> WriteCellStyle getContentStyle() {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 内容的策略</span>
      WriteCellStyle contentWriteCellStyle = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> WriteCellStyle();

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 背景绿色
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">      contentWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">      contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置字体</span>
      WriteFont contentWriteFont = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> WriteFont();
      contentWriteFont.setFontHeightInPoints((</span><span style="color: rgba(0, 0, 255, 1)">short</span>) 9);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置字体大小</span>
      contentWriteFont.setFontName("宋体"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置字体名字</span>
      contentWriteCellStyle.setWriteFont(contentWriteFont);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在样式用应用设置的字体;

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置样式;</span>
      contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置底边框;</span>
      contentWriteCellStyle.setBottomBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置底边框颜色;</span>
      contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置左边框;</span>
      contentWriteCellStyle.setLeftBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置左边框颜色;</span>
      contentWriteCellStyle.setBorderRight(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置右边框;</span>
      contentWriteCellStyle.setRightBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置右边框颜色;</span>
      contentWriteCellStyle.setBorderTop(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置顶边框;</span>
      contentWriteCellStyle.setTopBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">/设置顶边框颜色;</span>
<span style="color: rgba(0, 0, 0, 1)">
      contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 水平居中</span>
      contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 垂直居中</span>
      contentWriteCellStyle.setWrapped(<span style="color: rgba(0, 0, 255, 1)">true</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置自动换行;</span>
<span style="color: rgba(0, 0, 0, 1)">
      contentWriteCellStyle.setDataFormat((</span><span style="color: rgba(0, 0, 255, 1)">short</span>) 49);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置单元格格式是:文本格式,方式长数字文本科学计数法</span>
<span style="color: rgba(0, 0, 0, 1)">
      contentWriteCellStyle.setShrinkToFit(</span><span style="color: rgba(0, 0, 255, 1)">true</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置文本收缩至合适</span>

      <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> contentWriteCellStyle;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">class</span> ExcelCellWidthStyleStrategy <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> AbstractColumnWidthStyleStrategy {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 可以根据这里的最大宽度,按自己需要进行调整,搭配单元格样式实现类中的,自动换行,效果更好</span>
      <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">int</span> MAX_COLUMN_WIDTH = 20<span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 0, 255, 1)">private</span> Map&lt;Integer, Map&lt;Integer, Integer&gt;&gt; CACHE = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap(8<span style="color: rgba(0, 0, 0, 1)">);

      @Override
      </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setColumnWidth(WriteSheetHolder writeSheetHolder, List&lt;CellData&gt;<span style="color: rgba(0, 0, 0, 1)"> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
            </span><span style="color: rgba(0, 0, 255, 1)">boolean</span> needSetWidth = isHead || !<span style="color: rgba(0, 0, 0, 1)">CollectionUtils.isEmpty(cellDataList);
            </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (needSetWidth) {
                Map</span>&lt;Integer, Integer&gt; maxColumnWidthMap =<span style="color: rgba(0, 0, 0, 1)"> (Map) CACHE.get(writeSheetHolder.getSheetNo());
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (maxColumnWidthMap == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
                  maxColumnWidthMap </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap(16<span style="color: rgba(0, 0, 0, 1)">);
                  CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap);
                }

                Integer columnWidth </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.dataLength(cellDataList, cell, isHead);
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (columnWidth &gt;= 0<span style="color: rgba(0, 0, 0, 1)">) {
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span> (columnWidth &gt;<span style="color: rgba(0, 0, 0, 1)"> MAX_COLUMN_WIDTH) {
                        columnWidth </span>=<span style="color: rgba(0, 0, 0, 1)"> MAX_COLUMN_WIDTH;
                  }

                  Integer maxColumnWidth </span>=<span style="color: rgba(0, 0, 0, 1)"> (Integer) ((Map) maxColumnWidthMap).get(cell.getColumnIndex());
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span> (maxColumnWidth == <span style="color: rgba(0, 0, 255, 1)">null</span> || columnWidth &gt;<span style="color: rgba(0, 0, 0, 1)"> maxColumnWidth) {
                        ((Map) maxColumnWidthMap).put(cell.getColumnIndex(), columnWidth);
                        writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth </span>* 256<span style="color: rgba(0, 0, 0, 1)">);
                  }

                }
            }
      }

      </span><span style="color: rgba(0, 0, 255, 1)">private</span> Integer dataLength(List&lt;CellData&gt;<span style="color: rgba(0, 0, 0, 1)"> cellDataList, Cell cell, Boolean isHead) {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (isHead) {
                </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> cell.getStringCellValue().getBytes().length;
            } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                CellData cellData </span>= (CellData) cellDataList.get(0<span style="color: rgba(0, 0, 0, 1)">);
                CellDataTypeEnum type </span>=<span style="color: rgba(0, 0, 0, 1)"> cellData.getType();
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (type == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
                  </span><span style="color: rgba(0, 0, 255, 1)">return</span> -1<span style="color: rgba(0, 0, 0, 1)">;
                } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                  </span><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (type) {
                        </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> STRING:
                            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> cellData.getStringValue().getBytes().length;
                        </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> BOOLEAN:
                            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> cellData.getBooleanValue().toString().getBytes().length;
                        </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> NUMBER:
                            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> cellData.getNumberValue().toString().getBytes().length;
                        </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">:
                            </span><span style="color: rgba(0, 0, 255, 1)">return</span> -1<span style="color: rgba(0, 0, 0, 1)">;
                  }
                }
            }
      }


    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">导出环境</span>
      HttpServletResponse response = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">文件名、分页名</span>
      String outFileName = "测试文件"<span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">表头格式</span>
      List&lt;List&lt;String&gt;&gt; headList = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;List&lt;String&gt;&gt;<span style="color: rgba(0, 0, 0, 1)">();
      headList.add(Lists.newArrayList(outFileName, </span>"合并列1", "列1"<span style="color: rgba(0, 0, 0, 1)">));
      headList.add(Lists.newArrayList(outFileName, </span>"合并列1", "列2"<span style="color: rgba(0, 0, 0, 1)">));
      headList.add(Lists.newArrayList(outFileName, </span>"合并列2", "列3"<span style="color: rgba(0, 0, 0, 1)">));
      headList.add(Lists.newArrayList(outFileName, </span>"合并列2", "列4"<span style="color: rgba(0, 0, 0, 1)">));
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">数据</span>
      List&lt;List&lt;Object&gt;&gt; dataList = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">();
      </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i= 0;i&lt;100;i++<span style="color: rgba(0, 0, 0, 1)">) {
            List</span>&lt;Object&gt; list = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">();
            list.add(</span>"1"+<span style="color: rgba(0, 0, 0, 1)">i);
            list.add(</span>"2"+<span style="color: rgba(0, 0, 0, 1)">i);
            list.add(</span>"3"+<span style="color: rgba(0, 0, 0, 1)">i);
            list.add(</span>"4"+<span style="color: rgba(0, 0, 0, 1)">i);
            dataList.add(list);
      }
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">导出表格</span>
<span style="color: rgba(0, 0, 0, 1)">      write(response,outFileName,headList,dataList);
    }
}</span></pre>
</div>
<p>&nbsp;<span style="color: rgba(255, 0, 0, 1)">由于上线linux系统时,大多数是使用openjdk,所以easyExcel用不了,于是用Apache POI写了一份同款导出,以下是完整代码:</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">package</span><span style="color: rgba(0, 0, 0, 1)"> com.heit.road.service.util;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.EasyExcel;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.enums.CellDataTypeEnum;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.metadata.CellData;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.metadata.Head;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.write.metadata.style.WriteCellStyle;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.write.metadata.style.WriteFont;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> com.heit.common.core.exception.ZtCommonException;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> io.swagger.models.auth.In;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> lombok.SneakyThrows;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> lombok.extern.slf4j.Slf4j;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.collections4.CollectionUtils;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.commons.lang3.ObjectUtils;
</span><span style="color: rgba(0, 0, 255, 1)">import</span> org.apache.poi.ss.usermodel.*<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.poi.ss.util.CellRangeAddress;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.apache.poi.xssf.usermodel.XSSFWorkbook;

</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.servlet.ServletOutputStream;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.servlet.http.HttpServletResponse;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.validation.constraints.NotNull;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.net.URLEncoder;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.Arrays;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.HashMap;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.List;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.util.Map;

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* 导出数据
* author: 唐泽齐
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Slf4j
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ExclUtil {

    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 导出excel
   *
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> response
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> outFileName 导出文件名
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> headList    表头列表   分页名-&gt;合并列名-&gt;...-&gt;...-&gt;列名
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> dataList    数据列表
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
    @SneakyThrows
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> write(@NotNull HttpServletResponse response, String outFileName, List&lt;List&lt;String&gt;&gt; headList, List&lt;List&lt;Object&gt;&gt;<span style="color: rgba(0, 0, 0, 1)"> dataList) {
      check(response, outFileName, headList, dataList);
      response.setContentType(</span>"application/vnd.ms-excel"<span style="color: rgba(0, 0, 0, 1)">);
      response.setCharacterEncoding(</span>"utf-8"<span style="color: rgba(0, 0, 0, 1)">);
      response.setHeader(</span>"Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(outFileName, "UTF-8").replaceAll("\\+", "%20") + ".xls"<span style="color: rgba(0, 0, 0, 1)">);
      response.setHeader(</span>"Access-Control-Expose-Headers", "Content-disposition"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">      Easy.write(response, outFileName, headList, dataList);</span>
<span style="color: rgba(0, 0, 0, 1)">      Poi.write(response, outFileName, headList, dataList);
    }
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> check(@NotNull HttpServletResponse response, String outFileName, List&lt;List&lt;String&gt;&gt; headList, List&lt;List&lt;Object&gt;&gt;<span style="color: rgba(0, 0, 0, 1)"> dataList) {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (ObjectUtils.isEmpty(response)) <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ZtCommonException("导出环境异常!"<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (ObjectUtils.isEmpty(outFileName)) <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ZtCommonException("未设置导出文件名!"<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (ObjectUtils.isEmpty(headList)) <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ZtCommonException("未设置导出表格式!"<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (ObjectUtils.isEmpty(dataList)) <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ZtCommonException("暂无可导出的数据!"<span style="color: rgba(0, 0, 0, 1)">);
    }
    </span><span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Easy {

      @SneakyThrows
      </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> write(@NotNull HttpServletResponse response, String outFileName, List&lt;List&lt;String&gt;&gt; headList, List&lt;List&lt;Object&gt;&gt;<span style="color: rgba(0, 0, 0, 1)"> dataList) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置单元格样式</span>
            HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                  <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> HorizontalCellStyleStrategy(getHeadStyle(), getContentStyle());
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 列宽策略设置</span>
            ExcelCellWidthStyleStrategy widthStyleStrategy = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ExcelCellWidthStyleStrategy();
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">导出数据</span>
<span style="color: rgba(0, 0, 0, 1)">            EasyExcel.write(response.getOutputStream()).
                  registerWriteHandler(horizontalCellStyleStrategy)
                  .registerWriteHandler(widthStyleStrategy)
                  .head(headList)
                  .sheet(outFileName)
                  .doWrite(dataList);
      }

      </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
         * 标题样式
         *
         * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
         <span style="color: rgba(0, 128, 0, 1)">*/</span>
      <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> WriteCellStyle getHeadStyle() {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 头的策略</span>
            WriteCellStyle headWriteCellStyle = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> WriteCellStyle();
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 背景颜色
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">      headWriteCellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE1.getIndex());
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">      headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 字体</span>
            WriteFont headWriteFont = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> WriteFont();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">      headWriteFont.setFontName("宋体");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置字体名字</span>
            headWriteFont.setFontHeightInPoints((<span style="color: rgba(0, 0, 255, 1)">short</span>) 10);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置字体大小</span>
            headWriteFont.setBold(<span style="color: rgba(0, 0, 255, 1)">true</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">字体加粗</span>
            headWriteCellStyle.setWriteFont(headWriteFont); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在样式用应用设置的字体;

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 样式</span>
            headWriteCellStyle.setBorderBottom(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置底边框;</span>
            headWriteCellStyle.setBottomBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置底边框颜色;</span>
            headWriteCellStyle.setBorderLeft(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置左边框;</span>
            headWriteCellStyle.setLeftBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置左边框颜色;</span>
            headWriteCellStyle.setBorderRight(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置右边框;</span>
            headWriteCellStyle.setRightBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置右边框颜色;</span>
            headWriteCellStyle.setBorderTop(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置顶边框;</span>
            headWriteCellStyle.setTopBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置顶边框颜色;</span>
<span style="color: rgba(0, 0, 0, 1)">
            headWriteCellStyle.setWrapped(</span><span style="color: rgba(0, 0, 255, 1)">true</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置自动换行;</span>
<span style="color: rgba(0, 0, 0, 1)">
            headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置水平对齐的样式为居中对齐;</span>
            headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置垂直对齐的样式为居中对齐;</span>
            headWriteCellStyle.setShrinkToFit(<span style="color: rgba(0, 0, 255, 1)">true</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置文本收缩至合适</span>

            <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> headWriteCellStyle;
      }
      </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
         * 内容样式
         *
         * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
         <span style="color: rgba(0, 128, 0, 1)">*/</span>
      <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> WriteCellStyle getContentStyle() {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 内容的策略</span>
            WriteCellStyle contentWriteCellStyle = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> WriteCellStyle();

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 背景绿色
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">      contentWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">      contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置字体</span>
            WriteFont contentWriteFont = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> WriteFont();
            contentWriteFont.setFontHeightInPoints((</span><span style="color: rgba(0, 0, 255, 1)">short</span>) 9);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置字体大小
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">      contentWriteFont.setFontName("宋体"); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置字体名字</span>
            contentWriteCellStyle.setWriteFont(contentWriteFont);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在样式用应用设置的字体;

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置样式;</span>
            contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置底边框;</span>
            contentWriteCellStyle.setBottomBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置底边框颜色;</span>
            contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置左边框;</span>
            contentWriteCellStyle.setLeftBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置左边框颜色;</span>
            contentWriteCellStyle.setBorderRight(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置右边框;</span>
            contentWriteCellStyle.setRightBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置右边框颜色;</span>
            contentWriteCellStyle.setBorderTop(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置顶边框;</span>
            contentWriteCellStyle.setTopBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">/设置顶边框颜色;</span>
<span style="color: rgba(0, 0, 0, 1)">
            contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 水平居中</span>
            contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 垂直居中</span>
            contentWriteCellStyle.setWrapped(<span style="color: rgba(0, 0, 255, 1)">true</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置自动换行;</span>
<span style="color: rgba(0, 0, 0, 1)">
            contentWriteCellStyle.setDataFormat((</span><span style="color: rgba(0, 0, 255, 1)">short</span>) 49);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置单元格格式是:文本格式,方式长数字文本科学计数法</span>
<span style="color: rgba(0, 0, 0, 1)">
            contentWriteCellStyle.setShrinkToFit(</span><span style="color: rgba(0, 0, 255, 1)">true</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置文本收缩至合适</span>

            <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> contentWriteCellStyle;
      }

      </span><span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">class</span> ExcelCellWidthStyleStrategy <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> AbstractColumnWidthStyleStrategy {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 可以根据这里的最大宽度,按自己需要进行调整,搭配单元格样式实现类中的,自动换行,效果更好</span>
            <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">int</span> MAX_COLUMN_WIDTH = 20<span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">private</span> Map&lt;Integer, Map&lt;Integer, Integer&gt;&gt; CACHE = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap(8<span style="color: rgba(0, 0, 0, 1)">);

            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setColumnWidth(WriteSheetHolder writeSheetHolder, List&lt;CellData&gt;<span style="color: rgba(0, 0, 0, 1)"> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
                </span><span style="color: rgba(0, 0, 255, 1)">boolean</span> needSetWidth = isHead || !<span style="color: rgba(0, 0, 0, 1)">CollectionUtils.isEmpty(cellDataList);
                </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (needSetWidth) {
                  Map</span>&lt;Integer, Integer&gt; maxColumnWidthMap =<span style="color: rgba(0, 0, 0, 1)"> (Map) CACHE.get(writeSheetHolder.getSheetNo());
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span> (maxColumnWidthMap == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
                        maxColumnWidthMap </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap(16<span style="color: rgba(0, 0, 0, 1)">);
                        CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap);
                  }

                  Integer columnWidth </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.dataLength(cellDataList, cell, isHead);
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span> (columnWidth &gt;= 0<span style="color: rgba(0, 0, 0, 1)">) {
                        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (columnWidth &gt;<span style="color: rgba(0, 0, 0, 1)"> MAX_COLUMN_WIDTH) {
                            columnWidth </span>=<span style="color: rgba(0, 0, 0, 1)"> MAX_COLUMN_WIDTH;
                        }

                        Integer maxColumnWidth </span>=<span style="color: rgba(0, 0, 0, 1)"> (Integer) ((Map) maxColumnWidthMap).get(cell.getColumnIndex());
                        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (maxColumnWidth == <span style="color: rgba(0, 0, 255, 1)">null</span> || columnWidth &gt;<span style="color: rgba(0, 0, 0, 1)"> maxColumnWidth) {
                            ((Map) maxColumnWidthMap).put(cell.getColumnIndex(), columnWidth);
                            writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth </span>* 256<span style="color: rgba(0, 0, 0, 1)">);
                        }

                  }
                }
            }

            </span><span style="color: rgba(0, 0, 255, 1)">private</span> Integer dataLength(List&lt;CellData&gt;<span style="color: rgba(0, 0, 0, 1)"> cellDataList, Cell cell, Boolean isHead) {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (isHead) {
                  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> cell.getStringCellValue().getBytes().length;
                } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                  CellData cellData </span>= (CellData) cellDataList.get(0<span style="color: rgba(0, 0, 0, 1)">);
                  CellDataTypeEnum type </span>=<span style="color: rgba(0, 0, 0, 1)"> cellData.getType();
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span> (type == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
                        </span><span style="color: rgba(0, 0, 255, 1)">return</span> -1<span style="color: rgba(0, 0, 0, 1)">;
                  } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                        </span><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (type) {
                            </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> STRING:
                              </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> cellData.getStringValue().getBytes().length;
                            </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> BOOLEAN:
                              </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> cellData.getBooleanValue().toString().getBytes().length;
                            </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> NUMBER:
                              </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> cellData.getNumberValue().toString().getBytes().length;
                            </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">:
                              </span><span style="color: rgba(0, 0, 255, 1)">return</span> -1<span style="color: rgba(0, 0, 0, 1)">;
                        }
                  }
                }
            }


      }
    }
    </span><span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Poi {
      @SneakyThrows
      </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> write(@NotNull HttpServletResponse response, String outFileName, List&lt;List&lt;String&gt;&gt; headList, List&lt;List&lt;Object&gt;&gt;<span style="color: rgba(0, 0, 0, 1)"> dataList) {
            Workbook workbook </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> XSSFWorkbook();
            Sheet sheet </span>=<span style="color: rgba(0, 0, 0, 1)"> workbook.createSheet(outFileName);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建单元格样式</span>
            CellStyle headerStyle =<span style="color: rgba(0, 0, 0, 1)"> getHeadCellStyle(workbook);
            CellStyle contentCellStyle </span>=<span style="color: rgba(0, 0, 0, 1)"> getContentCellStyle(workbook);
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建表头</span>
            Map&lt;String, List&lt;Integer&gt;&gt; regions = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">();
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> courrentRow = 0<span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0, j = 0; i &lt; headList.size(); i++ , j = 0<span style="color: rgba(0, 0, 0, 1)">) {
                List</span>&lt;String&gt; list =<span style="color: rgba(0, 0, 0, 1)"> headList.get(i);
                </span><span style="color: rgba(0, 0, 255, 1)">for</span> (; j &lt; list.size(); j++<span style="color: rgba(0, 0, 0, 1)">) {
                  courrentRow </span>=<span style="color: rgba(0, 0, 0, 1)"> j;
                  String str </span>=<span style="color: rgba(0, 0, 0, 1)"> headList.get(i).get(j);
                  Row headerRow </span>=<span style="color: rgba(0, 0, 0, 1)"> sheet.getRow(j);
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span>(headerRow == <span style="color: rgba(0, 0, 255, 1)">null</span>) headerRow =<span style="color: rgba(0, 0, 0, 1)"> sheet.createRow(j);
                  Cell headerCell </span>=<span style="color: rgba(0, 0, 0, 1)"> headerRow.createCell(i);
                  headerCell.setCellValue(str);
                  headerCell.setCellStyle(headerStyle);
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (regions.containsKey(str)) {
                        </span><span style="color: rgba(0, 0, 255, 1)">if</span> ((j &gt; 0 &amp;&amp; str.equalsIgnoreCase(headList.get(i).get(j - 1))) || (i &gt; 0 &amp;&amp; str.equalsIgnoreCase(headList.get(i - 1<span style="color: rgba(0, 0, 0, 1)">).get(j)))) {
                            regions.put(str, Arrays.asList(regions.get(str).get(</span>0), j, regions.get(str).get(2<span style="color: rgba(0, 0, 0, 1)">), i));
                        } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(regions.get(str).get(0).compareTo(regions.get(str).get(1)) != 0 || regions.get(str).get(2).compareTo(regions.get(str).get(3)) != 0<span style="color: rgba(0, 0, 0, 1)">) {
                              sheet.addMergedRegion(</span><span style="color: rgba(0, 0, 255, 1)">new</span> CellRangeAddress(regions.get(str).get(0), regions.get(str).get(1), regions.get(str).get(2), regions.get(str).get(3<span style="color: rgba(0, 0, 0, 1)">)));
                            }
                            regions.put(str, Arrays.asList(j, j, i, i));
                        }
                  } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                        regions.put(str, Arrays.asList(j, j, i, i));
                  }
                }
            }
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">合并表头</span>
            <span style="color: rgba(0, 0, 255, 1)">for</span> (Map.Entry&lt;String, List&lt;Integer&gt;&gt;<span style="color: rgba(0, 0, 0, 1)"> entry : regions.entrySet()) {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span>(entry.getValue().get(0).compareTo(entry.getValue().get(1)) != 0 || entry.getValue().get(2).compareTo(entry.getValue().get(3)) != 0<span style="color: rgba(0, 0, 0, 1)">) {
                  sheet.addMergedRegion(</span><span style="color: rgba(0, 0, 255, 1)">new</span> CellRangeAddress(entry.getValue().get(0), entry.getValue().get(1), entry.getValue().get(2), entry.getValue().get(3<span style="color: rgba(0, 0, 0, 1)">)));
                }
            }
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">冻结表头</span>
            sheet.createFreezePane(0,headList.get(0<span style="color: rgba(0, 0, 0, 1)">).size());
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建数据</span>
            Map&lt;Integer,Integer&gt; widths = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">();
            </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; dataList.size(); i++<span style="color: rgba(0, 0, 0, 1)">) {
                Row dataRow </span>= sheet.createRow(i + courrentRow + 1<span style="color: rgba(0, 0, 0, 1)">);
                </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> j = 0; j &lt; dataList.get(i).size(); j++<span style="color: rgba(0, 0, 0, 1)">) {
                  Cell dataCell </span>=<span style="color: rgba(0, 0, 0, 1)"> dataRow.createCell(j);
                  dataCell.setCellStyle(contentCellStyle);
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span>(dataList.get(i).get(j) <span style="color: rgba(0, 0, 255, 1)">instanceof</span> Number &amp;&amp; (dataList.get(i).get(j) <span style="color: rgba(0, 0, 255, 1)">instanceof</span> Integer || dataList.get(i).get(j) <span style="color: rgba(0, 0, 255, 1)">instanceof</span><span style="color: rgba(0, 0, 0, 1)"> Long) ) {
                        dataCell.setCellValue(((Number) dataList.get(i).get(j)).longValue());
                  } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (dataList.get(i).get(j) <span style="color: rgba(0, 0, 255, 1)">instanceof</span><span style="color: rgba(0, 0, 0, 1)"> Number) {
                        dataCell.setCellValue(((Number) dataList.get(i).get(j)).doubleValue());
                  } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                        dataCell.setCellValue(dataList.get(i).get(j) </span>== <span style="color: rgba(0, 0, 255, 1)">null</span> ? ""<span style="color: rgba(0, 0, 0, 1)"> : dataList.get(i).get(j).toString());
                  }
                  Integer width </span>= widths.getOrDefault(j, 20<span style="color: rgba(0, 0, 0, 1)">);
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span>(dataList.get(i).get(j).toString().length() &gt; width) width =<span style="color: rgba(0, 0, 0, 1)"> dataList.get(i).get(j).toString().length();
                  widths.put(j, width);
                }
            }
            </span><span style="color: rgba(0, 0, 255, 1)">for</span> (Map.Entry&lt;Integer, Integer&gt;<span style="color: rgba(0, 0, 0, 1)"> entry : widths.entrySet()) {
                sheet.setColumnWidth(entry.getKey(), entry.getValue() </span>* 256<span style="color: rgba(0, 0, 0, 1)">);
            }
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 写入文件</span>
            ServletOutputStream outputStream =<span style="color: rgba(0, 0, 0, 1)"> response.getOutputStream();
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
      }
      </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> CellStyle getHeadCellStyle(Workbook workbook) {
            CellStyle style </span>=<span style="color: rgba(0, 0, 0, 1)"> workbook.createCellStyle();
            style.setBorderBottom(BorderStyle.THIN);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置底边框;</span>
            style.setBottomBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置底边框颜色;</span>
            style.setBorderLeft(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置左边框;</span>
            style.setLeftBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置左边框颜色;</span>
            style.setBorderRight(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置右边框;</span>
            style.setRightBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置右边框颜色;</span>
            style.setBorderTop(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置顶边框;</span>
            style.setTopBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置顶边框颜色;</span>
<span style="color: rgba(0, 0, 0, 1)">

            style.setAlignment(HorizontalAlignment.CENTER);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置水平对齐的样式为居中对齐;</span>
            style.setVerticalAlignment(VerticalAlignment.CENTER);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置垂直对齐的样式为居中对齐;</span>
<span style="color: rgba(0, 0, 0, 1)">            style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            style.setWrapText(</span><span style="color: rgba(0, 0, 255, 1)">true</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置自动换行;</span>
            style.setShrinkToFit(<span style="color: rgba(0, 0, 255, 1)">true</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置文本收缩至合适</span>
<span style="color: rgba(0, 0, 0, 1)">
            Font font </span>=<span style="color: rgba(0, 0, 0, 1)"> workbook.createFont();
            font.setBold(</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
            font.setFontHeightInPoints((</span><span style="color: rgba(0, 0, 255, 1)">short</span>) 10<span style="color: rgba(0, 0, 0, 1)">);
            style.setFont(font);
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> style;
      }
      </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> CellStyle getContentCellStyle(Workbook workbook) {
            CellStyle style </span>=<span style="color: rgba(0, 0, 0, 1)"> workbook.createCellStyle();
            style.setBorderBottom(BorderStyle.THIN);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置底边框;</span>
            style.setBottomBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置底边框颜色;</span>
            style.setBorderLeft(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置左边框;</span>
            style.setLeftBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置左边框颜色;</span>
            style.setBorderRight(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置右边框;</span>
            style.setRightBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置右边框颜色;</span>
            style.setBorderTop(BorderStyle.THIN);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置顶边框;</span>
            style.setTopBorderColor((<span style="color: rgba(0, 0, 255, 1)">short</span>) 0); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置顶边框颜色;</span>
<span style="color: rgba(0, 0, 0, 1)">
            style.setWrapText(</span><span style="color: rgba(0, 0, 255, 1)">true</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置自动换行;</span>
<span style="color: rgba(0, 0, 0, 1)">
            style.setAlignment(HorizontalAlignment.CENTER);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置水平对齐的样式为居中对齐;</span>
            style.setVerticalAlignment(VerticalAlignment.CENTER);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置垂直对齐的样式为居中对齐;</span>
            style.setShrinkToFit(<span style="color: rgba(0, 0, 255, 1)">true</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置文本收缩至合适</span>
            Font font =<span style="color: rgba(0, 0, 0, 1)"> workbook.createFont();
            font.setBold(</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
            font.setFontHeightInPoints((</span><span style="color: rgba(0, 0, 255, 1)">short</span>) 9<span style="color: rgba(0, 0, 0, 1)">);
            style.setFont(font);
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> style;
      }
    }







}</span></pre>
</div>
<p>&nbsp;</p>
</div><br><br>
来源:https://www.cnblogs.com/tangzeqi/p/18880124
頁: [1]
查看完整版本: 记录一个利用alibaba的easyexcel实现的简易excel导出工具(完整版)