Search in sources :

Example 26 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project charon by harvies.

the class ExcelTest method test.

@Test
public void test() {
    List<Head> list = EasyExcelFactory.read(FileUtils.getCurrentUserHomePath() + "/Downloads/图片地址.xlsx").sheet(0).head(Head.class).doReadSync();
    System.err.println(list);
    ExcelWriter excelWriter = EasyExcelFactory.write(FileUtils.getCurrentUserHomePath() + "/Downloads/图片地址_write.xlsx").head(Head.class).build();
    WriteSheet writeSheet = new WriteSheet();
    writeSheet.setSheetNo(0);
    writeSheet.setSheetName("工作表1");
    excelWriter.write(list, writeSheet);
    excelWriter.finish();
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) Test(org.junit.jupiter.api.Test)

Example 27 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project springboot by lbshold.

the class CoordinateExcelController method template.

@ApiOperation("Excel导入模板下载")
@GetMapping("/not-auth/template")
public void template(HttpServletResponse response) throws Exception {
    List<MyCoordinate> result = new ArrayList<>();
    response.setCharacterEncoding("UTF-8");
    String name = URLEncoder.encode("坐标转换模板.xlsx", "UTF-8");
    response.setContentType("application/x-msdownload");
    response.addHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + name);
    try (OutputStream out = response.getOutputStream()) {
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
        Sheet sheet1 = new Sheet(1, 0, MyCoordinate2.class);
        sheet1.setSheetName("坐标转换模板");
        writer.write(result, sheet1);
        writer.finish();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : MyCoordinate(com.summit.coordinates.entity.MyCoordinate) OutputStream(java.io.OutputStream) ExcelWriter(com.alibaba.excel.ExcelWriter) ArrayList(java.util.ArrayList) Sheet(com.alibaba.excel.metadata.Sheet) IOException(java.io.IOException) GetMapping(org.springframework.web.bind.annotation.GetMapping) ApiOperation(io.swagger.annotations.ApiOperation)

Example 28 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project springboot by lbshold.

the class CoordinateExcelController method downLoad.

@ApiOperation(value = "转换后的坐标Excel导出", notes = "Excel批量导入坐标后,复制该请求链接浏览器访问下载Excel(转换后的坐标),转换失败Excel会有相应提示")
@GetMapping("/not-auth/export")
public void downLoad(HttpServletResponse response) throws Exception {
    Cache cache = cacheManager.getCache("file");
    Cache.ValueWrapper cacheValue = cache.get("result");
    if (cacheValue != null) {
        List<MyCoordinate> result = (List<MyCoordinate>) cacheValue.get();
        // 写入excel
        response.setCharacterEncoding("UTF-8");
        String name = URLEncoder.encode("转换后的Gcj02坐标.xlsx", "UTF-8");
        response.setContentType("application/x-msdownload");
        response.addHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + name);
        try (OutputStream out = response.getOutputStream()) {
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
            Sheet sheet1 = new Sheet(1, 0, MyCoordinate.class);
            sheet1.setSheetName("转换后的Gcj02坐标");
            writer.write(result, sheet1);
            writer.finish();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
    cache.clear();
}
Also used : MyCoordinate(com.summit.coordinates.entity.MyCoordinate) OutputStream(java.io.OutputStream) ExcelWriter(com.alibaba.excel.ExcelWriter) ArrayList(java.util.ArrayList) List(java.util.List) Sheet(com.alibaba.excel.metadata.Sheet) IOException(java.io.IOException) Cache(org.springframework.cache.Cache) GetMapping(org.springframework.web.bind.annotation.GetMapping) ApiOperation(io.swagger.annotations.ApiOperation)

Example 29 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project study by bage2014.

the class WriteTest method repeatedWrite.

/**
 * 重复多次写入
 * <p>
 * 1. 创建excel对应的实体对象 参照{@link ComplexHeadData}
 * <p>
 * 2. 使用{@link ExcelProperty}注解指定复杂的头
 * <p>
 * 3. 直接调用二次写入即可
 */
@Test
public void repeatedWrite() {
    // 方法1 如果写到同一个sheet
    String fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
    // 这里 需要指定写用哪个class去写
    ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build();
    // 这里注意 如果同一个sheet只要创建一次
    WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
    // 去调用写入,这里我调用了五次,实际使用时根据数据库分页的总的页数来
    for (int i = 0; i < 5; i++) {
        // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
        List<DemoData> data = data();
        excelWriter.write(data, writeSheet);
    }
    // / 千万别忘记finish 会帮忙关闭流
    excelWriter.finish();
    // 方法2 如果写到不同的sheet 同一个对象
    fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
    // 这里 指定文件
    excelWriter = EasyExcel.write(fileName, DemoData.class).build();
    // 去调用写入,这里我调用了五次,实际使用时根据数据库分页的总的页数来。这里最终会写到5个sheet里面
    for (int i = 0; i < 5; i++) {
        // 每次都要创建writeSheet 这里注意必须指定sheetNo
        writeSheet = EasyExcel.writerSheet(i, "模板").build();
        // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
        List<DemoData> data = data();
        excelWriter.write(data, writeSheet);
    }
    // / 千万别忘记finish 会帮忙关闭流
    excelWriter.finish();
    // 方法3 如果写到不同的sheet 不同的对象
    fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
    // 这里 指定文件
    excelWriter = EasyExcel.write(fileName).build();
    // 去调用写入,这里我调用了五次,实际使用时根据数据库分页的总的页数来。这里最终会写到5个sheet里面
    for (int i = 0; i < 5; i++) {
        // 每次都要创建writeSheet 这里注意必须指定sheetNo。这里注意DemoData.class 可以每次都变,我这里为了方便 所以用的同一个class 实际上可以一直变
        writeSheet = EasyExcel.writerSheet(i, "模板").head(DemoData.class).build();
        // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
        List<DemoData> data = data();
        excelWriter.write(data, writeSheet);
    }
    // / 千万别忘记finish 会帮忙关闭流
    excelWriter.finish();
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) Test(org.junit.Test)

Example 30 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project study by bage2014.

the class WriteTest method simpleWrite.

/**
 * 最简单的写
 * <p>
 * 1. 创建excel对应的实体对象 参照{@link DemoData}
 * <p>
 * 2. 直接写即可
 */
@Test
public void simpleWrite() {
    // 写法1
    String fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
    // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
    // 如果这里想使用03 则 传入excelType参数即可
    EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data());
    // 写法2
    fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
    // 这里 需要指定写用哪个class去写
    ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build();
    WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
    excelWriter.write(data(), writeSheet);
    // / 千万别忘记finish 会帮忙关闭流
    excelWriter.finish();
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) Test(org.junit.Test)

Aggregations

ExcelWriter (com.alibaba.excel.ExcelWriter)67 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)52 Test (org.junit.Test)31 Sheet (com.alibaba.excel.metadata.Sheet)13 HashMap (java.util.HashMap)13 FillConfig (com.alibaba.excel.write.metadata.fill.FillConfig)11 List (java.util.List)9 FileOutputStream (java.io.FileOutputStream)7 IOException (java.io.IOException)7 WriteTable (com.alibaba.excel.write.metadata.WriteTable)6 ArrayList (java.util.ArrayList)6 ExcelReader (com.alibaba.excel.ExcelReader)5 ReadSheet (com.alibaba.excel.read.metadata.ReadSheet)5 FillWrapper (com.alibaba.excel.write.metadata.fill.FillWrapper)4 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)4 OutputStream (java.io.OutputStream)4 Map (java.util.Map)4 LargeDataTest (com.alibaba.easyexcel.test.core.large.LargeDataTest)3 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)3 WriteExcelException (com.webank.wedatasphere.qualitis.rule.exception.WriteExcelException)3