Browse Source

Improved the code

developing
dota17 5 years ago
parent
commit
8368d37338
  1. 7
      src/test/java/com/alibaba/easyexcel/test/core/style/AnnotationStyleData.java
  2. 6
      src/test/java/com/alibaba/easyexcel/test/core/style/SimpleVerticalCellStyleStrategy.java
  3. 30
      src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java

7
src/test/java/com/alibaba/easyexcel/test/core/style/StyleOtherData.java → src/test/java/com/alibaba/easyexcel/test/core/style/AnnotationStyleData.java

@ -9,9 +9,10 @@ import lombok.Data;
* @author Pengliang Zhao * @author Pengliang Zhao
*/ */
@Data @Data
public class StyleOtherData { public class AnnotationStyleData {
@ContentStyle(dataFormat = (short)1, rotation = (short)1, indent = (short)1, leftBorderColor = (short)1, rightBorderColor = (short)1, @ContentStyle(dataFormat = (short)1, rotation = (short)1, indent = (short)1, leftBorderColor = (short)1,
topBorderColor = (short)1, bottomBorderColor = (short)1, fillForegroundColor = (short)1, fillBackgroundColor = (short)1) rightBorderColor = (short)1, topBorderColor = (short)1, bottomBorderColor = (short)1,
fillForegroundColor = (short)1, fillBackgroundColor = (short)1)
@ContentFontStyle(fontHeightInPoints = (short)1, color = (short)1, typeOffset = (short)1, charset = (short)1) @ContentFontStyle(fontHeightInPoints = (short)1, color = (short)1, typeOffset = (short)1, charset = (short)1)
@ExcelProperty("字符串") @ExcelProperty("字符串")
private String string; private String string;

6
src/test/java/com/alibaba/easyexcel/test/core/style/SimpleVerticalCellStyleStrategy.java

@ -17,13 +17,13 @@ import com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy;
* @author Pengliang Zhao * @author Pengliang Zhao
*/ */
public class SimpleVerticalCellStyleStrategy extends AbstractVerticalCellStyleStrategy { public class SimpleVerticalCellStyleStrategy extends AbstractVerticalCellStyleStrategy {
private StyleProperty styleProperty = StyleProperty.build(StyleOtherData.class.getAnnotation(ContentStyle.class)); private StyleProperty styleProperty = StyleProperty.build(AnnotationStyleData.class.getAnnotation(ContentStyle.class));
private FontProperty fontProperty = FontProperty.build(StyleOtherData.class.getAnnotation(ContentFontStyle.class)); private FontProperty fontProperty = FontProperty.build(AnnotationStyleData.class.getAnnotation(ContentFontStyle.class));
@Override @Override
protected WriteCellStyle headCellStyle(Head head) { protected WriteCellStyle headCellStyle(Head head) {
WriteCellStyle writeCellStyle = WriteCellStyle.build(styleProperty, fontProperty); WriteCellStyle writeCellStyle = WriteCellStyle.build(styleProperty, fontProperty);
if(head.getColumnIndex() == 0) { if (head.getColumnIndex() == 0) {
writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
WriteFont writeFont = new WriteFont(); WriteFont writeFont = new WriteFont();
writeFont.setItalic(true); writeFont.setItalic(true);

30
src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java

@ -132,23 +132,22 @@ public class StyleDataTest {
@Test @Test
public void t05AbstractVerticalCellStyleStrategy03() { public void t05AbstractVerticalCellStyleStrategy03() {
AbstractVerticalCellStyleStrategy verticalCellStyleStrategy = new SimpleVerticalCellStyleStrategy(); AbstractVerticalCellStyleStrategy verticalCellStyleStrategy = new SimpleVerticalCellStyleStrategy();
EasyExcel.write(file04, StyleOtherData.class).registerWriteHandler(verticalCellStyleStrategy).sheet() EasyExcel.write(file04, AnnotationStyleData.class).registerWriteHandler(verticalCellStyleStrategy).sheet()
.doWrite(data1()); .doWrite(data1());
} }
@Test @Test
public void t06longestMatchColumnWidthStyleStrategy() { public void t06longestMatchColumnWidthStyleStrategy() {
EasyExcel.write(file05, StyleData.class) EasyExcel.write(file05, StyleData.class).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("模板").doWrite(data()); .sheet("模板").doWrite(data());
} }
@Test @Test
public void t07loopMergeStrategy() { public void t07loopMergeStrategy() {
LoopMergeProperty loopMergeProperty = new LoopMergeProperty(1,2); LoopMergeProperty loopMergeProperty = new LoopMergeProperty(1, 2);
loopMergeProperty.setEachRow(2); loopMergeProperty.setEachRow(2);
loopMergeProperty.setColumnExtend(1); loopMergeProperty.setColumnExtend(1);
EasyExcel.write(file06, LoopMergeData.class) EasyExcel.write(file06, LoopMergeData.class).registerWriteHandler(new LoopMergeStrategy(loopMergeProperty, 1))
.registerWriteHandler(new LoopMergeStrategy(loopMergeProperty, 1))
.sheet("模板").doWrite(data2()); .sheet("模板").doWrite(data2());
} }
@ -157,9 +156,10 @@ public class StyleDataTest {
OnceAbsoluteMerge onceAbsoluteMerge = OnceAbsoluteMergeData.class.getAnnotation(OnceAbsoluteMerge.class); OnceAbsoluteMerge onceAbsoluteMerge = OnceAbsoluteMergeData.class.getAnnotation(OnceAbsoluteMerge.class);
OnceAbsoluteMergeProperty onceAbsoluteMergeProperty = OnceAbsoluteMergeProperty.build(onceAbsoluteMerge); OnceAbsoluteMergeProperty onceAbsoluteMergeProperty = OnceAbsoluteMergeProperty.build(onceAbsoluteMerge);
OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(onceAbsoluteMergeProperty); OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(onceAbsoluteMergeProperty);
EasyExcel.write(file08, OnceAbsoluteMergeData.class) EasyExcel.write(file08, OnceAbsoluteMergeData.class).registerWriteHandler(onceAbsoluteMergeStrategy).sheet("模板")
.registerWriteHandler(onceAbsoluteMergeStrategy).sheet("模板").doWrite(data3()); .doWrite(data3());
} }
private void readAndWrite(File file) { private void readAndWrite(File file) {
SimpleColumnWidthStyleStrategy simpleColumnWidthStyleStrategy = new SimpleColumnWidthStyleStrategy(50); SimpleColumnWidthStyleStrategy simpleColumnWidthStyleStrategy = new SimpleColumnWidthStyleStrategy(50);
SimpleRowHeightStyleStrategy simpleRowHeightStyleStrategy = SimpleRowHeightStyleStrategy simpleRowHeightStyleStrategy =
@ -199,12 +199,12 @@ public class StyleDataTest {
return list; return list;
} }
private List<StyleOtherData> data1() { private List<AnnotationStyleData> data1() {
List<StyleOtherData> list = new ArrayList<StyleOtherData>(); List<AnnotationStyleData> list = new ArrayList<AnnotationStyleData>();
StyleOtherData data = new StyleOtherData(); AnnotationStyleData data = new AnnotationStyleData();
data.setString("字符串0"); data.setString("字符串0");
data.setString1("字符串01"); data.setString1("字符串01");
StyleOtherData data1 = new StyleOtherData(); AnnotationStyleData data1 = new AnnotationStyleData();
data1.setString("字符串1"); data1.setString("字符串1");
data1.setString1("字符串11"); data1.setString1("字符串11");
list.add(data); list.add(data);
@ -218,7 +218,7 @@ public class StyleDataTest {
LoopMergeData loopMergeData = new LoopMergeData(); LoopMergeData loopMergeData = new LoopMergeData();
loopMergeData.setCategory("洗漱用品"); loopMergeData.setCategory("洗漱用品");
loopMergeData.setTowel("毛巾"); loopMergeData.setTowel("毛巾");
loopMergeData.setSize("10" + 1 + "#"); loopMergeData.setSize("10" + i + "#");
list.add(loopMergeData); list.add(loopMergeData);
} }
return list; return list;
@ -230,7 +230,7 @@ public class StyleDataTest {
OnceAbsoluteMergeData onceAbsoluteMergeData = new OnceAbsoluteMergeData(); OnceAbsoluteMergeData onceAbsoluteMergeData = new OnceAbsoluteMergeData();
onceAbsoluteMergeData.setCategory("洗漱用品"); onceAbsoluteMergeData.setCategory("洗漱用品");
onceAbsoluteMergeData.setTowel("毛巾"); onceAbsoluteMergeData.setTowel("毛巾");
onceAbsoluteMergeData.setSize("10" + 1 + "#"); onceAbsoluteMergeData.setSize("10" + i + "#");
list.add(onceAbsoluteMergeData); list.add(onceAbsoluteMergeData);
} }
return list; return list;

Loading…
Cancel
Save