Kaynağa Gözat

在线excel
1、改为luckysheet实现excel在线编辑
2、添加获取excel转json的字符串接口
2、添加excel保存接口

LHB 4 hafta önce
ebeveyn
işleme
f4b1ab4578

+ 4 - 0
blade-service-api/blade-manager-api/src/main/java/org/springblade/manager/entity/ExcelEditCallback.java

@@ -1,6 +1,7 @@
 package org.springblade.manager.entity;
 
 
+import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 
 @Data
@@ -22,4 +23,7 @@ public class ExcelEditCallback {
 
     private Long projectId;
 
+    @ApiModelProperty("excel转的JSON格式字符串")
+    private String jsonExcel;
+
 }

+ 7 - 0
blade-service/blade-manager/pom.xml

@@ -178,6 +178,13 @@
             <scope>system</scope>
             <systemPath>${basedir}/../../doc/lib/eVisaLib/spire.office.free-5.3.1.jar</systemPath>
         </dependency>
+        <dependency>
+            <groupId>io.github.autoffice</groupId>
+            <artifactId>luckysheet-lib</artifactId>
+            <version>1.1.0</version>
+            <scope>system</scope>
+            <systemPath>${basedir}/../../doc/lib/excelLib/luckysheet-lib-1.1.0.jar</systemPath>
+        </dependency>
 
         <!--计量-->
         <dependency>

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

@@ -4987,4 +4987,30 @@ public class ExcelTabController extends BladeController {
         System.out.println("检查完毕");
         return R.status(true);
     }
+
+    /**
+     * 在线excel 获取文件json数据
+     *
+     * @throws IOException
+     */
+    @PostMapping(value = "/getExcelUrl")
+    @ApiOperation(value = "获取文件json数据", notes = "获取文件json数据")
+    @ApiOperationSupport(order = 35)
+    @ResponseBody
+    public R getExcelUrl(@RequestBody ExcelEditCallback callback) {
+        return excelTabService.getExcelUrl(callback);
+    }
+
+    /**
+     * 在线excel 保存
+     *
+     * @throws IOException
+     */
+    @PostMapping(value = "/saveExcel")
+    @ApiOperation(value = "在线excel 保存", notes = "在线excel 保存")
+    @ApiOperationSupport(order = 35)
+    @ResponseBody
+    public R saveExcel(@RequestBody ExcelEditCallback callback) {
+        return excelTabService.saveExcel(callback);
+    }
 }

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

@@ -222,4 +222,8 @@ public interface IExcelTabService extends BaseService<ExcelTab> {
     void expailHtmlInfo(String thmlUrl, Long id, String s) throws Exception ;
 
     R batchAddNumbers(BatchAddNumbersDTO dto) throws Exception;
+
+    R getExcelUrl(ExcelEditCallback callback);
+
+    R saveExcel(ExcelEditCallback callback);
 }

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

@@ -2,6 +2,7 @@ package org.springblade.manager.service.impl;
 
 import cn.hutool.core.date.DateTime;
 import cn.hutool.core.date.StopWatch;
+import cn.hutool.json.JSONUtil;
 import cn.hutool.log.StaticLog;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
@@ -11,6 +12,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.fasterxml.jackson.databind.exc.InvalidFormatException;
 import com.mixsmart.utils.FormulaUtils;
 import com.mixsmart.utils.ListUtils;
 import com.mixsmart.utils.RegexUtils;
@@ -18,6 +20,7 @@ import com.spire.xls.ExcelPicture;
 import com.spire.xls.FileFormat;
 import com.spire.xls.Worksheet;
 import com.spire.xls.collections.PicturesCollection;
+import io.github.autoffice.luckysheet.LuckysheetConverter;
 import io.swagger.models.auth.In;
 import lombok.AllArgsConstructor;
 import org.apache.commons.collections4.CollectionUtils;
@@ -6842,4 +6845,171 @@ public class ExcelTabServiceImpl extends BaseServiceImpl<ExcelTabMapper, ExcelTa
         istrue = null_count == val_count && width == maxCol;
         return xy_type + "," + index_state + "," + width + "," + istrue;
     }
