Search in sources :

Example 1 with EasyExcelGenerator

use of com.rebuild.core.service.datareport.EasyExcelGenerator in project rebuild by getrebuild.

the class ReportTemplateController method preview.

@GetMapping("/report-templates/preview")
public void preview(@IdParam ID reportId, HttpServletResponse response) throws IOException {
    Object[] report = Application.createQueryNoFilter("select belongEntity,templateType from DataReportConfig where configId = ?").setParameter(1, reportId).unique();
    Entity entity = MetadataHelper.getEntity((String) report[0]);
    String sql = String.format("select %s from %s order by modifiedOn desc", entity.getPrimaryField().getName(), entity.getName());
    Object[] random = Application.createQueryNoFilter(sql).unique();
    if (random == null) {
        response.sendError(400, Language.L("未找到可供预览的记录"));
        return;
    }
    File file;
    try {
        // 列表报表
        if (ObjectUtils.toInt(report[1]) == DataReportManager.TYPE_LIST) {
            JSONObject queryData = JSONUtils.toJSONObject(new String[] { "pageSize", "entity" }, new Object[] { 2, report[0] });
            file = new EasyExcelListGenerator(reportId, queryData).generate();
        } else {
            file = new EasyExcelGenerator(reportId, (ID) random[0]).generate();
        }
    } catch (ConfigurationException ex) {
        response.sendError(500, ex.getLocalizedMessage());
        return;
    }
    FileDownloader.downloadTempFile(response, file, null);
}
Also used : Entity(cn.devezhao.persist4j.Entity) JSONObject(com.alibaba.fastjson.JSONObject) ConfigurationException(com.rebuild.core.configuration.ConfigurationException) JSONObject(com.alibaba.fastjson.JSONObject) EasyExcelListGenerator(com.rebuild.core.service.datareport.EasyExcelListGenerator) File(java.io.File) EasyExcelGenerator(com.rebuild.core.service.datareport.EasyExcelGenerator) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with EasyExcelGenerator

use of com.rebuild.core.service.datareport.EasyExcelGenerator in project rebuild by getrebuild.

the class ReportsController method reportGenerate.

@RequestMapping({ "report/generate", "report/export" })
public void reportGenerate(@PathVariable String entity, @IdParam(name = "report") ID reportId, @IdParam(name = "record") ID recordId, HttpServletRequest request, HttpServletResponse response) throws IOException {
    File file = new EasyExcelGenerator(reportId, recordId).generate();
    String fileName = getReportName(entity, reportId, file);
    if (ServletUtils.isAjaxRequest(request)) {
        JSON data = JSONUtils.toJSONObject(new String[] { "fileKey", "fileName" }, new Object[] { file.getName(), fileName });
        writeSuccess(response, data);
    } else {
        FileDownloader.downloadTempFile(response, file, fileName);
    }
}
Also used : JSON(com.alibaba.fastjson.JSON) File(java.io.File) EasyExcelGenerator(com.rebuild.core.service.datareport.EasyExcelGenerator) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with EasyExcelGenerator

use of com.rebuild.core.service.datareport.EasyExcelGenerator in project rebuild by getrebuild.

the class ReportTemplateController method preview.

@GetMapping("/report-templates/preview")
public void preview(@IdParam ID reportId, HttpServletRequest request, HttpServletResponse response) throws IOException {
    Object[] report = Application.createQueryNoFilter("select belongEntity from DataReportConfig where configId = ?").setParameter(1, reportId).unique();
    Entity entity = MetadataHelper.getEntity((String) report[0]);
    String sql = String.format("select %s from %s order by modifiedOn desc", entity.getPrimaryField().getName(), entity.getName());
    Object[] random = Application.createQueryNoFilter(sql).unique();
    if (random == null) {
        response.sendError(400, Language.L("未找到可供预览的记录"));
        return;
    }
    File file;
    try {
        File template = DataReportManager.instance.getTemplateFile(entity, reportId);
        file = new EasyExcelGenerator(template, (ID) random[0]).generate();
    } catch (ConfigurationException ex) {
        response.sendError(400, Language.L("未找到可供预览的记录"));
        return;
    }
    FileDownloader.setDownloadHeaders(request, response, file.getName());
    FileDownloader.writeLocalFile(file, response);
}
Also used : Entity(cn.devezhao.persist4j.Entity) ConfigurationException(com.rebuild.core.configuration.ConfigurationException) File(java.io.File) EasyExcelGenerator(com.rebuild.core.service.datareport.EasyExcelGenerator) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

EasyExcelGenerator (com.rebuild.core.service.datareport.EasyExcelGenerator)3 File (java.io.File)3 Entity (cn.devezhao.persist4j.Entity)2 ConfigurationException (com.rebuild.core.configuration.ConfigurationException)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 JSON (com.alibaba.fastjson.JSON)1 JSONObject (com.alibaba.fastjson.JSONObject)1 EasyExcelListGenerator (com.rebuild.core.service.datareport.EasyExcelListGenerator)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1