easyexcel导出动态头、指定列、自定义样式(不创建对象)
easyexcel
快速、简洁、解决大文件内存溢出的java处理Excel工具
项目地址:https://gitcode.com/gh_mirrors/ea/easyexcel
免费下载资源
·
<!--easyexcel-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.8</version>
</dependency>
Controller:
public void exportDetailLeave(@RequestBody Map<String, Object> params, HttpServletResponse response) {
List<Map<String,Object>> list = faceBedService.selectBedOnlineBy(params);
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;");
String[] header = {"宿舍","姓名","学号","状态","归寝时间"};
String fileName = "实时离寝情况报表";
// 标题样式
WriteCellStyle headWriteCellStyle = EasyExcelUtil.getHeadStyle();
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(headWriteCellStyle, new WriteCellStyle());
EasyExcel.write(response.getOutputStream())
// 第一行大标题样式设置
.registerWriteHandler(new SheetWriteHandlerUtil(fileName,header))
//设置默认样式及写入头信息开始的行数
.useDefaultStyle(true).relativeHeadRowIndex(1)
// 表头、内容样式设置
.registerWriteHandler(horizontalCellStyleStrategy)
// 统一列宽,如需设置自动列宽则new LongestMatchColumnWidthStyleStrategy()
.registerWriteHandler(new SimpleColumnWidthStyleStrategy(25))
.sheet(fileName)
// 这里放入动态头
.head(head(header,fileName))
// 当然这里数据也可以用 List<List<String>> 去传入
.doWrite(detail(list));
}
private List<List<Object>> detail(List<Map<String,Object>> mapList) {
List<List<Object>> list = new ArrayList<List<Object>>();
for (Map<String, Object> map : mapList) {
List<Object> objectList = new ArrayList<>();
objectList.add(map.get("dorm"));
objectList.add(map.get("stuName"));
objectList.add(map.get("stuNo"));
objectList.add(map.get("status"));
objectList.add(map.get("returnLeave"));
list.add(objectList);
}
return list;
}
EasyExcelUtil:
/**动态头传入*/
public static List<List<String>> head(String[] header, String bigTitle) {
List<String> head0 = null;
List<List<String>> list = new LinkedList<List<String>>();
for (String h : header) {
head0 = new LinkedList<>();
head0.add(bigTitle);
head0.add(h);
list.add(head0);
}
return list;
}
public static WriteCellStyle getHeadStyle(){
// 头的策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// 背景颜色
headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
// 字体
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontName("黑体");//设置字体名字
headWriteFont.setFontHeightInPoints((short)15);//设置字体大小
headWriteFont.setBold(true);//字体加粗
headWriteCellStyle.setWriteFont(headWriteFont); //在样式用应用设置的字体;
// 样式
headWriteCellStyle.setBorderBottom(BorderStyle.THIN);//设置底边框;
headWriteCellStyle.setBottomBorderColor((short) 0);//设置底边框颜色;
headWriteCellStyle.setBorderLeft(BorderStyle.THIN); //设置左边框;
headWriteCellStyle.setLeftBorderColor((short) 0);//设置左边框颜色;
headWriteCellStyle.setBorderRight(BorderStyle.THIN);//设置右边框;
headWriteCellStyle.setRightBorderColor((short) 0);//设置右边框颜色;
headWriteCellStyle.setBorderTop(BorderStyle.THIN);//设置顶边框;
headWriteCellStyle.setTopBorderColor((short) 0); //设置顶边框颜色;
headWriteCellStyle.setWrapped(true); //设置自动换行;
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//设置水平对齐的样式为居中对齐;
headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //设置垂直对齐的样式为居中对齐;
// headWriteCellStyle.setShrinkToFit(true);//设置文本收缩至合适
return headWriteCellStyle;
}
SheetWriteHandlerUtil:(设置第一行大标题样式)
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.CellRangeAddress;
public class SheetWriteHandlerUtil implements SheetWriteHandler {
private final String title;
private final String[] header;
public SheetWriteHandlerUtil(String title, String[] header) {
this.title = title;
this.header = header;
}
@Override
public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
}
@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
Workbook workbook = writeWorkbookHolder.getWorkbook();
Sheet sheet = workbook.getSheetAt(0);
//设置标题
Row row1 = sheet.createRow(0);
row1.setHeight((short) 800);
Cell cell = row1.createCell(0);
//设置单元格内容
cell.setCellValue(title);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
Font font = workbook.createFont();
font.setBold(true);
font.setFontHeight((short) 400);
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
// 第一行大标题占位设置
sheet.addMergedRegionUnsafe(new CellRangeAddress(0, 0, 0, header.length-1));
}
}
ps:尽量每个registerWriteHandler拦截器功能单一/尽量少的拦截器,否则可能会产生冲突没效果。controller这里只自定义了标题样式,不自定义内容样式的原因是使用动态头可能会导致自定义的内容样式失效!!!
效果图:
GitHub 加速计划 / ea / easyexcel
31.64 K
7.47 K
下载
快速、简洁、解决大文件内存溢出的java处理Excel工具
最近提交(Master分支:3 个月前 )
c42183df
Bugfix 3 个月前
efa7dff6 * 重新加回 `commons-io`
3 个月前
更多推荐
已为社区贡献2条内容
所有评论(0)