工具類(lèi)
package com.alibaba.excel.write.style;
import java.util.List;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
/**
* Use the same style for the row
*
* @author Jiaju Zhuang
*/
@Getter
@Setter
@EqualsAndHashCode
public class HorizontalCellStyleStrategy extends AbstractCellStyleStrategy {
private WriteCellStyle headWriteCellStyle;
private List<WriteCellStyle> contentWriteCellStyleList;
public HorizontalCellStyleStrategy() {
}
public HorizontalCellStyleStrategy(WriteCellStyle headWriteCellStyle,
List<WriteCellStyle> contentWriteCellStyleList) {
this.headWriteCellStyle = headWriteCellStyle;
this.contentWriteCellStyleList = contentWriteCellStyleList;
}
public HorizontalCellStyleStrategy(WriteCellStyle headWriteCellStyle, WriteCellStyle contentWriteCellStyle) {
this.headWriteCellStyle = headWriteCellStyle;
if (contentWriteCellStyle != null) {
this.contentWriteCellStyleList = ListUtils.newArrayList(contentWriteCellStyle);
}
}
@Override
protected void setHeadCellStyle(CellWriteHandlerContext context) {
if (stopProcessing(context) || headWriteCellStyle == null) {
return;
}
WriteCellData<?> cellData = context.getFirstCellData();
WriteCellStyle.merge(headWriteCellStyle, cellData.getOrCreateStyle());
}
@Override
protected void setContentCellStyle(CellWriteHandlerContext context) {
if (stopProcessing(context) || CollectionUtils.isEmpty(contentWriteCellStyleList)) {
return;
}
WriteCellData<?> cellData = context.getFirstCellData();
if (context.getRelativeRowIndex() == null || context.getRelativeRowIndex() <= 0) {
WriteCellStyle.merge(contentWriteCellStyleList.get(0), cellData.getOrCreateStyle());
} else {
WriteCellStyle.merge(
contentWriteCellStyleList.get(context.getRelativeRowIndex() % contentWriteCellStyleList.size()),
cellData.getOrCreateStyle());
}
}
protected boolean stopProcessing(CellWriteHandlerContext context) {
return context.getFirstCellData() == null;
}
}
package com.bytz.modules.cms.statistics.excel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.poi.ss.usermodel.*;
public class StyleUtils {
/**
* 標(biāo)題樣式
* @return
*/
public static WriteCellStyle getHeadStyle(){
// 頭的策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// 背景顏色
// headWriteCellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE1.getIndex());
// headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
// 字體
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontName("宋體");//設(shè)置字體名字
headWriteFont.setFontHeightInPoints((short)14);//設(shè)置字體大小
headWriteFont.setBold(true);//字體加粗
headWriteCellStyle.setWriteFont(headWriteFont); //在樣式用應(yīng)用設(shè)置的字體;
// 樣式
headWriteCellStyle.setBorderBottom(BorderStyle.THIN);//設(shè)置底邊框;
headWriteCellStyle.setBottomBorderColor((short) 0);//設(shè)置底邊框顏色;
headWriteCellStyle.setBorderLeft(BorderStyle.THIN); //設(shè)置左邊框;
headWriteCellStyle.setLeftBorderColor((short) 0);//設(shè)置左邊框顏色;
headWriteCellStyle.setBorderRight(BorderStyle.THIN);//設(shè)置右邊框;
headWriteCellStyle.setRightBorderColor((short) 0);//設(shè)置右邊框顏色;
headWriteCellStyle.setBorderTop(BorderStyle.THIN);//設(shè)置頂邊框;
headWriteCellStyle.setTopBorderColor((short) 0); //設(shè)置頂邊框顏色;
headWriteCellStyle.setWrapped(true); //設(shè)置自動(dòng)換行;
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//設(shè)置水平對(duì)齊的樣式為居中對(duì)齊;
headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //設(shè)置垂直對(duì)齊的樣式為居中對(duì)齊;
headWriteCellStyle.setShrinkToFit(true);//設(shè)置文本收縮至合適
return headWriteCellStyle;
}
/**
* 內(nèi)容樣式
* @return
*/
public static WriteCellStyle getContentStyle(){
// 內(nèi)容的策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
// 背景綠色
// 這里需要指定 FillPatternType 為FillPatternType.SOLID_FOREGROUND 不然無(wú)法顯示背景顏色.頭默認(rèn)了 FillPatternType所以可以不指定
// contentWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
// contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
// 設(shè)置字體
WriteFont contentWriteFont = new WriteFont();
contentWriteFont.setFontHeightInPoints((short) 12);//設(shè)置字體大小
contentWriteFont.setFontName("宋體"); //設(shè)置字體名字
contentWriteCellStyle.setWriteFont(contentWriteFont);//在樣式用應(yīng)用設(shè)置的字體;
//設(shè)置樣式;
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);//設(shè)置底邊框;
contentWriteCellStyle.setBottomBorderColor((short) 0);//設(shè)置底邊框顏色;
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN); //設(shè)置左邊框;
contentWriteCellStyle.setLeftBorderColor((short) 0);//設(shè)置左邊框顏色;
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);//設(shè)置右邊框;
contentWriteCellStyle.setRightBorderColor((short) 0);//設(shè)置右邊框顏色;
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);//設(shè)置頂邊框;
contentWriteCellStyle.setTopBorderColor((short) 0); ///設(shè)置頂邊框顏色;
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);// 水平居中
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
contentWriteCellStyle.setWrapped(true); //設(shè)置自動(dòng)換行;
// contentWriteCellStyle.setShrinkToFit(true);//設(shè)置文本收縮至合適
return contentWriteCellStyle;
}
}
使用文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-511999.html
// 設(shè)置單元格樣式
HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(StyleUtils.getHeadStyle(), StyleUtils.getContentStyle());
//寫(xiě)入表頭
EasyExcel.write(outputStream)
.head(header)
.needHead(true)
.autoCloseStream(true)
.sheet()
.registerWriteHandler(horizontalCellStyleStrategy)
.doWrite(dataList)
;
主要是文字居中文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-511999.html
到了這里,關(guān)于EasyExcel設(shè)置表格樣式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!