فهرست منبع

csv格式前端解析

gaoxiongzaq 1 سال پیش
والد
کامیت
0c4e5bc420

+ 1 - 0
server/src/main/java/cn/keking/service/FilePreview.java

@@ -32,6 +32,7 @@ public interface FilePreview {
     String DRAWUI_FILE_PREVIEW_PAGE = "drawio";
     String DRAWUI_FILE_PREVIEW_PAGE = "drawio";
     String NOT_SUPPORTED_FILE_PAGE = "fileNotSupported";
     String NOT_SUPPORTED_FILE_PAGE = "fileNotSupported";
     String XLSX_FILE_PREVIEW_PAGE = "officeweb";
     String XLSX_FILE_PREVIEW_PAGE = "officeweb";
+    String CSV_FILE_PREVIEW_PAGE = "officecsv";
 
 
     String filePreviewHandle(String url, Model model, FileAttribute fileAttribute);
     String filePreviewHandle(String url, Model model, FileAttribute fileAttribute);
 }
 }

+ 5 - 1
server/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java

@@ -60,9 +60,13 @@ public class OfficeFilePreviewImpl implements FilePreview {
         if (!officePreviewType.equalsIgnoreCase("html")) {
         if (!officePreviewType.equalsIgnoreCase("html")) {
             if (ConfigConstants.getOfficeTypeWeb() .equalsIgnoreCase("web")) {
             if (ConfigConstants.getOfficeTypeWeb() .equalsIgnoreCase("web")) {
                 if (suffix.equalsIgnoreCase("xlsx")) {
                 if (suffix.equalsIgnoreCase("xlsx")) {
-                    model.addAttribute("pdfUrl", url);
+                    model.addAttribute("pdfUrl", KkFileUtils.htmlEscape(url)); //特殊符号处理
                     return XLSX_FILE_PREVIEW_PAGE;
                     return XLSX_FILE_PREVIEW_PAGE;
                 }
                 }
+                if (suffix.equalsIgnoreCase("csv")) {
+                    model.addAttribute("pdfUrl", KkFileUtils.htmlEscape(url));
+                    return CSV_FILE_PREVIEW_PAGE;
+                }
             }
             }
         }
         }
         if (forceUpdatedCache|| !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
         if (forceUpdatedCache|| !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 137 - 0
server/src/main/resources/static/xspreadsheet/58eaeb4e52248a5c75936c6f4c33a370.svg


تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 5179 - 0
server/src/main/resources/static/xspreadsheet/cptable.full.js


+ 71 - 0
server/src/main/resources/static/xspreadsheet/is-utf8.js

@@ -0,0 +1,71 @@
+function isUTF8(bytes) {
+    var i = 0;
+    while (i < bytes.length) {
+        if ((// ASCII
+            bytes[i] == 0x09 ||
+            bytes[i] == 0x0A ||
+            bytes[i] == 0x0D ||
+            (0x20 <= bytes[i] && bytes[i] <= 0x7E)
+        )
+        ) {
+            i += 1;
+            continue;
+        }
+
+        if ((// non-overlong 2-byte
+            (0xC2 <= bytes[i] && bytes[i] <= 0xDF) &&
+            (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF)
+        )
+        ) {
+            i += 2;
+            continue;
+        }
+
+        if ((// excluding overlongs
+            bytes[i] == 0xE0 &&
+            (0xA0 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
+            (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
+        ) ||
+            (// straight 3-byte
+                ((0xE1 <= bytes[i] && bytes[i] <= 0xEC) ||
+                    bytes[i] == 0xEE ||
+                    bytes[i] == 0xEF) &&
+                (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
+                (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
+            ) ||
+            (// excluding surrogates
+                bytes[i] == 0xED &&
+                (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0x9F) &&
+                (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
+            )
+        ) {
+            i += 3;
+            continue;
+        }
+
+        if ((// planes 1-3
+            bytes[i] == 0xF0 &&
+            (0x90 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
+            (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
+            (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
+        ) ||
+            (// planes 4-15
+                (0xF1 <= bytes[i] && bytes[i] <= 0xF3) &&
+                (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
+                (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
+                (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
+            ) ||
+            (// plane 16
+                bytes[i] == 0xF4 &&
+                (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0x8F) &&
+                (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
+                (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
+            )
+        ) {
+            i += 4;
+            continue;
+        }
+        return false;
+    }
+    return true;
+}

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 24 - 0
server/src/main/resources/static/xspreadsheet/xlsx.full.min.js


تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 1 - 0
server/src/main/resources/static/xspreadsheet/xlsxspread.min.js


تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 1039 - 0
server/src/main/resources/static/xspreadsheet/xspreadsheet.css


تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 2 - 0
server/src/main/resources/static/xspreadsheet/xspreadsheet.js


تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 1 - 0
server/src/main/resources/static/xspreadsheet/zh-cn.js


+ 77 - 0
server/src/main/resources/web/officecsv.ftl

@@ -0,0 +1,77 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="UTF-8" />
+<title>${file.name}预览</title>
+<#include "*/commonHeader.ftl">
+<link rel="stylesheet" href="xspreadsheet/xspreadsheet.css"/>
+<script src="xspreadsheet/xspreadsheet.js"></script>
+<script src="xspreadsheet/is-utf8.js"></script>
+<script src="xspreadsheet/xlsx.full.min.js"></script>
+<script src="xspreadsheet/xlsxspread.min.js"></script>
+<script src="xspreadsheet/cptable.full.js"></script>
+<script src="xspreadsheet/zh-cn.js"></script>
+<script src="js/base64.min.js" type="text/javascript"></script>
+</head>
+	<#if pdfUrl?contains("http://") || pdfUrl?contains("https://")>
+    <#assign finalUrl="${pdfUrl}">
+    <#elseif pdfUrl?contains("ftp://") >
+  <#assign finalUrl="${pdfUrl}">
+<#else>
+    <#assign finalUrl="${baseUrl}${pdfUrl}">
+</#if>
+<body>
+<div id="htmlout"></div>
+<script>
+x_spreadsheet.locale('zh-cn');
+var HTMLOUT = document.getElementById('htmlout');
+var xspr = x_spreadsheet(HTMLOUT);
+HTMLOUT.style.height = (window.innerHeight - 400) + "px";
+HTMLOUT.style.width = (window.innerWidth - 50) + "px";
+
+var process_wb = (function() {
+  return function process_wb(wb) {
+    var data = stox(wb);
+    xspr.loadData(data);
+    if(typeof console !== 'undefined') console.log("output", new Date());
+  };
+})();
+ var url = '${finalUrl}';
+  var baseUrl = '${baseUrl}'.endsWith('/') ? '${baseUrl}' : '${baseUrl}' + '/';
+    if (!url.startsWith(baseUrl)) {
+         url = baseUrl + 'getCorsFile?urlPath=' + encodeURIComponent(Base64.encode(url));
+    }
+let xhr = new XMLHttpRequest();
+xhr.open('GET',url); //文件所在地址
+xhr.responseType = 'blob';
+xhr.onload = () => {
+let content = xhr.response;
+let blob = new Blob([content]);
+let file = new File([blob],'excel.csv',{ type: 'excel/csv' });
+var reader = new FileReader();
+reader.onload = function(e) {
+      if(typeof console !== 'undefined') console.log("onload", new Date());
+      var data = e.target.result;
+          data = new Uint8Array(data);
+          let f = isUTF8(data);
+            if (f) {
+              var str = cptable.utils.decode(65001, data);
+              process_wb(XLSX.read(str, { type: "string" }));
+                    } else {
+          var str = cptable.utils.decode(936, data);
+          process_wb(XLSX.read(str, { type: "string" }));
+                    }
+};
+reader.readAsArrayBuffer(file);
+        }
+        xhr.send();
+   		 /*初始化水印*/
+ if (!!window.ActiveXObject || "ActiveXObject" in window)
+{
+}else{
+ initWaterMark();
+}
+</script>
+
+</body>
+</html>