Przeglądaj źródła

优化SOP上传

callm 1 miesiąc temu
rodzic
commit
d00853eb1b
1 zmienionych plików z 81 dodań i 99 usunięć
  1. 81 99
      UAS_MES_ZKLH/FunctionCode/Query/Query_SOP.cs

+ 81 - 99
UAS_MES_ZKLH/FunctionCode/Query/Query_SOP.cs

@@ -126,138 +126,120 @@ namespace UAS_MES_NEW.Query
                             }
 
                             string path = Application.StartupPath + @"\" + ps_prodcode.Text + @"\";
+                            Worksheet she = workbook.Worksheets[i];
 
-                            // ========== 获取原始工作表 ==========
-                            Worksheet originalSheet = workbook.Worksheets[i];
-                            Console.WriteLine(originalSheet.Name);
-                            int lastColIndex = originalSheet.Cells.MaxDataColumn + 3;
+                            // 获取原表的实际数据范围和最大列
+                            int totalRows = she.Cells.MaxDataRow + 1;
+                            int lastColIndex = she.Cells.MaxDataColumn + 5;
                             string lastColLetter = CellsHelper.ColumnIndexToName(lastColIndex);
 
-                            int headerRows = 3;
-                            int dataRowsPerPage = int.Parse(RowCount.Value.ToString());
+                            int rowSliceSize = int.Parse(RowCount.Value.ToString());
+                            int imageIndex = 1;
 
-                            // ========== 计算数据行数 ==========
-                            int totalDataRows = 0;
-                            for (int r = headerRows; r <= originalSheet.Cells.MaxDataRow; r++)
+                            // ========== 核心修改:手动计算总页数 ==========
+                            // 总页数 = 总行数 除以 每页行数,向上取整
+                            int totalPages = (int)Math.Ceiling((double)totalRows / rowSliceSize);
+                            // ============================================
+
+                            // 记录原表设置,循环结束后恢复,避免污染原文件
+                            string originalPrintArea = she.PageSetup.PrintArea;
+                            double originalLeftMargin = she.PageSetup.LeftMargin;
+                            double originalRightMargin = she.PageSetup.RightMargin;
+                            double originalTopMargin = she.PageSetup.TopMargin;
+                            double originalBottomMargin = she.PageSetup.BottomMargin;
+
+                            // 设置居中、无边距的打印模式,确保图片紧密贴合
+                            she.PageSetup.CenterHorizontally = true;
+                            she.PageSetup.CenterVertically = true;
+                            she.PageSetup.LeftMargin = 0;
+                            she.PageSetup.RightMargin = 0;
+                            she.PageSetup.TopMargin = 0;
+                            she.PageSetup.BottomMargin = 0;
+
+                            // 循环开始前先彻底清空原有页脚,防止残留格式干扰
+                            she.PageSetup.SetFooter(0, "");
+                            she.PageSetup.SetFooter(1, "");
+                            she.PageSetup.SetFooter(2, "");
+
+                            for (int startRow = 0; startRow < totalRows; startRow += rowSliceSize)
                             {
-                                bool hasData = false;
-                                for (int c = 0; c <= originalSheet.Cells.MaxDataColumn; c++)
-                                {
-                                    if (originalSheet.Cells[r, c].Value != null && !string.IsNullOrWhiteSpace(originalSheet.Cells[r, c].Value.ToString()))
-                                    {
-                                        hasData = true;
-                                        break;
-                                    }
-                                }
-                                if (hasData)
+                                int endRow = Math.Min(startRow + rowSliceSize - 1, totalRows - 1);
+
+                                // 1. 动态设置原表的打印区域(模拟切片)
+                                if (endRow < rowSliceSize)
                                 {
-                                    totalDataRows++;
+                                    Console.WriteLine($"A{startRow + 1}:{lastColLetter}{rowSliceSize + 1}");
+                                    she.PageSetup.PrintArea = $"A{startRow + 1}:{lastColLetter}{rowSliceSize + 1}";
                                 }
                                 else
                                 {
-                                    totalDataRows++;
-                                    //break;
+                                    Console.WriteLine($"A{startRow + 1}:{lastColLetter}{endRow + 1}");
+                                    she.PageSetup.PrintArea = $"A{startRow + 1}:{lastColLetter}{endRow + 1}";
                                 }
-                            }
 
