|
@@ -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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|