Search in sources :

Example 11 with LongestMatchColumnWidthStyleStrategy

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

the class WriteTest method longestMatchColumnWidthWrite.

/**
 * 自动列宽(不太精确)
 * <p>
 * 这个目前不是很好用,比如有数字就会导致换行。而且长度也不是刚好和实际长度一致。 所以需要精确到刚好列宽的慎用。 当然也可以自己参照 {@link LongestMatchColumnWidthStyleStrategy}
 * 重新实现.
 * <p>
 * poi 自带{@link SXSSFSheet#autoSizeColumn(int)} 对中文支持也不太好。目前没找到很好的算法。 有的话可以推荐下。
 *
 * <p>
 * 1. 创建excel对应的实体对象 参照{@link LongestMatchColumnWidthData}
 * <p>
 * 2. 注册策略{@link LongestMatchColumnWidthStyleStrategy}
 * <p>
 * 3. 直接写即可
 */
@Test
public void longestMatchColumnWidthWrite() {
    String fileName = TestFileUtil.getPath() + "longestMatchColumnWidthWrite" + System.currentTimeMillis() + ".xlsx";
    // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
    EasyExcel.write(fileName, LongestMatchColumnWidthData.class).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("模板").doWrite(dataLong());
}
Also used : LongestMatchColumnWidthStyleStrategy(com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy) Test(org.junit.Test)

Aggregations

LongestMatchColumnWidthStyleStrategy (com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy)11 WriteHandler (com.alibaba.excel.write.handler.WriteHandler)3 CommentWriteHandler (com.diboot.file.excel.write.CommentWriteHandler)3 OptionWriteHandler (com.diboot.file.excel.write.OptionWriteHandler)3 IOException (java.io.IOException)3 ServletOutputStream (javax.servlet.ServletOutputStream)3 ExcelWriterBuilder (com.alibaba.excel.write.builder.ExcelWriterBuilder)2 ExcelWriterSheetBuilder (com.alibaba.excel.write.builder.ExcelWriterSheetBuilder)2 BusinessException (com.diboot.core.exception.BusinessException)2 ExcelBigNumberConvert (com.ruoyi.common.convert.ExcelBigNumberConvert)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Test (org.junit.Test)2 LocalDateTimeDateConverter (com.alibaba.excel.converters.localdatetime.LocalDateTimeDateConverter)1 ExcelBigNumberConvert (com.ruoyi.common.excel.convert.ExcelBigNumberConvert)1