Selaa lähdekoodia

在线excel
1、添加获取临时excel文件接口
2、添加预览接口

LHB 2 viikkoa sitten
vanhempi
commit
c3259f6e2d

+ 13 - 0
blade-service/blade-manager/src/main/java/org/springblade/manager/controller/ExcelTabController.java

@@ -5041,4 +5041,17 @@ public class ExcelTabController extends BladeController {
         return R.status(true);
     }
 
+    /**
+     * 获取清表的的PDF
+     */
+    @PostMapping("/get-excel-pdf")
+    @ApiOperationSupport(order = 74)
+    @ApiOperation(value = "获取清表的的PDF", notes = "获取清表的的PDF")
+    @ApiImplicitParams(value = {
+            @ApiImplicitParam(name = "id", value = "id", required = true),
+            @ApiImplicitParam(name = "文件路径", value = "id", required = true)
+    })
+    public R getExcelPdf(@RequestBody ExcelEditCallback callback) throws Exception {
+        return excelTabService.getExcelPdf(callback);
+    }
 }

+ 2 - 0
blade-service/blade-manager/src/main/java/org/springblade/manager/service/IExcelTabService.java

@@ -229,4 +229,6 @@ public interface IExcelTabService extends BaseService<ExcelTab> {
     R getExcelUrl(ExcelEditCallback callback);
 
     Boolean saveExcel(ExcelEditCallback callback);
+
+    R getExcelPdf(ExcelEditCallback callback) throws Exception;
 }

+ 64 - 0
blade-service/blade-manager/src/main/java/org/springblade/manager/service/impl/ExcelTabServiceImpl.java

@@ -4844,6 +4844,70 @@ public class ExcelTabServiceImpl extends BaseServiceImpl<ExcelTabMapper, ExcelTa
 
     }
 
+    @Override
+    public R getExcelPdf(ExcelEditCallback callback) throws Exception {
+
+        //以id查询清表 生成pdf
+        if(StringUtils.isEmpty(callback.getKey())){
+            return R.fail("没有清表id");
+        }
+
+        String id = callback.getKey();
+
+        String file_path = FileUtils.getSysLocalFileUrl();
+        String pdfPath = file_path + "/pdf//" + id + ".pdf";
+        String excelPath = file_path + "/pdf//" + id + ".xlsx";
+        File tabPdf = ResourceUtil.getFile(pdfPath);
+        if (tabPdf.exists()) {
+            tabPdf.delete();
+        }
+
+        //获取清表信息
+        ExcelTab excelTab = this.getById(id);
+        if (excelTab == null) {
+            return R.fail("未获取到清表信息");
+        }
+
+        //获取excel流 和 html流
+        InputStream exceInp = null;
+        if(StringUtils.isNotEmpty(callback.getUrl())){
+            exceInp = Files.newInputStream(new File(callback.getUrl()).toPath());
+        }else{
+            exceInp = CommonUtil.getOSSInputStream(excelTab.getFileUrl());
+        }
+
+
+
+        Workbook workbook = null;
+        int index = excelTab.getFileUrl().lastIndexOf(".");
+        String suffix = excelTab.getFileUrl().substring(index);
+
+        if (".xls".equalsIgnoreCase(suffix)) {
+            workbook = new XSSFWorkbook(exceInp);
+        } else if (".xlsx".equalsIgnoreCase(suffix)) {
+            workbook = new XSSFWorkbook(exceInp);
+        }
+
+        //输出流
+        FileOutputStream outputStream = new FileOutputStream(excelPath);
+        workbook.write(outputStream);
+        FileUtils.excelToPdf(excelPath, pdfPath);
+
+        //如果有临时文件  先删除临时文件
+        if(StringUtils.isNotEmpty(callback.getUrl())){
+            File file = new File(callback.getUrl());
+            if(file.exists()){
+                file.delete();
+            }
+        }
+        //把pdf上传到oss 以清表id为文件名称,确保一个清表只有一个pdf
+        BladeFile bladeFile = newIOSSClient.uploadFile(callback.getKey() + "_excel.pdf",pdfPath);
+        if(bladeFile != null){
+            return R.data(bladeFile.getLink());
+        }
+        return R.fail("上传oss失败");
+    }
+
 //    @Override
 //    public R getPriWbsPdfByPId(Long pkeyId) throws Exception {
 //        // 1. 获取文件存储路径

+ 49 - 0
blade-service/blade-user/src/main/java/org/springblade/system/user/controller/ExcelTableController.java

@@ -148,7 +148,56 @@ public class ExcelTableController {
 
 
 
+    /**
+     * 在线excel 获取临时文件 用于pdf展示
+     *
+     * @throws IOException
+     */
+    @PostMapping(value = "/getTempExcelFileUrl")
+    @ApiOperation(value = "在线excel 获取临时文件 用于pdf展示", notes = "在线excel 获取临时文件 用于pdf展示")
+    @ApiOperationSupport(order = 35)
+    @ResponseBody
+    public R getTempExcelFileUrl(@RequestBody ExcelEditCallback callback) {
+
+        String file_path = FileUtils.getSysLocalFileUrl();
+        //创建临时json文件
+        String jsonFile = file_path + "/excel/" + callback.getKey() + ".json";
+        //创建临时excel文件
+        String excelFile = file_path + "/excel/temp_" + callback.getKey() + ".xlsx";
 
+        try (FileWriter file = new FileWriter(jsonFile)) {
+
+            String jsonExcel = callback.getJsonExcel();
+            cn.hutool.json.JSONArray objects = JSONUtil.parseArray(jsonExcel);
+            cn.hutool.json.JSONObject entries = new cn.hutool.json.JSONObject();
+            cn.hutool.json.JSONObject info = new cn.hutool.json.JSONObject();
+            info.set("name","");
+            info.set("createdTime", DateTime.now());
+            info.set("modifiedTime",DateTime.now());
+            entries.set("info",info);
+            entries.set("sheets",objects);
+            jsonExcel = entries.toString();
+
+            file.write(jsonExcel);
+            file.flush();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        //获取文件流
+        try {
+            // 将luckysheet json文件转为excel
+            LuckysheetConverter.luckysheetToExcel(jsonFile, excelFile);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        } finally {
+            File file2 = new File(jsonFile);
+            if (file2.exists()) {
+                file2.delete();
+            }
+        }
+
+        return R.data(excelFile);
+    }