Browse Source

导出处理

yangg 6 months ago
parent
commit
a2a030b94f
1 changed files with 71 additions and 60 deletions
  1. 71 60
      src/views/project/components/dataList.vue

+ 71 - 60
src/views/project/components/dataList.vue

@@ -177,8 +177,10 @@
     <el-dialog
       title="已绑定模版"
       :visible.sync="boundTemplatesDialogVisible"
+      v-el-drag-dialog
       width="50%"
       append-to-body
+       :close-on-click-modal="false"
     >
       <el-table
         :data="boundTemplateList"
@@ -413,7 +415,7 @@ export default {
       }
     },
     // 2. 替换原有的 exportDocument 方法
-    async exportDocument(template) {
+  /*   async exportDocument(template) {
       try {
         const templateData = JSON.parse(template.data || "[]");
 
@@ -424,7 +426,6 @@ export default {
       <head>
         <meta charset="UTF-8">
         <style>
-          /* 保持原有样式 */
           body {
             font-family: SimSun, serif;  // 用宋体作默认字体
             font-size: 12pt;
@@ -458,20 +459,20 @@ export default {
             display: block;
             margin: 10px auto;
           }
-          /* 保持原有的字体样式 */
+ 
           span, div, p {
             font-family: inherit;
           }
-          /* 保持表格边框样式 */
+     
           table[border="1"] {
             border: 1px solid black;
           }
-          /* 保持列表样式 */
+     
           ul, ol {
             margin: 8px 0;
             padding-left: 20px;
           }
-          /* 保持标题样式 */
+          
           h1, h2, h3, h4, h5, h6 {
             font-family: SimSun, serif;
             margin: 12px 0;
@@ -507,17 +508,21 @@ export default {
           // 处理表格样式
           const tables = tempDiv.getElementsByTagName("table");
           Array.from(tables).forEach((table) => {
-            table.setAttribute("border", "1");
-            table.style.borderCollapse = "collapse";
-            table.style.width = "100%";
-            table.style.tableLayout = "fixed";
-
-            const cells = table.getElementsByTagName("td");
-            Array.from(cells).forEach((cell) => {
-              cell.style.border = "1px solid black";
-              cell.style.padding = "8px";
-              cell.style.wordWrap = "break-word";
-            });
+            // 只有当表格有边框属性时才添加边框样式
+            if (table.getAttribute("border") || 
+                window.getComputedStyle(table).border !== "none") {
+              table.setAttribute("border", "1");
+              table.style.borderCollapse = "collapse";
+              table.style.width = "100%";
+              table.style.tableLayout = "fixed";
+
+              const cells = table.getElementsByTagName("td");
+              Array.from(cells).forEach((cell) => {
+                cell.style.border = "1px solid black";
+                cell.style.padding = "8px";
+                cell.style.wordWrap = "break-word";
+              });
+            }
           });
 
           // 保持原有样式
@@ -534,7 +539,7 @@ export default {
               "font-style",
               "line-height",
               "margin",
-              "padding",
+              "padding"
             ];
 
             importantStyles.forEach((style) => {
@@ -562,12 +567,12 @@ export default {
             top: "2.54cm",
             right: "2.54cm",
             bottom: "2.54cm",
-            left: "2.54cm",
+            left: "2.54cm"
           },
           font: {
             name: "SimSun",
-            size: "12pt",
-          },
+            size: "12pt"
+          }
         });
 
         // 下载文件
@@ -577,9 +582,9 @@ export default {
         console.error("导出Word文档失败:", error);
         this.$message.error("导出文档失败,请稍后重试");
       }
-    },
+    }, */
 
-    /* // 修改导出文档的方法,确保表格有边框
+    // 修改导出文档的方法,确保表格有边框
     async exportDocument(template) {
       try {
         const templateData = JSON.parse(template.data || "[]");
@@ -770,7 +775,7 @@ export default {
         console.error("导出文档失败:", error);
         this.$alert("导出文档时发生错误,请稍后重试。");
       }
-    }, */
+    },
     /* 客户名称 */
     getCusomName(phaseId) {
       const product = this.customList.find((item) => item.id === phaseId);
@@ -933,7 +938,7 @@ export default {
         }
 
         this.$message.success("批量替换完成");
-       /*  this.boundTemplatesDialogVisible = false; */
+        /*  this.boundTemplatesDialogVisible = false; */
       } catch (error) {
         console.error("批量替换失败:", error);
         this.$message.error("批量替换过程中发生错误");
@@ -1242,14 +1247,14 @@ export default {
                 };
 
                 // 获取当前项的编号
-                  const currentCode = getCodeFromItem(item);
+                const currentCode = getCodeFromItem(item);
                 if (!currentCode) return; // 如果没有有效编号,跳过此项
 
                 // 查找匹配的替换内容
                 const matchedContent = replacementContents.find(
                   (content) => content.template_location === currentCode
                 );
-                
+
                 if (matchedContent?.content) {
                   // 构建替换内容
                   const formattedReplacement = `
@@ -1258,15 +1263,15 @@ export default {
                     </span>
                   `.trim();
 
-                    // 处理表格和其他内容
-                    updatedContent = this.processTableContent(
-                      updatedContent,
-                      item,
-                      formattedReplacement
-                    );
+                  // 处理表格和其他内容
+                  updatedContent = this.processTableContent(
+                    updatedContent,
+                    item,
+                    formattedReplacement
+                  );
 
                   // 记录日志
-                  if (process.env.NODE_ENV !== 'production') {
+                  if (process.env.NODE_ENV !== "production") {
                     console.log("替换信息:", {
                       原始编号: currentCode,
                       原始内容: item,
@@ -1328,11 +1333,12 @@ export default {
 
         // 新增:递归处理HTML节点
         const processNode = (node) => {
-          if (node.nodeType === 3) { // 文本节点
+          if (node.nodeType === 3) {
+            // 文本节点
             return node.textContent;
           }
 
-          let html = '';
+          let html = "";
           for (const child of node.childNodes) {
             html += processNode(child);
           }
@@ -1342,47 +1348,52 @@ export default {
         const handleSplitContent = () => {
           // 先获取纯文本内容用于匹配
           const plainText = processNode(tempDiv);
-          
+
           // 构建用于匹配分割标记的正则表达式
-          const splitRegex = new RegExp(`(\\[|【)(.*?)(${searchCode})(.*?)(\\]|】)`, 'g');
-          
+          const splitRegex = new RegExp(
+            `(\\[|【)(.*?)(${searchCode})(.*?)(\\]|】)`,
+            "g"
+          );
+
           let currentHtml = tempDiv.innerHTML;
-          
+
           // 查找所有可能的匹配
-          const matches = [...currentHtml.matchAll(/<[^>]+>|\[|\]|【|】|[^<\[\]【】]+/g)];
-          
+          const matches = [
+            ...currentHtml.matchAll(/<[^>]+>|\[|\]|【|】|[^<\[\]【】]+/g),
+          ];
+
           // 重建HTML,识别并替换目标编码
-          let rebuiltHtml = '';
-          let currentMatch = '';
+          let rebuiltHtml = "";
+          let currentMatch = "";
           let isCollecting = false;
           let bracketCount = 0;
-          
+
           for (let i = 0; i < matches.length; i++) {
             const part = matches[i][0];
-            
-            if (part === '[' || part === '【') {
+
+            if (part === "[" || part === "【") {
               isCollecting = true;
               bracketCount++;
               currentMatch = part;
               continue;
             }
-            
+
             if (isCollecting) {
               currentMatch += part;
-              
-              if (part === ']' || part === '】') {
+
+              if (part === "]" || part === "】") {
                 bracketCount--;
                 if (bracketCount === 0) {
                   // 检查收集到的内容是否包含目标编码
-                  const cleanText = currentMatch.replace(/<[^>]+>/g, '');
+                  const cleanText = currentMatch.replace(/<[^>]+>/g, "");
                   if (cleanText.includes(searchCode)) {
                     rebuiltHtml += replacement;
                     isCollecting = false;
-                    currentMatch = '';
+                    currentMatch = "";
                   } else {
                     rebuiltHtml += currentMatch;
                     isCollecting = false;
-                    currentMatch = '';
+                    currentMatch = "";
                   }
                 }
               }
@@ -1390,7 +1401,7 @@ export default {
               rebuiltHtml += part;
             }
           }
-          
+
           // 处理未闭合的标记
           if (currentMatch) {
             rebuiltHtml += currentMatch;
@@ -1398,10 +1409,10 @@ export default {
 
           // 清理可能的残留标签
           rebuiltHtml = rebuiltHtml
-            .replace(/(<span[^>]*>)\s*(<\/span>)/g, '')
-            .replace(/(<\/span>)(<span[^>]*>)/g, '')
-            .replace(/<a[^>]*><\/a>/g, '')
-            .replace(/\s+/g, ' ');
+            .replace(/(<span[^>]*>)\s*(<\/span>)/g, "")
+            .replace(/(<\/span>)(<span[^>]*>)/g, "")
+            .replace(/<a[^>]*><\/a>/g, "")
+            .replace(/\s+/g, " ");
 
           tempDiv.innerHTML = rebuiltHtml;
         };
@@ -1640,7 +1651,7 @@ export default {
       });
     },
 
-    // 获取有模版数据
+    // 获取��有模版数据
     async getAllTemplates() {
       const pageSize = 999; // 使用较大的页码一次性获取
       const params = {
@@ -2133,7 +2144,7 @@ export default {
         this.$message.success("导出成功");
         return res.data; // 返回结果,包含 file_path
       } catch (error) {
-        console.error("导出PDF败:", error);
+        console.error("导出PDF��败:", error);
         this.$alert("导出PDF时发生错误,请稍后重试。");
       }
     },