Search in sources :

Example 11 with WriteCellStyle

use of com.alibaba.excel.write.metadata.style.WriteCellStyle in project mybatch by mywill26.

the class GeneralExcelWriter method getStrategy.

public static HorizontalCellStyleStrategy getStrategy() {
    WriteCellStyle cellStyle = new WriteCellStyle();
    cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
    WriteFont writeFont = new WriteFont();
    writeFont.setFontHeightInPoints((short) 10);
    writeFont.setBold(true);
    cellStyle.setWriteFont(writeFont);
    return new HorizontalCellStyleStrategy(cellStyle, new WriteCellStyle());
}
Also used : WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) WriteFont(com.alibaba.excel.write.metadata.style.WriteFont) HorizontalCellStyleStrategy(com.alibaba.excel.write.style.HorizontalCellStyleStrategy)

Example 12 with WriteCellStyle

use of com.alibaba.excel.write.metadata.style.WriteCellStyle in project personManage by WHUTdk.

the class EasyexcelUtil method setDefaultCellStyle.

private static HorizontalCellStyleStrategy setDefaultCellStyle() {
    // 头的策略
    WriteCellStyle headWriteCellStyle = new WriteCellStyle();
    // 背景设置为红色
    headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
    WriteFont headWriteFont = new WriteFont();
    headWriteFont.setFontHeightInPoints((short) 12);
    headWriteCellStyle.setWriteFont(headWriteFont);
    // 内容的策略
    WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
    HSSFDataFormat dataFormat = new HSSFWorkbook().createDataFormat();
    // 本文格式
    contentWriteCellStyle.setDataFormat(dataFormat.getFormat("@"));
    contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
    contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    // 填充类型 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
    // contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
    // 背景绿色
    // contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    WriteFont contentWriteFont = new WriteFont();
    // 字体大小
    contentWriteFont.setFontHeightInPoints((short) 12);
    contentWriteCellStyle.setWriteFont(contentWriteFont);
    // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
    return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
}
Also used : WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) WriteFont(com.alibaba.excel.write.metadata.style.WriteFont) HSSFDataFormat(org.apache.poi.hssf.usermodel.HSSFDataFormat) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HorizontalCellStyleStrategy(com.alibaba.excel.write.style.HorizontalCellStyleStrategy)

Example 13 with WriteCellStyle

use of com.alibaba.excel.write.metadata.style.WriteCellStyle in project metersphere by metersphere.

the class EasyExcelExporter method export.

public void export(HttpServletResponse response, List data, String fileName, String sheetName) {
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("utf-8");
    WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
    contentWriteCellStyle.setWrapped(true);
    try {
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(null, contentWriteCellStyle);
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
        EasyExcel.write(response.getOutputStream(), this.clazz).registerWriteHandler(horizontalCellStyleStrategy).sheet(sheetName).doWrite(data);
    } catch (UnsupportedEncodingException e) {
        LogUtil.error(e.getMessage(), e);
        throw new ExcelException("Utf-8 encoding is not supported");
    } catch (IOException e) {
        LogUtil.error(e.getMessage(), e);
        throw new ExcelException("IO exception");
    }
}
Also used : WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) ExcelException(io.metersphere.exception.ExcelException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) HorizontalCellStyleStrategy(com.alibaba.excel.write.style.HorizontalCellStyleStrategy)

Example 14 with WriteCellStyle

use of com.alibaba.excel.write.metadata.style.WriteCellStyle in project easyexcel by alibaba.

the class WriteLargeTest method test.