-                            int totalRows = headerRows + totalDataRows;
-                            int totalPages = (int)Math.Ceiling((double)totalDataRows / dataRowsPerPage);
-
-                            int imageIndex = 1;
-
-                            for (int page = 0; page < (totalPages == 0 ? 1 : totalPages); page++)
-                            {
-                                // ========== 关键修复:每次循环复制一份新的工作表 ==========
-                                // 复制原始工作表到新的临时工作表
-                                int newSheetIndex = workbook.Worksheets.Add();
-                                Worksheet she = workbook.Worksheets[newSheetIndex];
-                                she.Copy(originalSheet);
-                                she.Name = $"Temp_{page}";
-
-                                // 计算当前页的数据范围
-                                int dataStartRow = headerRows + page * dataRowsPerPage;
-                                int dataEndRow = Math.Min(headerRows + (page + 1) * dataRowsPerPage - 1, totalRows - 1);
-
-                                // ========== 删除当前页不需要的行(保留标题行 + 当前页数据行) ==========
-                                // 从下往上删除,避免索引变化
-                                for (int r = totalRows - 1; r >= 0; r--)
+                                if (File.Exists(PrintPath.Text))
                                 {
-                                    if (r < headerRows || (r >= dataStartRow && r <= dataEndRow))
+                                    if (startRow == 0)
                                     {
-                                        // 保留标题行和当前页数据行
-                                        continue;
+                                        she.Pictures.Add(startRow + int.Parse(Y.Value.ToString()), int.Parse(X.Value.ToString()), PrintPath.Text);
                                     }
                                     else
                                     {
-                                        // 删除不需要的行
-                                        she.Cells.DeleteRow(r);
+                                        she.Pictures.Add(startRow - 4 + int.Parse(Y.Value.ToString()), int.Parse(X.Value.ToString()), PrintPath.Text);
                                     }
                                 }
 
-                                // ========== 设置打印区域为整个表 ==========
-                                int newTotalRows = she.Cells.MaxDataRow + 1;
-
-
-                                string newPrintArea = $"A1:{lastColLetter}{(totalPages == 0 ? dataRowsPerPage : newTotalRows)}";
-                                she.PageSetup.PrintArea = newPrintArea;
-
-                                she.PageSetup.CenterHorizontally = true;
-                                she.PageSetup.CenterVertically = true;
-                                she.PageSetup.LeftMargin = 0;
-                                she.PageSetup.RightMargin = 0;
-                                she.PageSetup.TopMargin = 0;
-                                she.PageSetup.BottomMargin = 2;
-
-                                // ========== 添加图片/水印 ==========
-                                if (File.Exists(PrintPath.Text))
-                                {
-                                    she.Pictures.Clear();
-                                    int rowOffset = headerRows + int.Parse(Y.Value.ToString());
-                                    she.Pictures.Add(rowOffset, int.Parse(X.Value.ToString()), PrintPath.Text);
-                                }
-
-                                // ========== 写入页脚 ==========
-
+                                // ========== 核心修改:手动写入硬编码页脚 ==========
+                                // 当前 page 是 imageIndex,总页数是我们刚才算的 totalPages
                                 string pageFooter = $"第 {imageIndex} 页 / 共 {totalPages} 页";
+                                // 直接写入页脚中间位置
                                 she.PageSetup.SetFooter(1, pageFooter);
+                                // ================================================
 
-                                // ========== 渲染图片 ==========
+                                // 2. 创建渲染选项
                                 ImageOrPrintOptions options = new ImageOrPrintOptions();
                                 options.ImageFormat = ImageFormat.Png;
-                                options.OnePagePerSheet = true;  // 改为 true,确保只渲染一页
+                                options.OnePagePerSheet = false;
                                 options.PrintingPage = PrintingPageType.Default;
 
+                                // 3. 渲染图片
                                 SheetRender sr = new SheetRender(she, options);
+                                Bitmap bitmap = sr.ToImage(0);
 
-                                if (sr.PageCount > 0)
-                                {
-                                    Bitmap bitmap = sr.ToImage(0);
-
-                                    string imagename = $"{originalSheet.Name}_{imageIndex}.png";
-                                    string imagePath = Path.Combine(path, imagename);
-                                    bitmap.Save(imagePath, ImageFormat.Png);
-
-                                    bitmap.Dispose();
+                                // 4. 处理并保存图片
+                                string imagename = $"{she.Name}_{imageIndex}.png";
+                                string imagePath = Path.Combine(path, imagename);
+                                bitmap.Save(imagePath, ImageFormat.Png);
 
-                                    OperatResult.AppendText("解析图片【" + imagePath + "】" + "\n");
+                                bitmap.Dispose();
+                                //sr.Dispose(); // 如果旧版本这里报错,可以保留注释
 
-                                    // ========== 上传与入库 ==========
-                                    Dictionary<string, object> dic = new Dictionary<string, object>();
-                                    dic.Add("em_name", "管理员");
-                                    dic.Add("em_code", "ADMIN");
-                                    dic.Add("caller", "ProductSOP");
-                                    OperatResult.AppendText("上传文件【" + imagePath + "】\n");
-                                    string fp_id = UploadFilesToRemoteUrl("https://mes.lihantech.cn/mes/MEScommon/uploadFiles.action?_noc=1", imagePath, dic);
+                                imageIndex++; // 这一行对应下一张图片的页码,放在这里即可
+                                OperatResult.AppendText("解析图片【" + imagePath + "】" + "\n");
 
-                                    string ps_id = dh.getFieldDataByCondition("ProductSOP", "ps_id", "ps_prodcode='" + ps_prodcode.Text + "'").ToString();
-                                    dh.ExecuteSql("insert into ProductSOPdetail(psd_id,psd_psid,psd_attach) select ProductSOPdetail_seq.nextval,ps_id,'" + imagename + ";" + fp_id + "' from ProductSOP where ps_prodcode='" + ps_prodcode.Text + "'", "insert");
-                                }
-
-                                // ========== 删除临时工作表 ==========
-                                workbook.Worksheets.RemoveAt(newSheetIndex);
+                                // 5. 上传与入库
+                                Dictionary<string, object> dic = new Dictionary<string, object>();
+                                dic.Add("em_name", "管理员");
+                                dic.Add("em_code", "ADMIN");
+                                dic.Add("caller", "ProductSOP");
+                                OperatResult.AppendText("上传文件【" + imagePath + "】\n");
+                                string fp_id = UploadFilesToRemoteUrl("https://mes.lihantech.cn/mes/MEScommon/uploadFiles.action?_noc=1", imagePath, dic);
 
-                                imageIndex++;
+                                string ps_id = dh.getFieldDataByCondition("ProductSOP", "ps_id", "ps_prodcode='" + ps_prodcode.Text + "'").ToString();
+                                dh.ExecuteSql("insert into ProductSOPdetail(psd_id,psd_psid,psd_attach) select ProductSOPdetail_seq.nextval,ps_id,'" + imagename + ";" + fp_id + "' from ProductSOP where ps_prodcode='" + ps_prodcode.Text + "'", "insert");
                             }
+
+                            // 恢复原表设置
+                            she.PageSetup.PrintArea = originalPrintArea;
+                            she.PageSetup.LeftMargin = originalLeftMargin;
+                            she.PageSetup.RightMargin = originalRightMargin;
+                            she.PageSetup.TopMargin = originalTopMargin;
+                            she.PageSetup.BottomMargin = originalBottomMargin;
+
+                            // 循环结束后彻底清空页脚,防止影响下次使用
+                            she.PageSetup.SetFooter(0, "");
+                            she.PageSetup.SetFooter(1, "");
+                            she.PageSetup.SetFooter(2, "");
                         }
                         catch (Exception ex)
                         {