+
+    //取文件
+    @Override
+    public R getExcelUrl(ExcelEditCallback callback) {
+        //创建临时文件
+        //获取onlyOffice缓存中的文件流
+        String file_path = FileUtils.getSysLocalFileUrl();
+        String filecode = SnowFlakeUtil.getId() + "";
+        String dataUrl = file_path + "/excel/" + filecode + ".xlsx";
+
+        try {
+            File file = downloadFileToTemp(callback.getUrl(), dataUrl);
+            if(file != null){
+                // 将excel文件转为luckysheet json
+                String json = LuckysheetConverter.excelToLuckySheetJson(dataUrl);
+                cn.hutool.json.JSONObject entries = JSONUtil.parseObj(json);
+                cn.hutool.json.JSONArray sheets = entries.getJSONArray("sheets");
+
+                //删除临时文件
+                if (file.exists()) {
+                    file.delete();
+                }
+                return R.success(sheets.toString());
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+
+        return R.fail("文件异常");
+    }
+
+
+
+    //设置文件
+    @Override
+    public R saveExcel(ExcelEditCallback callback) {
+        String tabId = callback.getKey();
+        if (tabId == null) {
+            throw new ServiceException("excel为空");
+        } else {
+            tabId = tabId.substring(0, tabId.lastIndexOf("_"));
+        }
+
+        String file_path = FileUtils.getSysLocalFileUrl();
+        String filecode = SnowFlakeUtil.getId() + "";
+        //创建临时json文件
+        String jsonFile = file_path + "/excel/" + filecode + ".json";
+        //创建临时excel文件
+        String excelFile = file_path + "/excel/" + filecode + ".xlsx";
+
+        try (FileWriter file = new FileWriter(jsonFile)) {
+            file.write(callback.getJsonExcel());
+            file.flush();
+            System.out.println("JSON数据已成功保存到: " + jsonFile);
+        } catch (IOException e) {
+            System.err.println("保存JSON文件时出错: " + e.getMessage());
+            e.printStackTrace();
+        }
+        //获取文件流
+        File file = new File(excelFile);
+
+        try {
+            // 将luckysheet json文件转为excel
+            LuckysheetConverter.luckysheetToExcel(jsonFile, excelFile);
+
+            //上传新文件到文件服务器
+            //excel修改 同步修改html 对象
+            String thmlUrl = file_path + filecode + ".html";
+            ExcelInfoUtils.excelInfo(Files.newInputStream(file.toPath()), excelFile, thmlUrl, "2");
+            //获取数据库信息
+            ExcelTab excelTab = baseMapper.selectById(Long.parseLong(tabId));
+            //上传到服务器
+            BladeFile bladeFile = newIOSSClient.uploadFile(excelTab.getExtension(), excelFile);
+            //获取文件大小
+            int size = (int) (file.length() / 1024 / 1024); //单位M
+            excelTab.setAttachSize(Long.parseLong(size + ""));
+            excelTab.setStatus(3);
+            excelTab.setFileUrl(bladeFile.getLink());
+            excelTab.setHtmlUrl(thmlUrl);
+            baseMapper.updateById(excelTab);
+
+            expailHtmlInfo(thmlUrl, excelTab.getId(), excelTab.getTabType() + "");
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+
+        if(file.exists()){
+            file.delete();
+        }
+        File file2 = new File(jsonFile);
+        if (file2.exists()) {
+            file2.delete();
+        }
+        return R.success("成功");
+    }
+
+
+
+
+    /**
+     * 从远程URL下载文件到本地临时文件
+     * @param fileUrl 远程文件的URL
+     * @return 本地临时文件
+     * @throws IOException 当下载或文件创建失败时抛出
+     */
+    public static File downloadFileToTemp(String fileUrl,String lsFile) throws IOException {
+        // 创建临时文件
+        File tempFile = new File(lsFile);
+        tempFile.deleteOnExit(); // 确保程序退出时删除临时文件
+
+        HttpURLConnection connection = null;
+        InputStream inputStream = null;
+        FileOutputStream outputStream = null;
+
+        try {
+            URL url = new URL(fileUrl);
+            connection = (HttpURLConnection) url.openConnection();
+            connection.setRequestMethod("GET");
+            connection.setConnectTimeout(30000);
+            connection.setReadTimeout(30000);
+
+            // 检查HTTP响应码
+            int responseCode = connection.getResponseCode();
+            if (responseCode != HttpURLConnection.HTTP_OK) {
+                throw new IOException("服务器返回HTTP响应码: " + responseCode);
+            }
+
+            // 获取输入流
+            inputStream = connection.getInputStream();
+
+            // 创建输出流写入临时文件
+            outputStream = new FileOutputStream(tempFile);
+
+            // 缓冲区大小
+            byte[] buffer = new byte[4096];
+            int bytesRead;
+
+            // 读取数据并写入文件
+            while ((bytesRead = inputStream.read(buffer)) != -1) {
+                outputStream.write(buffer, 0, bytesRead);
+            }
+
+            System.out.println("文件下载成功: " + tempFile.getAbsolutePath());
+            return tempFile;
+
+        } finally {
+            // 确保资源被关闭
+            if (inputStream != null) {
+                try {
+                    inputStream.close();
+                } catch (IOException e) {
+                    System.err.println("关闭输入流时出错: " + e.getMessage());
+                }
+            }
+            if (outputStream != null) {
+                try {
+                    outputStream.close();
+                } catch (IOException e) {
+                    System.err.println("关闭输出流时出错: " + e.getMessage());
+                }
+            }
+            if (connection != null) {
+                connection.disconnect();
+            }
+        }
+    }
+
 }

BIN
doc/lib/excelLib/luckysheet-lib-1.1.0.jar