Skip to content

Commit

Permalink
修改head是List时,内容单元格的样式不生效 #1339
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuangjiaju committed May 25, 2020
1 parent 2c45fa0 commit e9b9048
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @author Jiaju Zhuang
*/
public abstract class AbstractCellStyleStrategy implements CellWriteHandler, WorkbookWriteHandler, NotRepeatExecutor {

boolean hasInitialized = false;

@Override
Expand Down Expand Up @@ -50,7 +51,7 @@ public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTable
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
if (isHead == null || head == null) {
if (isHead == null) {
return;
}
if (isHead) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.alibaba.excel.write.metadata.style.WriteCellStyle;

/**
*
* Use the same style for the column
*
* @author Jiaju Zhuang
Expand All @@ -30,6 +29,9 @@ protected void initCellStyle(Workbook workbook) {

@Override
protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
if (head == null) {
return;
}
int columnIndex = head.getColumnIndex();
if (headCellStyleCache.containsKey(columnIndex)) {
CellStyle cellStyle = headCellStyleCache.get(columnIndex);
Expand All @@ -50,6 +52,9 @@ protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex)

@Override
protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
if (head == null) {
return;
}
int columnIndex = head.getColumnIndex();
if (contentCellStyleCache.containsKey(columnIndex)) {
CellStyle cellStyle = contentCellStyleCache.get(columnIndex);
Expand All @@ -71,17 +76,15 @@ protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowInde
/**
* Returns the column width corresponding to each column head
*
* @param head
* Nullable
* @param head Nullable
* @return
*/
protected abstract WriteCellStyle headCellStyle(Head head);

/**
* Returns the column width corresponding to each column head
*
* @param head
* Nullable
* @param head Nullable
* @return
*/
protected abstract WriteCellStyle contentCellStyle(Head head);
Expand Down
86 changes: 86 additions & 0 deletions src/test/java/com/alibaba/easyexcel/test/temp/WriteV34Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.alibaba.easyexcel.test.temp;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.easyexcel.test.demo.write.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;

/**
* 临时测试
*
* @author Jiaju Zhuang
**/
@Ignore
public class WriteV34Test {

private static final Logger LOGGER = LoggerFactory.getLogger(WriteV34Test.class);

@Test
public void test() throws Exception {
String fileName = TestFileUtil.getPath() + "handlerStyleWrite" + System.currentTimeMillis() + ".xlsx";
// 头的策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// 背景设置为红色
headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short)20);
headWriteCellStyle.setWriteFont(headWriteFont);
// 内容的策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
// 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
// 背景绿色
contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
WriteFont contentWriteFont = new WriteFont();
// 字体大小
contentWriteFont.setFontHeightInPoints((short)20);
contentWriteCellStyle.setWriteFont(contentWriteFont);
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName).head(head()).registerWriteHandler(horizontalCellStyleStrategy).sheet("模板")
.doWrite(data(1));
}


private List<List<String>> head() {
List<List<String>> list = new ArrayList<List<String>>();
List<String> head0 = new ArrayList<String>();
head0.add("字符串" + System.currentTimeMillis());
List<String> head1 = new ArrayList<String>();
head1.add("数字" + System.currentTimeMillis());
List<String> head2 = new ArrayList<String>();
head2.add("日期" + System.currentTimeMillis());
list.add(head0);
list.add(head1);
list.add(head2);
return list;
}

private List<DemoData> data(int no) {
List<DemoData> list = new ArrayList<DemoData>();
for (int i = 0; i < 10; i++) {
DemoData data = new DemoData();
data.setString("字符串" + no + "---" + i);
list.add(data);
}
return list;
}

}
1 change: 1 addition & 0 deletions update.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* 修复07版仅有样式的空行 默认不忽略的bug
* 写入`sheet`不设置`index``name`默认不为0的问题
* 修复多个`sheet`不按照顺序写入 会乱序的bug [Issue #1332](https://github.com/alibaba/easyexcel/issues/1332)
* 修改head是List时,内容单元格的样式不生效 [Issue #1339](https://github.com/alibaba/easyexcel/issues/1339)


# 2.2.3
Expand Down

0 comments on commit e9b9048

Please sign in to comment.