Browse Source

优化页码显示

callm 3 weeks ago
parent
commit
e0514d82b0
1 changed files with 26 additions and 9 deletions
  1. 26 9
      UAS_MES_ZKLH/FunctionCode/Query/Query_SOP.cs

+ 26 - 9
UAS_MES_ZKLH/FunctionCode/Query/Query_SOP.cs

@@ -130,12 +130,20 @@ namespace UAS_MES_NEW.Query
 
                             // 获取原表的实际数据范围和最大列
                             int totalRows = she.Cells.MaxDataRow;
-                            int lastColIndex = she.Cells.MaxDataColumn+5;
+                            int lastColIndex = she.Cells.MaxDataColumn + 5;
                             string lastColLetter = CellsHelper.ColumnIndexToName(lastColIndex);
 
                             int rowSliceSize = int.Parse(RowCount.Value.ToString());
                             int imageIndex = 1;
 
+                            // ========== 计算总页数 ==========
+                            int totalPages = (int)Math.Ceiling((double)totalRows / rowSliceSize);
+                            // 清除原有页脚,防止干扰
+                            she.PageSetup.SetFooter(0, ""); // 左
+                            she.PageSetup.SetFooter(1, ""); // 中
+                            she.PageSetup.SetFooter(2, ""); // 右
+                                                            // =================================
+
                             // 记录原表设置,循环结束后恢复,避免污染原文件
                             string originalPrintArea = she.PageSetup.PrintArea;
                             double originalLeftMargin = she.PageSetup.LeftMargin;
@@ -156,12 +164,12 @@ namespace UAS_MES_NEW.Query
                                 int endRow = Math.Min(startRow + rowSliceSize - 1, totalRows - 1);
 
                                 // 1. 动态设置原表的打印区域(模拟切片)
-                                // 注意:这里直接使用原表渲染,虽然旧版本对图片的浮层处理有限,但这能最大程度保留图片
                                 if (endRow < rowSliceSize)
                                 {
                                     she.PageSetup.PrintArea = $"A{startRow + 1}:{lastColLetter}{rowSliceSize + 1}";
                                 }
-                                else {
+                                else
+                                {
                                     she.PageSetup.PrintArea = $"A{startRow + 1}:{lastColLetter}{endRow + 1}";
                                 }
 
@@ -170,21 +178,25 @@ namespace UAS_MES_NEW.Query
                                     if (startRow == 0)
                                     {
                                         she.Pictures.Add(startRow + int.Parse(Y.Value.ToString()), int.Parse(X.Value.ToString()), PrintPath.Text);
-
                                     }
                                     else
                                     {
                                         she.Pictures.Add(startRow - 4 + int.Parse(Y.Value.ToString()), int.Parse(X.Value.ToString()), PrintPath.Text);
-
                                     }
                                 }
 
+                                // ========== 设置当前页的全局页码 ==========
+                                // 使用脚本命令拼接:当前页 &P ,总页数 &N
+                                string pageFooter = $"&P / &N";
+                                // 放置在页脚中间 (Section 1)
+                                she.PageSetup.SetFooter(1, pageFooter);
+                                // =========================================
 
                                 // 2. 创建渲染选项
                                 ImageOrPrintOptions options = new ImageOrPrintOptions();
                                 options.ImageFormat = ImageFormat.Png;
-                                options.OnePagePerSheet = true; // 强制在一页内输出,严格限制范围
-                                options.PrintingPage = PrintingPageType.Default; // 禁止忽略空白行列
+                                options.OnePagePerSheet = false;
+                                options.PrintingPage = PrintingPageType.Default;
 
                                 // 3. 渲染图片
                                 SheetRender sr = new SheetRender(she, options);
@@ -195,9 +207,8 @@ namespace UAS_MES_NEW.Query
                                 string imagePath = Path.Combine(path, imagename);
                                 bitmap.Save(imagePath, ImageFormat.Png);
 
-                                // 释放资源,防止内存溢出
                                 bitmap.Dispose();
-                                //sr.Dispose();
+                                //sr.Dispose(); // 如果旧版本这里报错,可以保留注释
 
                                 imageIndex++;
                                 OperatResult.AppendText("解析图片【" + imagePath + "】" + "\n");
@@ -220,6 +231,12 @@ namespace UAS_MES_NEW.Query
                             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)
                         {