callm 3 дней назад
Родитель
Сommit
3aa40e62a7
1 измененных файлов с 97 добавлено и 76 удалено
  1. 97 76
      UAS_MES_ZKLH/FunctionCode/Query/Query_SOP.cs

+ 97 - 76
UAS_MES_ZKLH/FunctionCode/Query/Query_SOP.cs

@@ -120,89 +120,91 @@ namespace UAS_MES_NEW.Query
                     {
                     {
                         try
                         try
                         {
                         {
+                            // ---------- 输出目录 ----------
                             if (!Directory.Exists(Application.StartupPath + @"\" + ps_prodcode.Text))
                             if (!Directory.Exists(Application.StartupPath + @"\" + ps_prodcode.Text))
                             {
                             {
                                 Directory.CreateDirectory(Application.StartupPath + @"\" + ps_prodcode.Text);
                                 Directory.CreateDirectory(Application.StartupPath + @"\" + ps_prodcode.Text);
                             }
                             }
-
                             string path = Application.StartupPath + @"\" + ps_prodcode.Text + @"\";
                             string path = Application.StartupPath + @"\" + ps_prodcode.Text + @"\";
+
                             Worksheet she = workbook.Worksheets[i];
                             Worksheet she = workbook.Worksheets[i];
 
 
-                            // 获取原表的实际数据范围和最大列
+                            // ---------- 基础参数 ----------
                             int lastColIndex = she.Cells.MaxDataColumn + 3;
                             int lastColIndex = she.Cells.MaxDataColumn + 3;
                             string lastColLetter = CellsHelper.ColumnIndexToName(lastColIndex);
                             string lastColLetter = CellsHelper.ColumnIndexToName(lastColIndex);
-
-                            int headerRows = 3;
+                            const int headerRows = 3;                              // 表头行数(每页都保留)
                             int dataRowsPerPage = int.Parse(RowCount.Value.ToString());
                             int dataRowsPerPage = int.Parse(RowCount.Value.ToString());
 
 
+                            int totalRows = she.Cells.MaxDataRow + 1;
+                            int totalDataRows = Math.Max(totalRows - headerRows, 0);
+                            int totalPages = (int)Math.Ceiling(totalDataRows / (double)dataRowsPerPage);
+                            if (totalPages == 0) totalPages = 1;
+
+                            // 水印在原表中的目标行/列
+                            int wmRowGlobal = int.Parse(Y.Value.ToString());
+                            int wmCol = int.Parse(X.Value.ToString());
 
 
-                            int totalDataRows = 0;
-                            for (int r = headerRows; r <= she.Cells.MaxDataRow; r++)
-                            {
-                                bool hasData = false;
-                                for (int c = 0; c <= she.Cells.MaxDataColumn; c++)
-                                {
-                                    if (she.Cells[r, c].Value != null && !string.IsNullOrWhiteSpace(she.Cells[r, c].Value.ToString()))
-                                    {
-                                        hasData = true;
-                                        break;
-                                    }
-                                }
-                                if (hasData)
-                                {
-                                    totalDataRows++;
-                                }
-                                else
-                                {
-                                    totalDataRows++;
-                                    //break;
-                                }
-                            }
-                            int totalRows = headerRows + totalDataRows;
-                            int totalPages = (int)Math.Ceiling((double)totalDataRows / dataRowsPerPage);
                             int imageIndex = 1;
                             int imageIndex = 1;
 
 
-                            for (int page = 0; page < (totalPages == 0 ? 1 : totalPages); page++)
+                            for (int page = 0; page < totalPages; page++)
                             {
                             {
+                                // ---------- 1. 复制原表到临时工作表 ----------
                                 int newSheetIndex = workbook.Worksheets.Add();
                                 int newSheetIndex = workbook.Worksheets.Add();
                                 Worksheet newshe = workbook.Worksheets[newSheetIndex];
                                 Worksheet newshe = workbook.Worksheets[newSheetIndex];
                                 newshe.Copy(she);
                                 newshe.Copy(she);
                                 newshe.Name = $"Temp_{page}";
                                 newshe.Name = $"Temp_{page}";
 
 
-                                // 计算当前页的数据范围
+                                // 本页在原表中的数据行区间(原表坐标,0-based)
                                 int dataStartRow = headerRows + page * dataRowsPerPage;
                                 int dataStartRow = headerRows + page * dataRowsPerPage;
                                 int dataEndRow = Math.Min(headerRows + (page + 1) * dataRowsPerPage - 1, totalRows - 1);
                                 int dataEndRow = Math.Min(headerRows + (page + 1) * dataRowsPerPage - 1, totalRows - 1);
 
 
-                                for (int r = totalRows - 1; r >= 0; r--)
+                                // ---------- 2. 按页过滤原表图片(Pictures) ----------
+                                // 保留:表头区图片 + 顶部锚点落在本页数据区的图片
+                                for (int p = newshe.Pictures.Count - 1; p >= 0; p--)
                                 {
                                 {
-                                    if (r < headerRows || (r >= dataStartRow && r <= dataEndRow))
+                                    Aspose.Cells.Drawing.Picture pic = newshe.Pictures[p];
+                                    int topRow = pic.UpperLeftRow;
+                                    bool isHeaderPic = topRow < headerRows;
+                                    bool belongsToPage = topRow >= dataStartRow && topRow <= dataEndRow;
+                                    if (!isHeaderPic && !belongsToPage)
                                     {
                                     {
-                                        // 保留标题行和当前页数据行
-                                        continue;
+                                        newshe.Pictures.RemoveAt(p);
                                     }
                                     }
-                                    else
+                                }
+
+                                // ---------- 3. 按页过滤原表形状(Shapes:文本框/箭头/连接线等) ----------
+                                // 防止"扭力扳手"这类文本框说明从第1页残留到第2页
+                                for (int s = newshe.Shapes.Count - 1; s >= 0; s--)
+                                {
+                                    Aspose.Cells.Drawing.Shape shape = newshe.Shapes[s];
+                                    int topRow = shape.UpperLeftRow;
+                                    bool isHeaderShape = topRow < headerRows;
+                                    bool belongsToPage = topRow >= dataStartRow && topRow <= dataEndRow;
+                                    if (!isHeaderShape && !belongsToPage)
                                     {
                                     {
-                                        // 删除不需要的行
-                                        newshe.Cells.DeleteRow(r);
+                                        newshe.Shapes.RemoveAt(s);
                                     }
                                     }
                                 }
                                 }
-                                int newTotalRows = newshe.Cells.MaxDataRow + 1;
 
 
-                                string newPrintArea = "";
-                                if (totalPages == 0)
+                                // ---------- 4. 清空非本页单元格内容(防止表格/文字残留) ----------
+                                for (int r = 0; r < totalRows; r++)
                                 {
                                 {
-                                    newPrintArea = $"A1:{lastColLetter}{25}";
+                                    if (r < headerRows) continue;                                   // 表头保留
+                                    if (r >= dataStartRow && r <= dataEndRow) continue;            // 本页数据保留
+                                    newshe.Cells.ClearRange(r, 0, r, lastColIndex);                // 清空该行内容与样式
                                 }
                                 }
-                                else
+
+                                // ---------- 5. 删除非本页整行 ----------
+                                for (int r = totalRows - 1; r >= 0; r--)
                                 {
                                 {
-                                    newPrintArea = $"A1:{lastColLetter}{newTotalRows}";
-                                    //newPrintArea = $"A1:{lastColLetter}{dataRowsPerPage}";
+                                    if (r < headerRows) continue;
+                                    if (r >= dataStartRow && r <= dataEndRow) continue;
+                                    newshe.Cells.DeleteRow(r);
                                 }
                                 }
-                                //Console.WriteLine(totalPages + " " + dataRowsPerPage + " " + newTotalRows);
-                                //Console.WriteLine($"A1:{lastColLetter}{(totalPages == 0 ? dataRowsPerPage : newTotalRows)}");
-                                //Console.WriteLine($"A1:{lastColLetter}{dataRowsPerPage}");
-                                newshe.PageSetup.PrintArea = newPrintArea;
 
 
+                                // ---------- 6. 页面设置 ----------
+                                int newTotalRows = newshe.Cells.MaxDataRow + 1;
+                                newshe.PageSetup.PrintArea = $"A1:{lastColLetter}{newTotalRows}";
                                 newshe.PageSetup.CenterHorizontally = true;
                                 newshe.PageSetup.CenterHorizontally = true;
                                 newshe.PageSetup.CenterVertically = true;
                                 newshe.PageSetup.CenterVertically = true;
                                 newshe.PageSetup.LeftMargin = 2;
                                 newshe.PageSetup.LeftMargin = 2;
@@ -212,52 +214,71 @@ namespace UAS_MES_NEW.Query
                                 newshe.PageSetup.SetFooter(0, "");
                                 newshe.PageSetup.SetFooter(0, "");
                                 newshe.PageSetup.SetFooter(1, "");
                                 newshe.PageSetup.SetFooter(1, "");
                                 newshe.PageSetup.SetFooter(2, "");
                                 newshe.PageSetup.SetFooter(2, "");
-                                // ========== 添加图片/水印 ==========
+
+                                // ---------- 7. 插入水印(额外图片,坐标映射到压缩后本页) ----------
                                 if (File.Exists(PrintPath.Text))
                                 if (File.Exists(PrintPath.Text))
                                 {
                                 {
-                                    int rowOffset = int.Parse(Y.Value.ToString());
-                                    newshe.Pictures.Add(rowOffset, int.Parse(X.Value.ToString()), PrintPath.Text);
+                                    int wmRowLocal = -1;
+                                    if (wmRowGlobal < headerRows)
+                                    {
+                                        wmRowLocal = wmRowGlobal;                              // 表头区直接用
+                                    }
+                                    else if (wmRowGlobal >= dataStartRow && wmRowGlobal <= dataEndRow)
+                                    {
+                                        wmRowLocal = headerRows + (wmRowGlobal - dataStartRow); // 数据行换算
+                                    }
+
+                                    if (wmRowLocal >= 0 && wmRowLocal < newTotalRows)
+                                    {
+                                        newshe.Pictures.Add(wmRowLocal, wmCol, PrintPath.Text);
+                                    }
                                 }
                                 }
 
 
+                                // ---------- 8. 页脚 ----------
                                 string pageFooter = $"第 {imageIndex} 页 / 共 {totalPages} 页";
                                 string pageFooter = $"第 {imageIndex} 页 / 共 {totalPages} 页";
                                 newshe.PageSetup.SetFooter(1, pageFooter);
                                 newshe.PageSetup.SetFooter(1, pageFooter);
 
 
+                                // ---------- 9. 渲染为 PNG ----------
                                 ImageOrPrintOptions options = new ImageOrPrintOptions();
                                 ImageOrPrintOptions options = new ImageOrPrintOptions();
                                 options.ImageFormat = ImageFormat.Png;
                                 options.ImageFormat = ImageFormat.Png;
-                                options.OnePagePerSheet = true;  // 改为 true,确保只渲染一页
+                                options.OnePagePerSheet = true;
                                 options.PrintingPage = PrintingPageType.Default;
                                 options.PrintingPage = PrintingPageType.Default;
 
 
                                 SheetRender sr = new SheetRender(newshe, options);
                                 SheetRender sr = new SheetRender(newshe, options);
-
                                 if (sr.PageCount > 0)
                                 if (sr.PageCount > 0)
                                 {
                                 {
-                                    Bitmap bitmap = sr.ToImage(0);
-
-                                    string imagename = $"{she.Name}_{imageIndex}.png";
-                                    string imagePath = Path.Combine(path, imagename);
-                                    bitmap.Save(imagePath, ImageFormat.Png);
-
-                                    bitmap.Dispose();
-
-                                    OperatResult.AppendText("解析图片【" + imagePath + "】" + "\n");
-
-                                    // ========== 上传与入库 ==========
-                                    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);
-
-                                    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");
+                                    using (Bitmap bitmap = sr.ToImage(0))
+                                    {
+                                        string imagename = $"{she.Name}_{imageIndex}.png";
+                                        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);
                                 workbook.Worksheets.RemoveAt(newSheetIndex);
-
                                 imageIndex++;
                                 imageIndex++;
-
                             }
                             }
                         }
                         }
                         catch (Exception ex)
                         catch (Exception ex)