callm 18 часов назад
Родитель
Сommit
0035ddd406
1 измененных файлов с 89 добавлено и 81 удалено
  1. 89 81
      UAS_MES_ZKLH/FunctionCode/Query/Query_SOP.cs

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

@@ -129,117 +129,125 @@ namespace UAS_MES_NEW.Query
                             Worksheet she = workbook.Worksheets[i];
 
                             // 获取原表的实际数据范围和最大列
-                            int totalRows = she.Cells.MaxDataRow + 1;
-                            int lastColIndex = she.Cells.MaxDataColumn + 5;
+                            int lastColIndex = she.Cells.MaxDataColumn + 3;
                             string lastColLetter = CellsHelper.ColumnIndexToName(lastColIndex);
 
-                            int rowSliceSize = int.Parse(RowCount.Value.ToString());
-                            int imageIndex = 1;
+                            int headerRows = 3;
+                            int dataRowsPerPage = int.Parse(RowCount.Value.ToString());
 
-                            // ========== 核心修改:手动计算总页数 ==========
-                            // 总页数 = 总行数 除以 每页行数,向上取整
-                            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)
-                            {
-                                int endRow = Math.Min(startRow + rowSliceSize - 1, totalRows - 1);
 
-                                // 1. 动态设置原表的打印区域(模拟切片)
-                                if (endRow < rowSliceSize)
+                            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)
                                 {
-                                    Console.WriteLine($"A{startRow + 1}:{lastColLetter}{rowSliceSize + 1}");
-                                    she.PageSetup.PrintArea = $"A{startRow + 1}:{lastColLetter}{rowSliceSize + 1}";
+                                    totalDataRows++;
                                 }
                                 else
                                 {
-                                    Console.WriteLine($"A{startRow + 1}:{lastColLetter}{endRow + 1}");
-                                    she.PageSetup.PrintArea = $"A{startRow + 1}:{lastColLetter}{endRow + 1}";
+                                    totalDataRows++;
+                                    //break;
                                 }
+                            }
+                            int totalRows = headerRows + totalDataRows;
+                            int totalPages = (int)Math.Ceiling((double)totalDataRows / dataRowsPerPage);
+                            int imageIndex = 1;
 
-                                if (File.Exists(PrintPath.Text))
+                            for (int page = 0; page < (totalPages == 0 ? 1 : totalPages); page++)
+                            {
+                                int newSheetIndex = workbook.Worksheets.Add();
+                                Worksheet newshe = workbook.Worksheets[newSheetIndex];
+                                newshe.Copy(she);
+                                newshe.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 (startRow == 0)
+                                    if (r < headerRows || (r >= dataStartRow && r <= dataEndRow))
                                     {
-                                        she.Pictures.Add(startRow + int.Parse(Y.Value.ToString()), int.Parse(X.Value.ToString()), PrintPath.Text);
+                                        // 保留标题行和当前页数据行
+                                        continue;
                                     }
                                     else
                                     {
-                                        she.Pictures.Add(startRow - 4 + int.Parse(Y.Value.ToString()), int.Parse(X.Value.ToString()), PrintPath.Text);
+                                        // 删除不需要的行
+                                        newshe.Cells.DeleteRow(r);
                                     }
                                 }
+                                int newTotalRows = newshe.Cells.MaxDataRow + 1;
+
+                                string newPrintArea = $"A1:{lastColLetter}{(totalPages == 0 ? dataRowsPerPage : newTotalRows)}";
+                                newshe.PageSetup.PrintArea = newPrintArea;
+
+                                newshe.PageSetup.CenterHorizontally = true;
+                                newshe.PageSetup.CenterVertically = true;
+                                newshe.PageSetup.LeftMargin = 2;
+                                newshe.PageSetup.RightMargin = 0;
+                                newshe.PageSetup.TopMargin = 2;
+                                newshe.PageSetup.BottomMargin = 2;
+                                newshe.PageSetup.SetFooter(0, "");
+                                newshe.PageSetup.SetFooter(1, "");
+                                newshe.PageSetup.SetFooter(2, "");
+                                // ========== 添加图片/水印 ==========
+                                if (File.Exists(PrintPath.Text))
+                                {
+                                    newshe.Pictures.Clear();
+                                    int rowOffset = headerRows + int.Parse(Y.Value.ToString());
+                                    newshe.Pictures.Add(rowOffset, int.Parse(X.Value.ToString()), PrintPath.Text);
+                                }
 
-                                // ========== 核心修改:手动写入硬编码页脚 ==========
-                                // 当前 page 是 imageIndex,总页数是我们刚才算的 totalPages
                                 string pageFooter = $"第 {imageIndex} 页 / 共 {totalPages} 页";
-                                // 直接写入页脚中间位置
-                                she.PageSetup.SetFooter(1, pageFooter);
-                                // ================================================
+                                newshe.PageSetup.SetFooter(1, pageFooter);
 
-                                // 2. 创建渲染选项
                                 ImageOrPrintOptions options = new ImageOrPrintOptions();
                                 options.ImageFormat = ImageFormat.Png;
-                                options.OnePagePerSheet = false;
+                                options.OnePagePerSheet = true;  // 改为 true,确保只渲染一页
                                 options.PrintingPage = PrintingPageType.Default;
 
-                                // 3. 渲染图片
-                                SheetRender sr = new SheetRender(she, options);
-                                Bitmap bitmap = sr.ToImage(0);
+                                SheetRender sr = new SheetRender(newshe, options);
 
-                                // 4. 处理并保存图片
-                                string imagename = $"{she.Name}_{imageIndex}.png";
-                                string imagePath = Path.Combine(path, imagename);
-                                bitmap.Save(imagePath, ImageFormat.Png);
+                                if (sr.PageCount > 0)
+                                {
+                                    Bitmap bitmap = sr.ToImage(0);
 
-                                bitmap.Dispose();
-                                //sr.Dispose(); // 如果旧版本这里报错,可以保留注释
+                                    string imagename = $"{she.Name}_{imageIndex}.png";
+                                    string imagePath = Path.Combine(path, imagename);
+                                    bitmap.Save(imagePath, ImageFormat.Png);
 
-                                imageIndex++; // 这一行对应下一张图片的页码,放在这里即可
-                                OperatResult.AppendText("解析图片【" + imagePath + "】" + "\n");
+                                    bitmap.Dispose();
 
-                                // 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);
+                                    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");
-                            }
+                                    // ========== 上传与入库 ==========
+                                    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);
 
-                            // 恢复原表设置
-                            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, "");
+                                    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);
+
+                                imageIndex++;
+
+                            }
                         }
                         catch (Exception ex)
                         {