@Test
public void test() throws Exception {
    // 方法2 如果写到不同的sheet 同一个对象
    String fileName = TestFileUtil.getPath() + "large" + System.currentTimeMillis() + ".xlsx";
    // 头的策略
    WriteCellStyle headWriteCellStyle = new WriteCellStyle();
    // 背景设置为红色
    headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
    WriteFont headWriteFont = new WriteFont();
    headWriteFont.setFontHeightInPoints((short) 20);
    headWriteCellStyle.setWriteFont(headWriteFont);
    // 内容的策略
    WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
    // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
    contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
    // 背景绿色
    contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    WriteFont contentWriteFont = new WriteFont();
    // 字体大小
    contentWriteFont.setFontHeightInPoints((short) 20);
    contentWriteCellStyle.setWriteFont(contentWriteFont);
    // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
    HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    ExcelWriter excelWriter = EasyExcel.write(fileName, LargeData.class).registerWriteHandler(horizontalCellStyleStrategy).build();
    WriteSheet writeSheet = EasyExcel.writerSheet().build();
    for (int j = 0; j < 100; j++) {
        excelWriter.write(data(), writeSheet);
        LOGGER.info("{} fill success.", j);
    }
    excelWriter.finish();
}
Also used : WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) WriteFont(com.alibaba.excel.write.metadata.style.WriteFont) ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) HorizontalCellStyleStrategy(com.alibaba.excel.write.style.HorizontalCellStyleStrategy) Test(org.junit.Test)

Example 15 with WriteCellStyle

use of com.alibaba.excel.write.metadata.style.WriteCellStyle in project easyexcel by alibaba.

the class Lock2Test method write.

@Test
public void write() throws Exception {
    String fileName = TestFileUtil.getPath() + "styleWrite" + System.currentTimeMillis() + ".xlsx";
    // 头的策略
    WriteCellStyle headWriteCellStyle = new WriteCellStyle();
    // 背景设置为红色
    headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
    WriteFont headWriteFont = new WriteFont();
    headWriteFont.setFontHeightInPoints((short) 20);
    headWriteCellStyle.setWriteFont(headWriteFont);
    // 内容的策略
    WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
    // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
    contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
    // 背景绿色
    contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    WriteFont contentWriteFont = new WriteFont();
    // 字体大小
    contentWriteFont.setFontHeightInPoints((short) 20);
    contentWriteCellStyle.setWriteFont(contentWriteFont);
    // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
    HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
    EasyExcel.write(fileName, DemoData.class).registerWriteHandler(horizontalCellStyleStrategy).sheet("模板").doWrite(data());
}
Also used : WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) WriteFont(com.alibaba.excel.write.metadata.style.WriteFont) HorizontalCellStyleStrategy(com.alibaba.excel.write.style.HorizontalCellStyleStrategy) DemoData(com.alibaba.easyexcel.test.demo.write.DemoData) Test(org.junit.Test)

Aggregations

WriteCellStyle (com.alibaba.excel.write.metadata.style.WriteCellStyle)18 WriteFont (com.alibaba.excel.write.metadata.style.WriteFont)14 HorizontalCellStyleStrategy (com.alibaba.excel.write.style.HorizontalCellStyleStrategy)10 Test (org.junit.Test)8 Head (com.alibaba.excel.metadata.Head)4 AbstractVerticalCellStyleStrategy (com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy)4 CellStyle (org.apache.poi.ss.usermodel.CellStyle)4 CellWriteHandlerContext (com.alibaba.excel.write.handler.context.CellWriteHandlerContext)3 ExcelWriter (com.alibaba.excel.ExcelWriter)2 WriteCellData (com.alibaba.excel.metadata.data.WriteCellData)2 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)2 ArrayList (java.util.ArrayList)2 Cell (org.apache.poi.ss.usermodel.Cell)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2 DemoData (com.alibaba.easyexcel.test.demo.write.DemoData)1 HeadFontStyle (com.alibaba.excel.annotation.write.style.HeadFontStyle)1 HeadStyle (com.alibaba.excel.annotation.write.style.HeadStyle)1 CommentData (com.alibaba.excel.metadata.data.CommentData)1 DataFormatData (com.alibaba.excel.metadata.data.DataFormatData)1 FormulaData (com.alibaba.excel.metadata.data.FormulaData)1