Bladeren bron

优化最后一页空白

callm 2 dagen geleden
bovenliggende
commit
b27d5ed3a5
1 gewijzigde bestanden met toevoegingen van 25 en 31 verwijderingen
  1. 25 31
      UAS_MES_ZKLH/FunctionCode/Query/Query_SOP.cs

+ 25 - 31
UAS_MES_ZKLH/FunctionCode/Query/Query_SOP.cs

@@ -53,7 +53,10 @@ namespace UAS_MES_NEW.Query
             pr_code.DBTitle = "物料查询";
             pr_code.Condition = "pr_code not like '%LX%'";
             pr_code.SetValueField = new string[] { "pr_code" };
-            DateCode.Value = DateCode.Value.AddMonths(2);
+            DateTime targetMonth = DateCode.Value.AddMonths(2);
+            DateTime endOfMonth = new DateTime(targetMonth.Year, targetMonth.Month,
+                                               DateTime.DaysInMonth(targetMonth.Year, targetMonth.Month));
+            DateCode.Value = endOfMonth;
             dh = SystemInf.dh;
         }
 
@@ -126,17 +129,13 @@ namespace UAS_MES_NEW.Query
                             Directory.CreateDirectory(Application.StartupPath + @"\" + ps_prodcode.Text);
                         }
                         string path = Application.StartupPath + @"\" + ps_prodcode.Text + @"\";
-
                         Worksheet she = workbook.Worksheets[i];
-
                         // ---------- 基础参数 ----------
                         int lastColIndex = she.Cells.MaxDataColumn + 3;
                         string lastColLetter = CellsHelper.ColumnIndexToName(lastColIndex);
                         const int headerRows = 3;                              // 表头行数(每页都保留)
                         int dataRowsPerPage = int.Parse(RowCount.Value.ToString());
-
-                        // ★ 关键修复①:计算"内容最大行"时必须包含图片和形状的底部锚点行
-                        // 图片/形状不影响 Cells.MaxDataRow,只看单元格会把底部图片切掉
+                        // 计算"内容最大行"时包含图片和形状的底部锚点行
                         int contentMaxRow = she.Cells.MaxDataRow;
                         for (int p = 0; p < she.Pictures.Count; p++)
                         {
@@ -148,17 +147,22 @@ namespace UAS_MES_NEW.Query
                             if (she.Shapes[s].LowerRightRow > contentMaxRow)
                                 contentMaxRow = she.Shapes[s].LowerRightRow;
                         }
-                        int totalRows = contentMaxRow + 1;
-                        int totalDataRows = Math.Max(totalRows - headerRows, 0);
-                        int totalPages = (int)Math.Ceiling(totalDataRows / (double)dataRowsPerPage);
+
+                        // ========== ★ 修改点1:totalRows 向上取整到完整页数 ==========
+                        // 原来:int totalRows = contentMaxRow + 1;  → 最后一页只到实际数据行,空白被截掉
+                        // 现在:先算实际数据行数和页数,再把 totalRows 补足为 headerRows + 总页数 × 每页行数
+                        // 这样最后一页即使数据不足,也会保留完整的 dataRowsPerPage 行空白
+                        int actualDataRows = Math.Max(contentMaxRow + 1 - headerRows, 0);
+                        int totalPages = (int)Math.Ceiling(actualDataRows / (double)dataRowsPerPage);
                         if (totalPages == 0) totalPages = 1;
+                        int totalRows = headerRows + totalPages * dataRowsPerPage;
+                        // ========== ★ 修改点1结束 ==========
 
+                        int totalDataRows = Math.Max(totalRows - headerRows, 0);
                         // 水印在原表中的目标行/列
                         int wmRowGlobal = int.Parse(Y.Value.ToString());
                         int wmCol = int.Parse(X.Value.ToString());
-
                         int imageIndex = 1;
-
                         for (int page = 0; page < totalPages; page++)
                         {
                             // ---------- 1. 复制原表到临时工作表 ----------
@@ -166,11 +170,9 @@ namespace UAS_MES_NEW.Query
                             Worksheet newshe = workbook.Worksheets[newSheetIndex];
                             newshe.Copy(she);
                             newshe.Name = $"Temp_{page}";
-
                             // 本页在原表中的数据行区间(原表坐标,0-based)
                             int dataStartRow = headerRows + page * dataRowsPerPage;
                             int dataEndRow = Math.Min(headerRows + (page + 1) * dataRowsPerPage - 1, totalRows - 1);
-
                             // ---------- 2. 按页过滤原表图片(Pictures) ----------
                             for (int p = newshe.Pictures.Count - 1; p >= 0; p--)
                             {
@@ -183,9 +185,7 @@ namespace UAS_MES_NEW.Query
                                     newshe.Pictures.RemoveAt(p);
                                 }
                             }
-
                             // ---------- 3. 按页过滤原表形状(Shapes:文本框/箭头/连接线等) ----------
