From 2319610f3736608d8937043fc83c39f69f202ca0 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Wed, 20 Oct 2021 23:56:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=86=99=E7=9A=84=E6=A0=B7?= =?UTF-8?q?=E5=BC=8F=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../easyexcel/test/core/StyleTestUtils.java | 43 ++++++++++++ .../test/core/style/StyleDataTest.java | 65 +++++++++++++++---- 2 files changed, 97 insertions(+), 11 deletions(-) create mode 100644 src/test/java/com/alibaba/easyexcel/test/core/StyleTestUtils.java diff --git a/src/test/java/com/alibaba/easyexcel/test/core/StyleTestUtils.java b/src/test/java/com/alibaba/easyexcel/test/core/StyleTestUtils.java new file mode 100644 index 00000000..266f724a --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/StyleTestUtils.java @@ -0,0 +1,43 @@ +package com.alibaba.easyexcel.test.core; + +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFCell; + +public class StyleTestUtils { + + public static byte[] getFillForegroundColor(Cell cell) { + if (cell instanceof XSSFCell) { + return ((XSSFCell)cell).getCellStyle().getFillForegroundColorColor().getRGB(); + } else { + return short2byte(((HSSFCell)cell).getCellStyle().getFillForegroundColorColor().getTriplet()); + } + } + + public static byte[] getFontColor(Cell cell, Workbook workbook) { + if (cell instanceof XSSFCell) { + return ((XSSFCell)cell).getCellStyle().getFont().getXSSFColor().getRGB(); + } else { + return short2byte(((HSSFCell)cell).getCellStyle().getFont(workbook).getHSSFColor((HSSFWorkbook)workbook) + .getTriplet()); + } + } + + public static short getFontHeightInPoints(Cell cell, Workbook workbook) { + if (cell instanceof XSSFCell) { + return ((XSSFCell)cell).getCellStyle().getFont().getFontHeightInPoints(); + } else { + return ((HSSFCell)cell).getCellStyle().getFont(workbook).getFontHeightInPoints(); + } + } + + private static byte[] short2byte(short[] shorts) { + byte[] bytes = new byte[shorts.length]; + for (int i = 0; i < shorts.length; i++) { + bytes[i] = (byte)shorts[i]; + } + return bytes; + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java index ceb571db..a48c3238 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java @@ -4,14 +4,15 @@ import java.io.File; import java.util.ArrayList; import java.util.List; +import com.alibaba.easyexcel.test.core.StyleTestUtils; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.annotation.write.style.HeadFontStyle; import com.alibaba.excel.annotation.write.style.HeadStyle; import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.metadata.data.DataFormatData; import com.alibaba.excel.metadata.property.FontProperty; import com.alibaba.excel.metadata.property.StyleProperty; -import com.alibaba.excel.metadata.data.DataFormatData; import com.alibaba.excel.write.merge.LoopMergeStrategy; import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy; import com.alibaba.excel.write.metadata.style.WriteCellStyle; @@ -22,11 +23,17 @@ import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy; import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy; import org.apache.poi.ss.usermodel.BorderStyle; +import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.IndexedColors; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.VerticalAlignment; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; +import org.junit.Assert; import org.junit.BeforeClass; import org.junit.FixMethodOrder; import org.junit.Test; @@ -40,20 +47,26 @@ public class StyleDataTest { private static File file07; private static File file03; + private static File fileVerticalCellStyleStrategy07; + private static File fileVerticalCellStyleStrategy207; + private static File fileLoopMergeStrategy; @BeforeClass public static void init() { file07 = TestFileUtil.createNewFile("style07.xlsx"); file03 = TestFileUtil.createNewFile("style03.xls"); + fileVerticalCellStyleStrategy07 = TestFileUtil.createNewFile("verticalCellStyle.xlsx"); + fileVerticalCellStyleStrategy207 = TestFileUtil.createNewFile("verticalCellStyle2.xlsx"); + fileLoopMergeStrategy = TestFileUtil.createNewFile("loopMergeStrategy.xlsx"); } @Test - public void t01ReadAndWrite07() { + public void t01ReadAndWrite07() throws Exception { readAndWrite(file07); } @Test - public void t02ReadAndWrite03() { + public void t02ReadAndWrite03() throws Exception { readAndWrite(file03); } @@ -113,9 +126,12 @@ public class StyleDataTest { return writeCellStyle; } }; - EasyExcel.write(file07, StyleData.class).registerWriteHandler(verticalCellStyleStrategy).sheet() + EasyExcel.write(fileVerticalCellStyleStrategy07, StyleData.class).registerWriteHandler( + verticalCellStyleStrategy).sheet() .doWrite(data()); + } + @Test public void t04AbstractVerticalCellStyleStrategy02() { final StyleProperty styleProperty = StyleProperty.build(StyleData.class.getAnnotation(HeadStyle.class)); @@ -152,32 +168,36 @@ public class StyleDataTest { return writeCellStyle; } }; - EasyExcel.write(file07, StyleData.class).registerWriteHandler(verticalCellStyleStrategy).sheet() + EasyExcel.write(fileVerticalCellStyleStrategy207, StyleData.class).registerWriteHandler( + verticalCellStyleStrategy).sheet() .doWrite(data()); } @Test public void t05LoopMergeStrategy() { - EasyExcel.write(file07, StyleData.class).sheet().registerWriteHandler(new LoopMergeStrategy(2, 1)) + EasyExcel.write(fileLoopMergeStrategy, StyleData.class).sheet().registerWriteHandler( + new LoopMergeStrategy(2, 1)) .doWrite(data10()); } - private void readAndWrite(File file) { + private void readAndWrite(File file) throws Exception { SimpleColumnWidthStyleStrategy simpleColumnWidthStyleStrategy = new SimpleColumnWidthStyleStrategy(50); SimpleRowHeightStyleStrategy simpleRowHeightStyleStrategy = new SimpleRowHeightStyleStrategy((short)40, (short)50); WriteCellStyle headWriteCellStyle = new WriteCellStyle(); - headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); + headWriteCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); WriteFont headWriteFont = new WriteFont(); headWriteFont.setFontHeightInPoints((short)20); + headWriteFont.setColor(IndexedColors.DARK_YELLOW.getIndex()); headWriteCellStyle.setWriteFont(headWriteFont); WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND); - contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex()); + contentWriteCellStyle.setFillForegroundColor(IndexedColors.TEAL.getIndex()); WriteFont contentWriteFont = new WriteFont(); - contentWriteFont.setFontHeightInPoints((short)20); - headWriteCellStyle.setWriteFont(contentWriteFont); + contentWriteFont.setFontHeightInPoints((short)30); + contentWriteFont.setColor(IndexedColors.DARK_TEAL.getIndex()); + contentWriteCellStyle.setWriteFont(contentWriteFont); HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); @@ -186,6 +206,29 @@ public class StyleDataTest { .registerWriteHandler(simpleRowHeightStyleStrategy).registerWriteHandler(horizontalCellStyleStrategy) .registerWriteHandler(onceAbsoluteMergeStrategy).sheet().doWrite(data()); EasyExcel.read(file, StyleData.class, new StyleDataListener()).sheet().doRead(); + + Workbook workbook = WorkbookFactory.create(file); + Sheet sheet = workbook.getSheetAt(0); + Row row0 = sheet.getRow(0); + Cell cell00 = row0.getCell(0); + Assert.assertArrayEquals(new byte[] {-1, -1, 0}, StyleTestUtils.getFillForegroundColor(cell00)); + Assert.assertArrayEquals(new byte[] {-128, -128, 0}, StyleTestUtils.getFontColor(cell00, workbook)); + Assert.assertEquals(20, StyleTestUtils.getFontHeightInPoints(cell00, workbook)); + + Cell cell01 = row0.getCell(1); + Assert.assertArrayEquals(new byte[] {-1, -1, 0}, StyleTestUtils.getFillForegroundColor(cell01)); + Assert.assertArrayEquals(new byte[] {-128, -128, 0}, StyleTestUtils.getFontColor(cell01, workbook)); + Assert.assertEquals(20, StyleTestUtils.getFontHeightInPoints(cell01, workbook)); + + Row row1 = sheet.getRow(1); + Cell cell10 = row1.getCell(0); + Assert.assertArrayEquals(new byte[] {0, -128, -128}, StyleTestUtils.getFillForegroundColor(cell10)); + Assert.assertArrayEquals(new byte[] {0, 51, 102}, StyleTestUtils.getFontColor(cell10, workbook)); + Assert.assertEquals(30, StyleTestUtils.getFontHeightInPoints(cell10, workbook)); + Cell cell11 = row1.getCell(1); + Assert.assertArrayEquals(new byte[] {0, -128, -128}, StyleTestUtils.getFillForegroundColor(cell11)); + Assert.assertArrayEquals(new byte[] {0, 51, 102}, StyleTestUtils.getFontColor(cell11, workbook)); + Assert.assertEquals(30, StyleTestUtils.getFontHeightInPoints(cell11, workbook)); } private List data() {