|
@@ -0,0 +1,235 @@
|
|
|
+package org.springblade.system.user.controller;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
|
|
+import io.github.autoffice.luckysheet.LuckysheetConverter;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.springblade.common.utils.SnowFlakeUtil;
|
|
|
+import org.springblade.core.log.exception.ServiceException;
|
|
|
+import org.springblade.core.oss.model.BladeFile;
|
|
|
+import org.springblade.core.tool.api.R;
|
|
|
+import org.springblade.manager.entity.ExcelEditCallback;
|
|
|
+import org.springblade.manager.entity.ExcelTab;
|
|
|
+import org.springblade.manager.feign.ExcelTabClient;
|
|
|
+import org.springblade.resource.feign.NewIOSSClient;
|
|
|
+import org.springblade.system.user.util.FileUtils;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.nio.file.Files;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author LHB
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping(value = "/excel-table")
|
|
|
+@AllArgsConstructor
|
|
|
+@Api(value = "excel5.4.0接口", tags = "excel5.4.0接口")
|
|
|
+public class ExcelTableController {
|
|
|
+
|
|
|
+ private final ExcelTabClient excelTabClient;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在线excel 获取文件json数据
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @PostMapping(value = "/getExcelUrl")
|
|
|
+ @ApiOperation(value = "获取文件json数据", notes = "获取文件json数据")
|
|
|
+ @ApiOperationSupport(order = 35)
|
|
|
+ @ResponseBody
|
|
|
+ public R getExcelUrl(@RequestBody ExcelEditCallback callback) {
|
|
|
+ //创建临时文件
|
|
|
+ //获取onlyOffice缓存中的文件流
|
|
|
+ String file_path = FileUtils.getSysLocalFileUrl();
|
|
|
+ String filecode = SnowFlakeUtil.getId() + "";
|
|
|
+ String dataUrl = file_path + "/excel/" + filecode + ".xlsx";
|
|
|
+
|
|
|
+ try {
|
|
|
+ boolean b = downloadExcelFile(callback.getUrl(), dataUrl);
|
|
|
+ if(!b){
|
|
|
+ return R.fail("文件加载失败");
|
|
|
+ }
|
|
|
+ File file = new File(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.data(sheets.toString());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return R.fail("文件异常");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在线excel 保存
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @PostMapping(value = "/saveExcel")
|
|
|
+ @ApiOperation(value = "在线excel 保存", notes = "在线excel 保存")
|
|
|
+ @ApiOperationSupport(order = 35)
|
|
|
+ @ResponseBody
|
|
|
+ public R saveExcel(@RequestBody ExcelEditCallback callback) {
|
|
|
+ String tabId = callback.getKey();
|
|
|
+ if (tabId == null) {
|
|
|
+ throw new ServiceException("excel为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ 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)) {
|
|
|
+
|
|
|
+ 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();
|
|
|
+ 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);
|
|
|
+
|
|
|
+ //通过poi5.4.0 转成excel之后
|
|
|
+
|
|
|
+ //记录最新的excel文件
|
|
|
+ callback.setUrl(excelFile);
|
|
|
+ //把文件传到manage 处理文件保存
|
|
|
+ boolean b = excelTabClient.saveExcelTableLink(callback);
|
|
|
+ if(b == false){
|
|
|
+ return R.fail("失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ File file2 = new File(jsonFile);
|
|
|
+ if (file2.exists()) {
|
|
|
+ file2.delete();
|
|
|
+ }
|
|
|
+ return R.success("成功");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载远程Excel文件到本地,保持所有样式不变
|
|
|
+ *
|
|
|
+ * @param remoteFileUrl 远程Excel文件的URL地址
|
|
|
+ * @param localFilePath 本地保存路径(包括文件名)
|
|
|
+ * @return 下载成功返回true,否则返回false
|
|
|
+ */
|
|
|
+ public static boolean downloadExcelFile(String remoteFileUrl, String localFilePath) {
|
|
|
+ HttpURLConnection httpConn = null;
|
|
|
+ BufferedInputStream in = null;
|
|
|
+ FileOutputStream out = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 创建URL对象
|
|
|
+ URL url = new URL(remoteFileUrl);
|
|
|
+
|
|
|
+ // 打开连接
|
|
|
+ httpConn = (HttpURLConnection) url.openConnection();
|
|
|
+ httpConn.setRequestMethod("GET");
|
|
|
+ httpConn.connect();
|
|
|
+
|
|
|
+ // 检查HTTP响应码
|
|
|
+ int responseCode = httpConn.getResponseCode();
|
|
|
+ if (responseCode != HttpURLConnection.HTTP_OK) {
|
|
|
+ System.err.println("服务器返回HTTP代码: " + responseCode);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取文件大小(用于可能的进度跟踪)
|
|
|
+ int contentLength = httpConn.getContentLength();
|
|
|
+
|
|
|
+ // 创建保存目录(如果不存在)
|
|
|
+ File localFile = new File(localFilePath);
|
|
|
+ File parentDir = localFile.getParentFile();
|
|
|
+ if (parentDir != null && !parentDir.exists()) {
|
|
|
+ parentDir.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建输入输出流
|
|
|
+ in = new BufferedInputStream(httpConn.getInputStream());
|
|
|
+ out = new FileOutputStream(localFile);
|
|
|
+
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
+ int bytesRead;
|
|
|
+ long totalRead = 0;
|
|
|
+
|
|
|
+ System.out.println("开始下载Excel文件...");
|
|
|
+
|
|
|
+ // 读取数据并写入本地文件
|
|
|
+ while ((bytesRead = in.read(buffer)) != -1) {
|
|
|
+ out.write(buffer, 0, bytesRead);
|
|
|
+ totalRead += bytesRead;
|
|
|
+
|
|
|
+ // 可选:显示下载进度
|
|
|
+ if (contentLength > 0) {
|
|
|
+ int progress = (int) (totalRead * 100 / contentLength);
|
|
|
+ System.out.print("\r下载进度: " + progress + "%");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println("\nExcel文件下载完成: " + localFilePath);
|
|
|
+ return true;
|
|
|
+
|
|
|
+ } catch (IOException ex) {
|
|
|
+ System.err.println("下载过程中发生错误: " + ex.getMessage());
|
|
|
+ ex.printStackTrace();
|
|
|
+ return false;
|
|
|
+ } finally {
|
|
|
+ // 关闭资源
|
|
|
+ try {
|
|
|
+ if (in != null) in.close();
|
|
|
+ if (out != null) out.close();
|
|
|
+ if (httpConn != null) httpConn.disconnect();
|
|
|
+ } catch (IOException ex) {
|
|
|
+ System.err.println("关闭资源时发生错误: " + ex.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|