Browse Source

修改head是List时,内容单元格的样式不生效 #1339

developing
Jiaju Zhuang 5 years ago
parent
commit
e9b904884e
  1. 3
      src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java
  2. 13
      src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java
  3. 86
      src/test/java/com/alibaba/easyexcel/test/temp/WriteV34Test.java
  4. 1
      update.md

3
src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java

@ -22,6 +22,7 @@ import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
* @author Jiaju Zhuang
*/
public abstract class AbstractCellStyleStrategy implements CellWriteHandler, WorkbookWriteHandler, NotRepeatExecutor {
boolean hasInitialized = false;
@Override
@ -50,7 +51,7 @@ public abstract class AbstractCellStyleStrategy implements CellWriteHandler, Wor
@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) {

13
src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java

@ -12,7 +12,6 @@ import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
/**
*
* Use the same style for the column
*
* @author Jiaju Zhuang
@ -30,6 +29,9 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl
@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);
@ -50,6 +52,9 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl
@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);
@ -71,8 +76,7 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl
/**
* Returns the column width corresponding to each column head
*
* @param head
* Nullable
* @param head Nullable
* @return
*/
protected abstract WriteCellStyle headCellStyle(Head head);
@ -80,8 +84,7 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl
/**
* Returns the column width corresponding to each column head
*
* @param head
* Nullable
* @param head Nullable
* @return
*/
protected abstract WriteCellStyle contentCellStyle(Head head);

86
src/test/java/com/alibaba/easyexcel/test/temp/WriteV34Test.java

@ -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
update.md

@ -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

Loading…
Cancel
Save