-
                             for (int s = newshe.Shapes.Count - 1; s >= 0; s--)
                             {
                                 Aspose.Cells.Drawing.Shape shape;
@@ -198,11 +198,9 @@ namespace UAS_MES_NEW.Query
                                     // 下标已经失效,直接跳过
                                     continue;
                                 }
-
                                 int topRow = shape.UpperLeftRow;
                                 bool isHeaderShape = topRow < headerRows;
                                 bool belongsToPage = topRow >= dataStartRow && topRow <= dataEndRow;
-
                                 if (!isHeaderShape && !belongsToPage)
                                 {
                                     try
@@ -216,7 +214,6 @@ namespace UAS_MES_NEW.Query
                                     }
                                 }
                             }
-
                             // ---------- 4. 清空非本页单元格内容 ----------
                             for (int r = 0; r < totalRows; r++)
                             {
@@ -224,7 +221,6 @@ namespace UAS_MES_NEW.Query
                                 if (r >= dataStartRow && r <= dataEndRow) continue;
                                 newshe.Cells.ClearRange(r, 0, r, lastColIndex);
                             }
-
                             // ---------- 5. 删除非本页整行 ----------
                             for (int r = totalRows - 1; r >= 0; r--)
                             {
@@ -232,10 +228,7 @@ namespace UAS_MES_NEW.Query
                                 if (r >= dataStartRow && r <= dataEndRow) continue;
                                 newshe.Cells.DeleteRow(r);
                             }
-
                             // ---------- 6. 页面设置 ----------
-                            // ★ 关键修复②:PrintArea 必须覆盖临时表中图片/形状延伸到的最大行
-                            // 不能只用 Cells.MaxDataRow + 1,否则底部图片被截断
                             int printMaxRow = newshe.Cells.MaxDataRow;
                             for (int p = 0; p < newshe.Pictures.Count; p++)
                             {
@@ -248,14 +241,23 @@ namespace UAS_MES_NEW.Query
                                     printMaxRow = newshe.Shapes[s].LowerRightRow;
                             }
                             int newTotalRows = printMaxRow + 1;
+
+                            // ========== ★ 修改点2:PrintArea 固定页高,最后一页空白行也显示 ==========
+                            // 原来:最后一页 PrintArea 只到 newTotalRows(实际内容行),空白被截掉
+                            // 现在:打印区高度取 max(实际内容行, 表头+每页行数),保证每页等高
+                            int pageHeight = headerRows + dataRowsPerPage;
+                            int printEndRow = Math.Max(newTotalRows, pageHeight);
+
                             if (i == 0)
                             {
                                 newshe.PageSetup.PrintArea = $"A1:{lastColLetter}{25}";
                             }
                             else
                             {
-                                newshe.PageSetup.PrintArea = $"A1:{lastColLetter}{newTotalRows}";
+                                newshe.PageSetup.PrintArea = $"A1:{lastColLetter}{printEndRow}";
                             }
+                            // ========== ★ 修改点2结束 ==========
+
                             newshe.PageSetup.CenterHorizontally = true;
                             newshe.PageSetup.CenterVertically = true;
                             newshe.PageSetup.LeftMargin = 2;
@@ -265,7 +267,6 @@ namespace UAS_MES_NEW.Query
                             newshe.PageSetup.SetFooter(0, "");
                             newshe.PageSetup.SetFooter(1, "");
                             newshe.PageSetup.SetFooter(2, "");
-
                             // ---------- 7. 插入水印(额外图片,坐标映射到压缩后本页) ----------
                             if (File.Exists(PrintPath.Text))
                             {
@@ -275,13 +276,11 @@ namespace UAS_MES_NEW.Query
                             // ---------- 8. 页脚 ----------
                             string pageFooter = $"第 {imageIndex} 页 / 共 {totalPages} 页";
                             newshe.PageSetup.SetFooter(1, pageFooter);
-
                             // ---------- 9. 渲染为 PNG ----------
                             ImageOrPrintOptions options = new ImageOrPrintOptions();
                             options.ImageFormat = ImageFormat.Png;
                             options.OnePagePerSheet = true;
                             options.PrintingPage = PrintingPageType.Default;
-
                             SheetRender sr = new SheetRender(newshe, options);
                             if (sr.PageCount > 0)
                             {
@@ -291,29 +290,24 @@ namespace UAS_MES_NEW.Query
                                     string imagePath = Path.Combine(path, imagename);
                                     bitmap.Save(imagePath, ImageFormat.Png);
                                     OperatResult.AppendText("解析图片【" + imagePath + "】\n");
-
                                     // ---------- 10. 上传 ----------
                                     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);
-
                                     // ---------- 11. 入库 ----------
                                     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");
                                 }
                             }
-
                             // ---------- 12. 删除临时工作表 ----------
                             workbook.Worksheets.RemoveAt(newSheetIndex);
                             imageIndex++;