Search in sources :

Example 46 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project springboot-demo by small-rose.

the class ExcelWriteDemo01 method demo03.

@Test
public void demo03() {
    // 写法3
    String fileName = TestFileUtil.getPath() + "simpleWrite" + datetime() + ".xlsx";
    // 这里 需要指定写用哪个class去写
    ExcelWriter excelWriter = null;
    try {
        excelWriter = EasyExcel.write(fileName, DoorData.class).build();
        WriteSheet writeSheet = EasyExcel.writerSheet("sheet1").build();
        excelWriter.write(data(), writeSheet);
    } finally {
        // 千万别忘记finish 会帮忙关闭流
        if (excelWriter != null) {
            excelWriter.finish();
        }
    }
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) Test(org.junit.Test)

Example 47 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project springboot-demo by small-rose.

the class ExcelFacadeService method addColumnInfos.

private void addColumnInfos(String schema, String fileName, List<TableInfo> allTables) {
    if (allTables.isEmpty()) {
        log.info(" allTables size is 0 ");
    }
    int index = 2;
    for (TableInfo tableInfo : allTables) {
        if (index == 4) {
            break;
        }
        ExcelWriter excelWriter = EasyExcel.write(fileName, TableColumn.class).build();
        List<TableColumn> columnInfos = oracleSelectService.getColumnInfo(schema, tableInfo.getTableName());
        WriteSheet writeSheet = EasyExcel.writerSheet(index, tableInfo.getTableName()).head(TableColumn.class).build();
        excelWriter.write(columnInfos, writeSheet);
        excelWriter.finish();
        index++;
    }
    System.out.println("=====执行完成=====");
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) TableInfo(com.xiaocai.demo.excel.oracle.vo.TableInfo) TableColumn(com.xiaocai.demo.excel.oracle.vo.TableColumn)

Example 48 with ExcelWriter

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

the class WriteTest method tableWrite.

/**
 * 使用table去写入
 * <p>
 * 1. 创建excel对应的实体对象 参照{@link DemoData}
 * <p>
 * 2. 然后写入table即可
 */
@Test
public void tableWrite() {
    String fileName = TestFileUtil.getPath() + "tableWrite" + System.currentTimeMillis() + ".xlsx";
    // 这里直接写多个table的案例了,如果只有一个 也可以直一行代码搞定,参照其他案例
    // 这里 需要指定写用哪个class去写
    ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build();
    // 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了
    WriteSheet writeSheet = EasyExcel.writerSheet("模板").needHead(Boolean.FALSE).build();
    // 这里必须指定需要头,table 会继承sheet的配置,sheet配置了不需要,table 默认也是不需要
    WriteTable writeTable0 = EasyExcel.writerTable(0).needHead(Boolean.TRUE).build();
    WriteTable writeTable1 = EasyExcel.writerTable(1).needHead(Boolean.TRUE).build();
    // 第一次写入会创建头
    excelWriter.write(data(), writeSheet, writeTable0);
    // 第二次写如也会创建头,然后在第一次的后面写入数据
    excelWriter.write(data(), writeSheet, writeTable1);
    // / 千万别忘记finish 会帮忙关闭流
    excelWriter.finish();
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) WriteTable(com.alibaba.excel.write.metadata.WriteTable) Test(org.junit.Test)

Example 49 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project Qualitis by WeBankFinTech.

the class RuleMetricServiceImpl method writeExcelToOutput.

private void writeExcelToOutput(List<ExcelRuleMetric> excelRuleMetrics, OutputStream outputStream) throws WriteExcelException, IOException {
    try {
        LOGGER.info("Start to write metric excel");
        ExcelWriter writer = new ExcelWriter(outputStream, ExcelTypeEnum.XLSX, true);
        Sheet templateSheet = new Sheet(1, 0, ExcelRuleMetric.class);
        templateSheet.setSheetName(ExcelSheetName.RULE_METRIC_NAME);
        writer.write(excelRuleMetrics, templateSheet);
        writer.finish();
        LOGGER.info("Finish to write metric excel");
    } catch (Exception e) {
        throw new WriteExcelException(e.getMessage());
    } finally {
        outputStream.close();
    }
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteExcelException(com.webank.wedatasphere.qualitis.rule.exception.WriteExcelException) Sheet(com.alibaba.excel.metadata.Sheet) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) IOException(java.io.IOException) WriteExcelException(com.webank.wedatasphere.qualitis.rule.exception.WriteExcelException)

Example 50 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project excel-spring-boot-starter by pig-mesh.

the class SingleSheetWriteHandler method write.

@Override
public void write(Object obj, HttpServletResponse response, ResponseExcel responseExcel) {
    List<?> list = (List<?>) obj;
    ExcelWriter excelWriter = getExcelWriter(response, responseExcel);
    // 有模板则不指定sheet名
    Class<?> dataClass = list.get(0).getClass();
    WriteSheet sheet = this.sheet(responseExcel.sheets()[0], dataClass, responseExcel.template(), responseExcel.headGenerator());
    excelWriter.write(list, sheet);
    excelWriter.finish();
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) List(java.util.List)

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