Search in sources :

Example 31 with ExcelWriter

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

the class RuleBatchServiceImpl method writeExcelToOutput.

private void writeExcelToOutput(List<ExcelTemplateRuleByProject> templateRules, List<ExcelCustomRuleByProject> customRules, List<ExcelMultiTemplateRuleByProject> multiTemplateRules, List<ExcelTemplateFileRuleByProject> templateFileRules, OutputStream outputStream) throws WriteExcelException, IOException {
    try {
        LOGGER.info("Start to write excel");
        ExcelWriter writer = new ExcelWriter(outputStream, ExcelTypeEnum.XLSX, true);
        Sheet templateSheet = new Sheet(1, 0, ExcelTemplateRuleByProject.class);
        templateSheet.setSheetName(ExcelSheetName.TEMPLATE_RULE_NAME);
        writer.write(templateRules, templateSheet);
        Sheet customSheet = new Sheet(2, 0, ExcelCustomRuleByProject.class);
        customSheet.setSheetName(ExcelSheetName.CUSTOM_RULE_NAME);
        writer.write(customRules, customSheet);
        Sheet multiTemplateSheet = new Sheet(3, 0, ExcelMultiTemplateRuleByProject.class);
        multiTemplateSheet.setSheetName(ExcelSheetName.MULTI_TEMPLATE_RULE_NAME);
        writer.write(multiTemplateRules, multiTemplateSheet);
        Sheet templateFileSheet = new Sheet(4, 0, ExcelTemplateFileRuleByProject.class);
        templateFileSheet.setSheetName(ExcelSheetName.TEMPLATE_FILE_RULE_NAME);
        writer.write(templateFileRules, templateFileSheet);
        writer.finish();
        LOGGER.info("Finish to write excel");
    } catch (Exception e) {
        throw new WriteExcelException(e.getMessage(), 500);
    } finally {
        outputStream.close();
    }
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteExcelException(com.webank.wedatasphere.qualitis.rule.exception.WriteExcelException) Sheet(com.alibaba.excel.metadata.Sheet) ClusterInfoNotConfigException(com.webank.wedatasphere.qualitis.exception.ClusterInfoNotConfigException) IOException(java.io.IOException) WriteExcelException(com.webank.wedatasphere.qualitis.rule.exception.WriteExcelException) TaskNotExistException(com.webank.wedatasphere.qualitis.exception.TaskNotExistException) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) ParseException(org.apache.hadoop.hive.ql.parse.ParseException) MetaDataAcquireFailedException(com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)

Example 32 with ExcelWriter

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

the class ProjectBatchServiceImpl method writeExcelToOutput.

