Search in sources :

Example 1 with SimpleColumnWidthStyleStrategy

use of com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy in project easyexcel by alibaba.

the class StyleDataTest method readAndWrite.

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.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.TEAL.getIndex());
    WriteFont contentWriteFont = new WriteFont();
    contentWriteFont.setFontHeightInPoints((short) 30);
    contentWriteFont.setColor(IndexedColors.DARK_TEAL.getIndex());
    contentWriteCellStyle.setWriteFont(contentWriteFont);
    HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(2, 2, 0, 1);
    EasyExcel.write(file, StyleData.class).registerWriteHandler(simpleColumnWidthStyleStrategy).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);
    Assert.assertEquals(50 * 256, sheet.getColumnWidth(0), 0);
    Row row0 = sheet.getRow(0);
    Assert.assertEquals(800, row0.getHeight(), 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);
    Assert.assertEquals(1000, row1.getHeight(), 0);
    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));
}
Also used : WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) OnceAbsoluteMergeStrategy(com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy) WriteFont(com.alibaba.excel.write.metadata.style.WriteFont) SimpleRowHeightStyleStrategy(com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) SimpleColumnWidthStyleStrategy(com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy) HorizontalCellStyleStrategy(com.alibaba.excel.write.style.HorizontalCellStyleStrategy)

Aggregations

OnceAbsoluteMergeStrategy (com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy)1 WriteCellStyle (com.alibaba.excel.write.metadata.style.WriteCellStyle)1 WriteFont (com.alibaba.excel.write.metadata.style.WriteFont)1 HorizontalCellStyleStrategy (com.alibaba.excel.write.style.HorizontalCellStyleStrategy)1 SimpleColumnWidthStyleStrategy (com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy)1 SimpleRowHeightStyleStrategy (com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy)1 Cell (org.apache.poi.ss.usermodel.Cell)1 Row (org.apache.poi.ss.usermodel.Row)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1