Skip to content

Commit 8cceadf

Browse files
committed
格式化源文件
1 parent dd7ed08 commit 8cceadf

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

fastexcel-core/src/main/java/cn/idev/excel/write/merge/DynamicMergeStrategy.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.util.Deque;
1212

1313
/**
14-
* @Description Specifies that the column merges adjacent cells with the same content
14+
* @Description Specifies that the column merges adjacent cells with the same content
1515
* @Date 2025/3/8
1616
*/
1717
public class DynamicMergeStrategy implements RowWriteHandler {
@@ -30,47 +30,49 @@ public class DynamicMergeStrategy implements RowWriteHandler {
3030
private final int dataSize;
3131
private final Deque<MergeRow> rowStack = new ArrayDeque<>();
3232

33-
public DynamicMergeStrategy(int columnIndex,int dataSize) {
34-
this(columnIndex,1,dataSize);
33+
public DynamicMergeStrategy(int columnIndex, int dataSize) {
34+
this(columnIndex, 1, dataSize);
3535
}
36-
public DynamicMergeStrategy(int columnIndex, int columnExtend,int dataSize) {
36+
37+
public DynamicMergeStrategy(int columnIndex, int columnExtend, int dataSize) {
3738
if (columnExtend < 1) {
3839
throw new IllegalArgumentException("ColumnExtend must be greater than 1");
3940
}
4041
if (columnIndex < 0) {
4142
throw new IllegalArgumentException("ColumnIndex must be greater than 0");
4243
}
43-
if(dataSize<=0){
44+
if (dataSize <= 0) {
4445
throw new IllegalArgumentException("dataSize must be greater than 0");
4546
}
4647
this.columnIndex = columnIndex;
4748
this.columnExtend = columnExtend;
4849
this.dataSize = dataSize;
4950

5051
}
52+
5153
@Override
5254
public void afterRowDispose(RowWriteHandlerContext context) {
5355
if (context.getHead() || context.getRelativeRowIndex() == null) {
5456
return;
5557
}
5658
Row row = context.getRow();
5759
rowStack.push(new MergeRow(row, context.getRelativeRowIndex()));
58-
if(context.getRelativeRowIndex()==(dataSize-1)){
59-
while (!rowStack.isEmpty()){
60-
MergeRow lastRow = rowStack.pop();
61-
while (!rowStack.isEmpty()){
60+
if (context.getRelativeRowIndex() == (dataSize - 1)) {
61+
while (!rowStack.isEmpty()) {
62+
MergeRow lastRow = rowStack.pop();
63+
while (!rowStack.isEmpty()) {
6264
MergeRow prevRow = rowStack.pop();
6365

6466
if (!prevRow.getRow().getCell(columnIndex).getStringCellValue().equals(lastRow.getRow().getCell(columnIndex).getStringCellValue())) {
65-
if(lastRow.getRow().getRowNum()!=(prevRow.getRow().getRowNum()+1)){
66-
CellRangeAddress cellRangeAddress = new CellRangeAddress(prevRow.getRow().getRowNum()+1,
67+
if (lastRow.getRow().getRowNum() != (prevRow.getRow().getRowNum() + 1)) {
68+
CellRangeAddress cellRangeAddress = new CellRangeAddress(prevRow.getRow().getRowNum() + 1,
6769
lastRow.getRow().getRowNum(), columnIndex, columnIndex + columnExtend - 1);
6870
context.getWriteSheetHolder().getSheet().addMergedRegionUnsafe(cellRangeAddress);
6971
}
7072
rowStack.push(prevRow);
7173
break;
72-
}else {
73-
if(prevRow.getRelativeRowIndex().equals(0)){
74+
} else {
75+
if (prevRow.getRelativeRowIndex().equals(0)) {
7476
CellRangeAddress cellRangeAddress = new CellRangeAddress(prevRow.getRow().getRowNum(),
7577
lastRow.getRow().getRowNum(), columnIndex, columnIndex + columnExtend - 1);
7678
context.getWriteSheetHolder().getSheet().addMergedRegionUnsafe(cellRangeAddress);
@@ -85,7 +87,7 @@ public void afterRowDispose(RowWriteHandlerContext context) {
8587

8688
@Data
8789
@AllArgsConstructor
88-
public static class MergeRow{
90+
public static class MergeRow {
8991
private Row row;
9092
private Integer relativeRowIndex;
9193
}

fastexcel-test/src/test/java/cn/idev/excel/test/demo/write/WriteTest.java

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ public void handlerStyleWrite() {
460460
// 背景设置为红色
461461
headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
462462
WriteFont headWriteFont = new WriteFont();
463-
headWriteFont.setFontHeightInPoints((short)20);
463+
headWriteFont.setFontHeightInPoints((short) 20);
464464
headWriteCellStyle.setWriteFont(headWriteFont);
465465
// 内容的策略
466466
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
@@ -470,7 +470,7 @@ public void handlerStyleWrite() {
470470
contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
471471
WriteFont contentWriteFont = new WriteFont();
472472
// 字体大小
473-
contentWriteFont.setFontHeightInPoints((short)20);
473+
contentWriteFont.setFontHeightInPoints((short) 20);
474474
contentWriteCellStyle.setWriteFont(contentWriteFont);
475475
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
476476
HorizontalCellStyleStrategy horizontalCellStyleStrategy =
@@ -626,42 +626,42 @@ public void customHeadReadAndDynamicMergeStrategy() {
626626
EasyExcel.write(TestFileUtil.getPath() + "customHeadRead" + System.currentTimeMillis() + ".xlsx")
627627
.head(yearHead())
628628
.sheet("模板")
629-
.registerWriteHandler(new DynamicMergeStrategy(0,maps.size()))
630-
.registerWriteHandler(new DynamicMergeStrategy(2,maps.size()))
629+
.registerWriteHandler(new DynamicMergeStrategy(0, maps.size()))
630+
.registerWriteHandler(new DynamicMergeStrategy(2, maps.size()))
631631
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
632632
.doWrite(maps);
633633

634634
List<Map<Integer, String>> maps1 = yearData1();
635635
EasyExcel.write(TestFileUtil.getPath() + "customHeadRead" + System.currentTimeMillis() + ".xlsx")
636636
.head(yearHead())
637637
.sheet("模板")
638-
.registerWriteHandler(new DynamicMergeStrategy(0,maps1.size()))
639-
.registerWriteHandler(new DynamicMergeStrategy(2,maps1.size()))
638+
.registerWriteHandler(new DynamicMergeStrategy(0, maps1.size()))
639+
.registerWriteHandler(new DynamicMergeStrategy(2, maps1.size()))
640640
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
641641
.doWrite(maps1);
642642
List<Map<Integer, String>> maps2 = yearData2();
643643
EasyExcel.write(TestFileUtil.getPath() + "customHeadRead" + System.currentTimeMillis() + ".xlsx")
644644
.head(yearHead())
645645
.sheet("模板")
646-
.registerWriteHandler(new DynamicMergeStrategy(0,maps2.size()))
647-
.registerWriteHandler(new DynamicMergeStrategy(2,maps2.size()))
646+
.registerWriteHandler(new DynamicMergeStrategy(0, maps2.size()))
647+
.registerWriteHandler(new DynamicMergeStrategy(2, maps2.size()))
648648
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
649649
.doWrite(maps2);
650650
List<Map<Integer, String>> maps3 = yearData3();
651651
EasyExcel.write(TestFileUtil.getPath() + "customHeadRead" + System.currentTimeMillis() + ".xlsx")
652652
.head(yearHead())
653653
.sheet("模板")
654-
.registerWriteHandler(new DynamicMergeStrategy(0,maps3.size()))
655-
.registerWriteHandler(new DynamicMergeStrategy(2,maps3.size()))
654+
.registerWriteHandler(new DynamicMergeStrategy(0, maps3.size()))
655+
.registerWriteHandler(new DynamicMergeStrategy(2, maps3.size()))
656656
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
657657
.doWrite(maps3);
658658

659659
List<Map<Integer, String>> maps4 = yearData4();
660660
EasyExcel.write(TestFileUtil.getPath() + "customHeadRead" + System.currentTimeMillis() + ".xlsx")
661661
.head(yearHead())
662662
.sheet("模板")
663-
.registerWriteHandler(new DynamicMergeStrategy(0,maps4.size()))
664-
.registerWriteHandler(new DynamicMergeStrategy(2,maps4.size()))
663+
.registerWriteHandler(new DynamicMergeStrategy(0, maps4.size()))
664+
.registerWriteHandler(new DynamicMergeStrategy(2, maps4.size()))
665665
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
666666
.doWrite(maps4);
667667
}
@@ -670,94 +670,100 @@ public List<List<String>> yearHead() {
670670
List<List<String>> head = new ArrayList<>();
671671
for (int i = 0; i < 12; i++) {
672672
List<String> h = new ArrayList<>();
673-
if(i<3){
673+
if (i < 3) {
674674
h.add("第一季度");
675675
}
676-
if(i>=3&&i<6){
676+
if (i >= 3 && i < 6) {
677677
h.add("第二季度");
678678
}
679-
if(i>=6&&i<9){
679+
if (i >= 6 && i < 9) {
680680
h.add("第三季度");
681681
}
682-
if(i>=9){
682+
if (i >= 9) {
683683
h.add("第四季度");
684684
}
685685
h.add("第" + (i + 1) + "月");
686686
head.add(h);
687687
}
688688
return head;
689689
}
690+
690691
public List<Map<Integer, String>> yearData() {
691692
List<Map<Integer, String>> data = new ArrayList<>();
692693
for (int i = 0; i < 100; i++) {
693694
Map<Integer, String> map = new HashMap<>();
694695
for (int j = 0; j < 12; j++) {
695-
if(i<20){
696-
map.put( j , "第" + (j + 1) + "月"+"前20条数据");
697-
}else {
698-
map.put( j , "第" + (j + 1) + "月");
696+
if (i < 20) {
697+
map.put(j, "第" + (j + 1) + "月" + "前20条数据");
698+
} else {
699+
map.put(j, "第" + (j + 1) + "月");
699700
}
700701

701702
}
702703
data.add(map);
703704
}
704705
return data;
705706
}
707+
706708
public List<Map<Integer, String>> yearData1() {
707709
List<Map<Integer, String>> data = new ArrayList<>();
708710
for (int i = 0; i < 3; i++) {
709711
Map<Integer, String> map = new HashMap<>();
710712
for (int j = 0; j < 12; j++) {
711-
if(i>0){
712-
map.put( j , (j + 1) + "");
713-
}else {
714-
map.put(j,i+"");
713+
if (i > 0) {
714+
map.put(j, (j + 1) + "");
715+
} else {
716+
map.put(j, i + "");
715717
}
716718

717719
}
718720
data.add(map);
719721
}
720722
return data;
721723
}
724+
722725
public List<Map<Integer, String>> yearData4() {
723726
List<Map<Integer, String>> data = new ArrayList<>();
724727
for (int i = 0; i < 3; i++) {
725728
Map<Integer, String> map = new HashMap<>();
726729
for (int j = 0; j < 12; j++) {
727-
if(i<2){
728-
map.put( j , (j + 1) + "");
729-
}else {
730-
map.put(j,i+"");
730+
if (i < 2) {
731+
map.put(j, (j + 1) + "");
732+
} else {
733+
map.put(j, i + "");
731734
}
732735

733736
}
734737
data.add(map);
735738
}
736739
return data;
737740
}
741+
738742
public List<Map<Integer, String>> yearData3() {
739743
List<Map<Integer, String>> data = new ArrayList<>();
740744
for (int i = 0; i < 3; i++) {
741745
Map<Integer, String> map = new HashMap<>();
742746
for (int j = 0; j < 12; j++) {
743-
map.put(j,j+"");
747+
map.put(j, j + "");
744748

745749
}
746750
data.add(map);
747751
}
748752
return data;
749753
}
754+
750755
public List<Map<Integer, String>> yearData2() {
751756
List<Map<Integer, String>> data = new ArrayList<>();
752757
for (int i = 0; i < 3; i++) {
753758
Map<Integer, String> map = new HashMap<>();
754759
for (int j = 0; j < 12; j++) {
755-
map.put(j,i+"");
760+
map.put(j, i + "");
756761
}
757762
data.add(map);
758763
}
759764
return data;
760765
}
766+
761767
/**
762768
* 自动列宽(不太精确)
763769
* <p>

0 commit comments

Comments
 (0)