use of com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy in project diboot by dibo-software.
the class ExcelHelper method buildWriteSheet.
/**
* 构建WriteSheet
* <p>
* 默认:自列适应宽、单元格下拉选项(验证)写入,批注写入
*
* @param sheetName 可指定sheetName
* @param columnNameList 需要导出的ExcelModel列字段名称列表,为空时导出所有列
* @param consumer
* @param writeHandlers
*/
public static <T> void buildWriteSheet(String sheetName, Collection<String> columnNameList, BiConsumer<CommentWriteHandler, WriteSheet> consumer, WriteHandler... writeHandlers) {
ExcelWriterSheetBuilder writerSheet = EasyExcel.writerSheet().sheetName(sheetName);
CommentWriteHandler commentWriteHandler = new CommentWriteHandler();
writerSheet.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy());
writerSheet.registerWriteHandler(new OptionWriteHandler());
writerSheet.registerWriteHandler(commentWriteHandler);
for (WriteHandler handler : writeHandlers) {
writerSheet.registerWriteHandler(handler);
}
if (V.notEmpty(columnNameList)) {
writerSheet.includeColumnFiledNames(columnNameList);
}
consumer.accept(commentWriteHandler, writerSheet.build());
}
use of com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy in project diboot by dibo-software.
the class ExcelHelper method writeDynamicData.
/**
* 简单将数据写入excel文件
* <p>默认列宽自适应数据长度, 可自定义</p>
*
* @param filePath
* @param sheetName
* @param dataList
* @param writeHandlers
* @return
*/
@Deprecated
public static boolean writeDynamicData(String filePath, String sheetName, List<List<String>> dataList, WriteHandler... writeHandlers) throws Exception {
try {
ExcelWriterBuilder write = EasyExcel.write(filePath);
write = write.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy());
for (WriteHandler handler : writeHandlers) {
write = write.registerWriteHandler(handler);
}
ExcelWriterSheetBuilder sheet = write.sheet(sheetName);
sheet.doWrite(dataList);
return true;
} catch (Exception e) {
log.error("数据写入excel文件失败", e);
return false;
}
}
use of com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy in project ruoyi-vue-pro by YunaiV.
the class ExcelUtils method write.
/**
* 将列表以 Excel 响应给前端
*
* @param response 响应
* @param filename 文件名
* @param sheetName Excel sheet 名
* @param head Excel head 头
* @param data 数据列表哦
* @param <T> 泛型,保证 head 和 data 类型的一致性
* @throws IOException 写入失败的情况
*/
public static <T> void write(HttpServletResponse response, String filename, String sheetName, Class<T> head, List<T> data) throws IOException {
// 输出 Excel
EasyExcel.write(response.getOutputStream(), head).autoCloseStream(// 不要自动关闭,交给 Servlet 自己处理
false).registerWriteHandler(// 基于 column 长度,自动适配。最大 255 宽度
new LongestMatchColumnWidthStyleStrategy()).sheet(sheetName).doWrite(data);
// 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
}
use of com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy in project RuoYi-Flowable-Plus by KonBAI-Q.
the class ExcelUtil method exportExcel.
/**
* 导出excel
*
* @param list 导出数据集合
* @param sheetName 工作表的名称
* @return 结果
*/
public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, HttpServletResponse response) {
try {
String filename = encodingFilename(sheetName);
response.reset();
FileUtils.setAttachmentResponseHeader(response, filename);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
ServletOutputStream os = response.getOutputStream();
EasyExcel.write(os, clazz).autoCloseStream(false).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).registerConverter(new ExcelBigNumberConvert()).sheet(sheetName).doWrite(list);
} catch (IOException e) {
throw new RuntimeException("导出Excel异常");
}
}
use of com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy in project study by bage2014.
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());
}
Aggregations