Search in sources :

Example 1 with LongestMatchColumnWidthStyleStrategy

use of com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy in project RuoYi-Vue-Plus by JavaLionLi.

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异常");
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ExcelBigNumberConvert(com.ruoyi.common.convert.ExcelBigNumberConvert) IOException(java.io.IOException) LongestMatchColumnWidthStyleStrategy(com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy)

Example 2 with LongestMatchColumnWidthStyleStrategy

use of com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy in project RuoYi-Cloud-Plus by JavaLionLi.

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异常");
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ExcelBigNumberConvert(com.ruoyi.common.excel.convert.ExcelBigNumberConvert) IOException(java.io.IOException) LongestMatchColumnWidthStyleStrategy(com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy)

Example 3 with LongestMatchColumnWidthStyleStrategy

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

the class WebUtils method exportExcel.

/**
 * 导出excel文档
 *
 * @param response 当前响应
 * @param clazz    类型
 * @param data     数据
 * @param name     导出文件名
 * @param <T>      导出定义类的类型
 * @throws IOException IO异常
 */
public static <T> void exportExcel(HttpServletResponse response, Class<T> clazz, List<T> data, String name) throws IOException {
    addExportFilenameHeaders(response, name);
    response.setContentType(APPLICATION_VND_MS_EXCEL);
    EasyExcel.write(response.getOutputStream(), clazz).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("sheet1").doWrite(data);
}
Also used : LongestMatchColumnWidthStyleStrategy(com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy)

Example 4 with LongestMatchColumnWidthStyleStrategy

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

the class WebUtils method exportExcel.

/**
 * 动态列导出Excel
 *
 * @param response 响应
 * @param headers  表头
 * @param data     数据
 * @param name     文件 名
 * @throws IOException IO异常
 */
public static void exportExcel(HttpServletResponse response, List<List<String>> headers, List<List<Object>> data, String name) throws IOException {
    addExportFilenameHeaders(response, name + ".xls");
    response.setContentType(APPLICATION_VND_MS_EXCEL);
    EasyExcel.write(response.getOutputStream()).registerConverter(new LocalDateTimeDateConverter()).head(headers).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("sheet1").doWrite(data);
}
Also used : LocalDateTimeDateConverter(com.alibaba.excel.converters.localdatetime.LocalDateTimeDateConverter) LongestMatchColumnWidthStyleStrategy(com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy)

Example 5 with LongestMatchColumnWidthStyleStrategy

use of com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy in project diboot by dibo-software.

the class ExcelHelper method writeData.

/**
 * 简单将数据写入excel文件
 * <p>默认列宽自适应数据长度、写入单元格下拉选项, 可自定义</p>
 *
 * @param filePath
 * @param sheetName
 * @param dataList
 * @param <T>
 * @return
 */
@Deprecated
public static <T extends BaseExcelModel> boolean writeData(String filePath, String sheetName, List<T> dataList, WriteHandler... writeHandlers) throws Exception {
    try {
        if (V.isEmpty(dataList)) {
            return writeDynamicData(filePath, sheetName, Collections.emptyList());
        }
        Class<T> tClass = (Class<T>) dataList.get(0).getClass();
        ExcelWriterBuilder write = EasyExcel.write(filePath, tClass);
        write = write.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy());
        for (WriteHandler handler : writeHandlers) {
            write = write.registerWriteHandler(handler);
        }
        write.sheet(sheetName).doWrite(dataList);
        return true;
    } catch (Exception e) {
        log.error("数据写入excel文件失败", e);
        return false;
    }
}
Also used : ExcelWriterBuilder(com.alibaba.excel.write.builder.ExcelWriterBuilder) WriteHandler(com.alibaba.excel.write.handler.WriteHandler) OptionWriteHandler(com.diboot.file.excel.write.OptionWriteHandler) CommentWriteHandler(com.diboot.file.excel.write.CommentWriteHandler) LongestMatchColumnWidthStyleStrategy(com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy) BusinessException(com.diboot.core.exception.BusinessException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

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