当前位置:网站首页>Easypoi export interlaced style settings
Easypoi export interlaced style settings
2022-07-28 07:09:00 【Fu Hua-】
export excel You need to set the interlaced background style , Here's the picture :
1、ExcelUtil Export tool class
/** * Export set interlaced background color * @param params * @param list * @param pojoClass * @param fileName * @param response */
public static void exportExcel( ExportParams params,List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response) throws IOException {
Workbook workbook = ExcelExportUtil.exportExcel(params,pojoClass,list);
if (workbook != null);
setRowBackground(workbook);
downLoadExcel(fileName, response, workbook);
}
/** * Set the background color for even rows */
private static void setRowBackground(Workbook workbook){
Sheet sheet = workbook.getSheetAt(0);
CellStyle styles = ExcelStyleUtil.getStyles(workbook,false,(short) 12);
for(int i = 0; i <= sheet.getLastRowNum(); i ++) {
Row row = sheet.getRow(i);
if (i%2==0 && i>0){
// The title uses the global title style , Don't set the style separately , So exclude the title
for(int j = 0; j < row.getPhysicalNumberOfCells(); j ++) {
Cell cell = row.getCell(j);
cell.setCellStyle(styles);
}
}
}
}
/** * download * @param fileName File name * @param response * @param workbook excel data */
public static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + "." + ExcelTypeEnum.XLS.getValue(), "UTF-8"));
workbook.write(response.getOutputStream());
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
2、ExcelStyleUtil Style tool class
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams;
import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
import org.apache.poi.ss.usermodel.*;
public class ExcelStyleUtil implements IExcelExportStyler {
private static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("TEXT");
private static final short FONT_SIZE_TEN = 12;
private static final short FONT_SIZE_ELEVEN = 14;
private static final short FONT_SIZE_TWELVE = 18;
/** * Headline style */
private CellStyle headerStyle;
/** * Each column header style */
private CellStyle titleStyle;
/** * Data line style */
private CellStyle styles;
public ExcelStyleUtil(Workbook workbook) {
this.init(workbook);
}
/** * Initialize style * * @param workbook */
private void init(Workbook workbook) {
this.headerStyle = initHeaderStyle(workbook);
this.titleStyle = initTitleStyle(workbook);
this.styles = initStyles(workbook);
}
/** * Headline style * * @param color * @return */
@Override
public CellStyle getHeaderStyle(short color) {
return headerStyle;
}
/** * Each column header style * * @param color * @return */
@Override
public CellStyle getTitleStyle(short color) {
return titleStyle;
}
/** * Data line style * * @param parity Can be used to represent even and odd rows * @param entity The data content * @return style */
@Override
public CellStyle getStyles(boolean parity, ExcelExportEntity entity) {
return styles;
}
/** * Get style method * * @param dataRow data row * @param obj object * @param data data */
@Override
public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity entity, Object obj, Object data) {
return getStyles(true, entity);
}
/** * The style settings used by the template */
@Override
public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams) {
return null;
}
/** * Set interlaced background color */
public static CellStyle getStyles(Workbook workbook,boolean isBold,short size) {
CellStyle style = getBaseCellStyle(workbook);
style.setFont(getFont(workbook, size,isBold));
// Background color
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setDataFormat(STRING_FORMAT);
return style;
}
/** * initialization -- Headline style * * @param workbook * @return */
private CellStyle initHeaderStyle(Workbook workbook) {
CellStyle style = getBaseCellStyle(workbook);
style.setFont(getFont(workbook, FONT_SIZE_TWELVE, true));
return style;
}
/** * initialization -- Each column header style * * @param workbook * @return */
private CellStyle initTitleStyle(Workbook workbook) {
CellStyle style = getBaseCellStyle(workbook);
style.setFont(getFont(workbook, FONT_SIZE_ELEVEN, true));
// Background color
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
return style;
}
/** * initialization -- Data line style * * @param workbook * @return */
private CellStyle initStyles(Workbook workbook) {
CellStyle style = getBaseCellStyle(workbook);
style.setFont(getFont(workbook, FONT_SIZE_TEN, false));
style.setDataFormat(STRING_FORMAT);
return style;
}
/** * Basic style * * @return */
private static CellStyle getBaseCellStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
// Under the frame
style.setBorderBottom(BorderStyle.THIN);
// The left margin
style.setBorderLeft(BorderStyle.THIN);
// On the border
style.setBorderTop(BorderStyle.THIN);
// Right margin
style.setBorderRight(BorderStyle.THIN);
// Horizontal center
style.setAlignment(HorizontalAlignment.CENTER);
// Center up and down
style.setVerticalAlignment(VerticalAlignment.CENTER);
// Set auto wrap
style.setWrapText(true);
return style;
}
/** * Font style * * @param size font size * @param isBold Is it bold * @return */
private static Font getFont(Workbook workbook, short size, boolean isBold) {
Font font = workbook.createFont();
// Font style
font.setFontName(" Song style ");
// Is it bold
font.setBold(isBold);
// font size
font.setFontHeightInPoints(size);
return font;
}
}
3、 export
/** * export */
@RequestMapping("/export")
public void export(SysUser query, HttpServletResponse response) throws IOException {
ExportParams params = new ExportParams();
params.setTitleHeight((short) 15);
params.setHeight((short) 12);
params.setStyle(ExcelStyleUtil.class);
List<SysUser> list = sysUserService.list(query);
ExcelUtils.exportExcel(params,list, SysUser.class," User list ",response);
}
边栏推荐
- Blue bridge code error ticket
- DOM operation cases
- Erudite Valley Learning Records] Super summary, attentive sharing | common APIs
- Esxi community nvme driver update v1.1
- MySQL排除节假日,计算日期差
- Svg understanding and drawing application
- DOM -- page rendering, style attribute operation, preloading and lazy loading, anti shake and throttling
- Starting point Chinese website font anti crawling technology web page can display numbers and letters, and the web page code is garbled or blank
- Standard C language summary 4
- shell---sed语句练习
猜你喜欢

DHCP服务

MOOC翁恺C语言第八周:指针与字符串:1.指针2.字符类型3.字符串4.字符串计算

PXE无人值守安装管理

爬虫学习总结

Custom components -- styles

metasploit渗透ms7_010练习

Neo4j运行报错Error occurred during initialization of VM Incompatible minimum and maximum heap sizes spec

Esxi community network card driver

Firewall - iptables firewall (four tables and five links, firewall configuration method, detailed explanation of matching rules)

Applet creation component
随机推荐
根据excel生成create建表SQL语句
DNS domain name resolution
Blue Bridge Cup square filling number
Implementation method of Bert
Standard C language learning summary 5
NAT network address translation
shell---条件语句练习
Standard C language learning summary 3
Uni app double click event simulation
小甲鱼C(第五章循环控制结构程序567)break和continue语句
分解路径为目录名和文件名的方法
Differences and relationships among NPM, Yran and NPX
[learning records of erudite Valley] Super summary, attentive sharing | collection
MOOC Weng Kai C language week 7: array operation: 1. array operation 2. Search 3. preliminary sorting
Blue bridge code error ticket
MOOC翁恺C语言第八周:指针与字符串:1.指针2.字符类型3.字符串4.字符串计算
shell---sed语句练习
MOOC Weng Kai C language week 8: pointer and string: 1. Pointer 2. Character type 3. String 4. String calculation
[learning notes] process creation
Shell script - regular expression