private void writeExcelToOutput(List<ExcelProject> excelProjects, List<ExcelTemplateRuleByProject> excelTemplateRuleByProjects, List<ExcelCustomRuleByProject> excelCustomRuleByProjects, List<ExcelMultiTemplateRuleByProject> excelMultiTemplateRuleByProjects, List<ExcelTemplateFileRuleByProject> excelTemplateFileRuleByProject, OutputStream outputStream) throws WriteExcelException, IOException {
    try {
        LOGGER.info("Start to write excel");
        ExcelWriter writer = new ExcelWriter(outputStream, ExcelTypeEnum.XLSX, true);
        Sheet templateRuleSheet = new Sheet(1, 0, ExcelTemplateRuleByProject.class);
        templateRuleSheet.setSheetName(ExcelSheetName.TEMPLATE_RULE_NAME);
        writer.write(excelTemplateRuleByProjects, templateRuleSheet);
        Sheet projectSheet = new Sheet(2, 0, ExcelProject.class);
        projectSheet.setSheetName(ExcelSheetName.PROJECT_NAME);
        writer.write(excelProjects, projectSheet);
        Sheet customRuleSheet = new Sheet(3, 0, ExcelCustomRuleByProject.class);
        customRuleSheet.setSheetName(ExcelSheetName.CUSTOM_RULE_NAME);
        writer.write(excelCustomRuleByProjects, customRuleSheet);
        Sheet multiRuleSheet = new Sheet(4, 0, ExcelMultiTemplateRuleByProject.class);
        multiRuleSheet.setSheetName(ExcelSheetName.MULTI_TEMPLATE_RULE_NAME);
        writer.write(excelMultiTemplateRuleByProjects, multiRuleSheet);
        Sheet templateFileSheet = new Sheet(5, 0, ExcelTemplateFileRuleByProject.class);
        templateFileSheet.setSheetName(ExcelSheetName.TEMPLATE_FILE_RULE_NAME);
        writer.write(excelTemplateFileRuleByProject, templateFileSheet);
        writer.finish();
        LOGGER.info("Finish to write 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) ZipException(net.lingala.zip4j.exception.ZipException) RoleNotFoundException(javax.management.relation.RoleNotFoundException) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) IOException(java.io.IOException) WriteExcelException(com.webank.wedatasphere.qualitis.rule.exception.WriteExcelException)

Example 33 with ExcelWriter

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

the class ApplicationServiceImpl method writeExcelFile.

private void writeExcelFile(File tmpFile, List<String> tables, UploadResultRequest request) throws UnExpectedRequestException {
    int sheetNo = 1;
    OutputStream tmpOutputStream = null;
    try {
        Path parentPath = Paths.get(tmpFile.getParent());
        if (Files.notExists(parentPath)) {
            tmpFile.getParentFile().mkdirs();
        }
        if (!tmpFile.exists()) {
            LOGGER.info("Start to create local file.");
            tmpFile.createNewFile();
        }
        tmpOutputStream = new FileOutputStream(tmpFile);
        ExcelWriter writer = new ExcelWriter(tmpOutputStream, ExcelTypeEnum.XLSX, true);
        // Find task with the start time and end time.
        for (String tableName : tables) {
            List<Task> tasks = taskDao.findWithSubmitTimeAndDatasource(request.getStartTime(), request.getEndTime(), request.getClusterName(), request.getDatabaseName(), tableName);
            List<TaskRuleSimple> taskRuleSimples = tasks.stream().map(Task::getTaskRuleSimples).flatMap(taskRuleSimpleSet -> taskRuleSimpleSet.stream()).distinct().collect(Collectors.toList());
            // Generate analysis result excel
            List<ExcelResult> results = new ArrayList<>(taskRuleSimples.size());
            for (TaskRuleSimple taskRuleSimple : taskRuleSimples) {
                ExcelResult excelResult = new ExcelResult();
                excelResult.setRuleName(taskRuleSimple.getRuleName());
                excelResult.setClusterName(request.getClusterName());
                excelResult.setDatabaseName(request.getDatabaseName());
                excelResult.setTableName(tableName);
                excelResult.setBeginTime(taskRuleSimple.getTask().getBeginTime());
                excelResult.setEndTime(taskRuleSimple.getTask().getEndTime());
                StringBuffer checkTemplateStr = new StringBuffer();
                StringBuffer resultStr = new StringBuffer();
                joinAlarmConfig(results, taskRuleSimple, excelResult, request, checkTemplateStr, resultStr);
                results.add(excelResult);
            }
            Sheet templateSheet = new Sheet(sheetNo++, 0, ExcelResult.class);
            templateSheet.setSheetName(tableName + "-" + ExcelSheetName.ANALYSIS_NAME);
            writer.write(results, templateSheet);
        }
        writer.finish();
    } catch (FileNotFoundException e) {
        LOGGER.error(e.getMessage(), e);
        throw new UnExpectedRequestException("{&FAILED_TO_CREATE_LOCAL_FILE}");
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
        throw new UnExpectedRequestException("{&FAILED_TO_CREATE_LOCAL_FILE}");
    } finally {
        try {
            tmpOutputStream.close();
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
}
Also used : Path(java.nio.file.Path) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) Task(com.webank.wedatasphere.qualitis.entity.Task) ExcelResult(com.webank.wedatasphere.qualitis.excel.ExcelResult) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ExcelWriter(com.alibaba.excel.ExcelWriter) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) TaskRuleSimple(com.webank.wedatasphere.qualitis.entity.TaskRuleSimple) Sheet(com.alibaba.excel.metadata.Sheet)

Example 34 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project ballcat by ballcat-projects.

the class ManySheetWriteHandler method write.

@Override
public void write(Object obj, HttpServletResponse response, ResponseExcel responseExcel) {
    List<?> objList = (List<?>) obj;
    ExcelWriter excelWriter = getExcelWriter(response, responseExcel);
    Sheet[] sheets = responseExcel.sheets();
    WriteSheet sheet;
    for (int i = 0; i < sheets.length; i++) {
        List<?> eleList = (List<?>) objList.get(i);
        Class<?> dataClass = eleList.get(0).getClass();
        // 创建sheet
        sheet = this.sheet(sheets[i], dataClass, responseExcel.template(), responseExcel.headGenerator());
        // 写入sheet
        excelWriter.write(eleList, sheet);
    }
    excelWriter.finish();
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) List(java.util.List) Sheet(com.hccake.common.excel.annotation.Sheet) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet)

Example 35 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project ballcat by ballcat-projects.

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