Search in sources :

Example 1 with OnceAbsoluteMergeStrategy

use of com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy in project easyexcel by alibaba.

the class AbstractWriteHolder method dealOnceAbsoluteMerge.

private void dealOnceAbsoluteMerge(List<WriteHandler> handlerList) {
    OnceAbsoluteMergeProperty onceAbsoluteMergeProperty = getExcelWriteHeadProperty().getOnceAbsoluteMergeProperty();
    if (onceAbsoluteMergeProperty == null) {
        return;
    }
    handlerList.add(new OnceAbsoluteMergeStrategy(onceAbsoluteMergeProperty));
}
Also used : OnceAbsoluteMergeProperty(com.alibaba.excel.metadata.property.OnceAbsoluteMergeProperty) OnceAbsoluteMergeStrategy(com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy)

Example 2 with OnceAbsoluteMergeStrategy

use of com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy in project easyexcel by alibaba.

the class FillTempTest method complexFill.

/**
 * 复杂的填充
 *
 * @since 2.1.1
 */
@Test
public void complexFill() {
    // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
    // {} 代表普通变量 {.} 代表是list的变量
    OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(2, 2, 0, 1);
    String fileName = TestFileUtil.getPath() + "complexFill" + System.currentTimeMillis() + ".xlsx";
    ExcelWriter excelWriter = EasyExcel.write(fileName).registerWriteHandler(onceAbsoluteMergeStrategy).withTemplate(TestFileUtil.readUserHomeFile("test/simple.xlsx")).build();
    WriteSheet writeSheet0 = EasyExcel.writerSheet(0).build();
    WriteSheet writeSheet1 = EasyExcel.writerSheet(1).build();
    excelWriter.fill(teamp(), writeSheet0);
    excelWriter.fill(teamp(), writeSheet1);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("date", "2019年10月9日13:28:28");
    map.put("total", 1000);
    excelWriter.fill(map, writeSheet0);
    excelWriter.finish();
}
Also used : OnceAbsoluteMergeStrategy(com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy) HashMap(java.util.HashMap) ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) Test(org.junit.Test)

Example 3 with OnceAbsoluteMergeStrategy

use of com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy 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)3 ExcelWriter (com.alibaba.excel.ExcelWriter)1 OnceAbsoluteMergeProperty (com.alibaba.excel.metadata.property.OnceAbsoluteMergeProperty)1 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)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 HashMap (java.util.HashMap)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 Test (org.junit.Test)1