峰峦@叠嶂 2025-06-17 08:39 采纳率: 98%
浏览 9
已结题

设置生产领料单标题和列框的需求问题;

你好,以下是我们生成生产领料单模板的代码 ,需要将标题“生产领料单”的字体,设置为14号大小,表格主体框的各列设置为居中,请问应该修改下面的代码,可以满足需要;

img



```c#

    // 生产领料单生成器
    public class PickingListGenerator
    {
        // 生成领料单的核心方法
        public void GeneratePickingList(List<MaterialStockForm.MaterialIssue> issues)
        {
            // 配置 EPPlus 许可证上下文
            ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;

            // 创建保存文件对话框
            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                saveFileDialog.Filter = "Excel文件|*.xlsx";
                saveFileDialog.Title = "保存生产领料单";
                saveFileDialog.FileName = $"生产领料单_{DateTime.Now:yyyyMMddHHmmss}.xlsx";

                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    using (ExcelPackage package = new ExcelPackage(new FileInfo(saveFileDialog.FileName)))
                    {
                        // 按药品批号分组
                        var groupedData = issues.GroupBy(i => i.ProductBatch);

                        // 创建工作表
                        ExcelWorksheet sheet = package.Workbook.Worksheets.Add("生产领料单");
                        int rowIndex = 0;

                        foreach (var group in groupedData)
                        {
                            // 生成表头
                            rowIndex = CreateHeader(sheet, rowIndex, group.Key, group.First().IssueDate);

                            // 生成物料行
                            rowIndex = CreateMaterialRows(sheet, rowIndex, group);

                            // 生成表尾
                            rowIndex = CreateFooter(sheet, rowIndex);

                            // 分组间空行,不带边框
                            for (int i = 0; i < 2; i++)
                            {
                                rowIndex++;
                            }
                        }

                        //// 调整列宽以适应内容,0615为了列宽,能采用自设定的宽度而设置;
                        //for (int i = 1; i <= 7; i++)
                        //{
                        //    sheet.Column(i).AutoFit();
                        //}

                        // 保存文件
                        package.Save();
                    }
                }
            }
        }

    }

    private static int CreateHeader(ExcelWorksheet sheet, int rowIndex, string batchNo, DateTime date)
    {
        // 表单编号行
        sheet.Cells[rowIndex + 1, 1].Value = "S0R-P-06-004-01";
        rowIndex++;

        // 标题行
        sheet.Cells[rowIndex + 1, 1].Value = "生 产 领 料 单";
        sheet.Cells[rowIndex + 1, 1, rowIndex + 1, 7].Merge = true;
        sheet.Cells[rowIndex + 1, 1].Style.Font.Bold = true;
        sheet.Cells[rowIndex + 1, 1].Style.Font.Size = 14;
        sheet.Cells[rowIndex + 1, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
        rowIndex++;

        // 空行
        rowIndex++;

        // 部门信息行
        sheet.Cells[rowIndex + 1, 1].Value = "领料部门:          前处理车间";
        sheet.Cells[rowIndex + 1, 3].Value = "日  期:";
        sheet.Cells[rowIndex + 1, 4].Value = date.ToString("yyyy-MM-dd");
        sheet.Cells[rowIndex + 1, 6].Value = "编 号:";
        rowIndex++;

        // 领料类型行
        sheet.Cells[rowIndex + 1, 1].Value = "领料类型:           一般领料";
        sheet.Cells[rowIndex + 1, 3].Value = "成本对象名称:";

        // 添加针对成本名称的下拉列表
        var dataValidation = sheet.DataValidations.AddListValidation($"D{rowIndex + 1}");
        dataValidation.Formula.Values.Add("荷丹胶囊干膏粉");
        dataValidation.Formula.Values.Add("荷丹片干膏粉");

        sheet.Cells[rowIndex + 1, 6].Value = "发料仓库:   原料库";
        rowIndex++;

        // 批号信息行
        sheet.Cells[rowIndex + 1, 1].Value = "生产任务单号:";
        sheet.Cells[rowIndex + 1, 3].Value = $"批  号:       {batchNo}";
        sheet.Cells[rowIndex + 1, 6].Value = "产品批号:";
        rowIndex++;

        // 空行
        rowIndex++;

        // 列标题行
        string[] headers = { "物料编码", "物料名称", "物料批号", "单位", "申领数量", "实发数量", "工序名称" };
        for (int i = 0; i < headers.Length; i++)
        {
            sheet.Cells[rowIndex + 1, i + 1].Value = headers[i];
            //sheet.Cells[rowIndex + 1, i + 1].Style.Font.Bold = true;//使上述字体加黑,加粗;
            sheet.Cells[rowIndex + 1, i + 1].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
        }
        rowIndex++;

        return rowIndex;
    }

    private static int CreateMaterialRows(ExcelWorksheet sheet, int rowIndex, IGrouping<string, MaterialStockForm.MaterialIssue> group)
    {
        int startRow = rowIndex + 1;
        int actualRowCount = 0; // 记录实际数据行数

        // 设置默认字体和缩放比例
        sheet.Cells.Style.Font.Name = "宋体"; // 使用标准字体
        sheet.Cells.Style.Font.Size = 9;
        sheet.View.ZoomScale = 100;

        foreach (var item in group)
        {
            var row = sheet.Cells[rowIndex + 1, 1, rowIndex + 1, 7];

            if (MaterialStockForm.MaterialMappings.TryGetValue(item.MaterialName, out var master))
            {
                row[rowIndex + 1, 1].Value = master.MaterialCode;
                row[rowIndex + 1, 2].Value = master.MaterialName;
            }
            else
            {
                row[rowIndex + 1, 1].Value = "未知编码";
                row[rowIndex + 1, 2].Value = item.MaterialName;
            }

            row[rowIndex + 1, 3].Value = item.MaterialBatch;
            row[rowIndex + 1, 4].Value = "千克";
            row[rowIndex + 1, 5].Value = item.IssueQty;
            row[rowIndex + 1, 6].Value = ""; // 实发数量留空
            row[rowIndex + 1, 7].Value = "提取配料";

            // 设置虚线边框      
            row.Style.Border.Top.Style = ExcelBorderStyle.Dashed;
            row.Style.Border.Bottom.Style = ExcelBorderStyle.Dashed;
            row.Style.Border.Left.Style = ExcelBorderStyle.Dashed;
            row.Style.Border.Right.Style = ExcelBorderStyle.Dashed;

            // 设置固定行高为22.5
            sheet.Row(rowIndex + 1).Height = 22.5;
            sheet.Row(rowIndex + 1).CustomHeight = true; // 确保使用自定义行高

            rowIndex++;
            actualRowCount++;
        }

        // 计算需要补充的空行数
        int emptyRowsNeeded = 8 - actualRowCount;
        if (emptyRowsNeeded > 0)
        {
            // 添加空行,带虚线边框
            for (int i = 0; i < emptyRowsNeeded; i++)
            {
                var emptyRow = sheet.Cells[rowIndex + 1, 1, rowIndex + 1, 7];

                // 所有单元格留空,但设置边框
                for (int col = 1; col <= 7; col++)
                {
                    emptyRow[rowIndex + 1, col].Value = "";
                }

                // 设置虚线边框
                emptyRow.Style.Border.Top.Style = ExcelBorderStyle.Dashed;
                emptyRow.Style.Border.Bottom.Style = ExcelBorderStyle.Dashed;
                emptyRow.Style.Border.Left.Style = ExcelBorderStyle.Dashed;
                emptyRow.Style.Border.Right.Style = ExcelBorderStyle.Dashed;

                // 设置固定行高
                sheet.Row(rowIndex + 1).Height = 22.5;
                sheet.Row(rowIndex + 1).CustomHeight = true;

                rowIndex++;
            }
        }


        // 为表单体添加边框    
        var tableBody = sheet.Cells[startRow, 1, rowIndex, 7];
        tableBody.Style.Border.Top.Style = ExcelBorderStyle.Dashed;
        tableBody.Style.Border.Bottom.Style = ExcelBorderStyle.Dashed;
        tableBody.Style.Border.Left.Style = ExcelBorderStyle.Dashed;
        tableBody.Style.Border.Right.Style = ExcelBorderStyle.Dashed;

        // 补充空行(空一行)
        for (int i = 0; i < 1; i++)
        {
            rowIndex++;
        }

        // 设置列宽,第1列的宽为9.38(仅需设置一次,列宽是列级属性,而非行级)
        sheet.Column(1).Width = 9.45;  // 物料编码
        sheet.Column(2).Width = 24.13; // 物料名称
        sheet.Column(3).Width = 12.25; // 物料批号
        sheet.Column(4).Width = 5;     // 单位
        sheet.Column(5).Width = 9.5;   // 申领数量
        sheet.Column(6).Width = 9.25;  // 实发数量
        sheet.Column(7).Width = 10.88; // 工序名称

        return rowIndex;
    }

    private static int CreateFooter(ExcelWorksheet sheet, int rowIndex)
    {
        sheet.Cells[rowIndex + 1, 1].Value = "审核:         记账 :          领料:         发料:         工序签字:        制单:代玉杰";
        sheet.Cells[rowIndex + 1, 1, rowIndex + 1, 7].Merge = true;
        rowIndex++;

        return rowIndex;
    }

```

  • 写回答

6条回答 默认 最新

  • 宝码香车 优质创作者: JavaScript技术领域 2025-06-17 08:42
    关注

    让【宝码香车】来帮你解答,本回答参考DeepSeek编写,并整理提供。

    如果还有疑问可以点击头像关注私信或评论。如果答案让您满意,请采纳、点赞、关注,非常感谢!

    根据您的要求,我将修改代码以满足以下需求:

    1. 将标题"生产领料单"的字体设置为14号
    2. 将表格主体框的各列设置为居中

    以下是修改后的代码(主要修改部分已标注注释):

    private static int CreateHeader(ExcelWorksheet sheet, int rowIndex, string batchNo, DateTime date)
    {
        // 表单编号行
        sheet.Cells[rowIndex + 1, 1].Value = "S0R-P-06-004-01";
        rowIndex++;
    
    
        // 标题行 - 已设置14号字体
        sheet.Cells[rowIndex + 1, 1].Value = "生 产 领 料 单";
        sheet.Cells[rowIndex + 1, 1, rowIndex + 1, 7].Merge = true;
        sheet.Cells[rowIndex + 1, 1].Style.Font.Bold = true;
        sheet.Cells[rowIndex + 1, 1].Style.Font.Size = 14; // 已设置为14号字体
        sheet.Cells[rowIndex + 1, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
        rowIndex++;
    
    
        // 空行
        rowIndex++;
    
    
        // 部门信息行
        sheet.Cells[rowIndex + 1, 1].Value = "领料部门:          前处理车间";
        sheet.Cells[rowIndex + 1, 3].Value = "日  期:";
        sheet.Cells[rowIndex + 1, 4].Value = date.ToString("yyyy-MM-dd");
        sheet.Cells[rowIndex + 1, 6].Value = "编 号:";
        rowIndex++;
    
    
        // 领料类型行
        sheet.Cells[rowIndex + 1, 1].Value = "领料类型:           一般领料";
        sheet.Cells[rowIndex + 1, 3].Value = "成本对象名称:";
    
    
        // 添加针对成本名称的下拉列表
        var dataValidation = sheet.DataValidations.AddListValidation($"D{rowIndex + 1}");
        dataValidation.Formula.Values.Add("荷丹胶囊干膏粉");
        dataValidation.Formula.Values.Add("荷丹片干膏粉");
    
    
        sheet.Cells[rowIndex + 1, 6].Value = "发料仓库:   原料库";
        rowIndex++;
    
    
        // 批号信息行
        sheet.Cells[rowIndex + 1, 1].Value = "生产任务单号:";
        sheet.Cells[rowIndex + 1, 3].Value = $"批  号:       {batchNo}";
        sheet.Cells[rowIndex + 1, 6].Value = "产品批号:";
        rowIndex++;
    
    
        // 空行
        rowIndex++;
    
    
        // 列标题行
        string[] headers = { "物料编码", "物料名称", "物料批号", "单位", "申领数量", "实发数量", "工序名称" };
        for (int i = 0; i < headers.Length; i++)
        {
            sheet.Cells[rowIndex + 1, i + 1].Value = headers[i];
            sheet.Cells[rowIndex + 1, i + 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // 列标题居中
            sheet.Cells[rowIndex + 1, i + 1].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
        }
        rowIndex++;
    
    
        return rowIndex;
    }
    
    
    private static int CreateMaterialRows(ExcelWorksheet sheet, int rowIndex, IGrouping<string, MaterialStockForm.MaterialIssue> group)
    {
        int startRow = rowIndex + 1;
        int actualRowCount = 0;
    
    
        // 设置默认字体和缩放比例
        sheet.Cells.Style.Font.Name = "宋体";
        sheet.Cells.Style.Font.Size = 9;
        sheet.View.ZoomScale = 100;
    
    
        foreach (var item in group)
        {
            var row = sheet.Cells[rowIndex + 1, 1, rowIndex + 1, 7];
    
    
            if (MaterialStockForm.MaterialMappings.TryGetValue(item.MaterialName, out var master))
            {
                row[rowIndex + 1, 1].Value = master.MaterialCode;
                row[rowIndex + 1, 2].Value = master.MaterialName;
            }
            else
            {
                row[rowIndex + 1, 1].Value = "未知编码";
                row[rowIndex + 1, 2].Value = item.MaterialName;
            }
    
    
            row[rowIndex + 1, 3].Value = item.MaterialBatch;
            row[rowIndex + 1, 4].Value = "千克";
            row[rowIndex + 1, 5].Value = item.IssueQty;
            row[rowIndex + 1, 6].Value = "";
            row[rowIndex + 1, 7].Value = "提取配料";
    
    
            // 设置虚线边框并居中
            row.Style.Border.Top.Style = ExcelBorderStyle.Dashed;
            row.Style.Border.Bottom.Style = ExcelBorderStyle.Dashed;
            row.Style.Border.Left.Style = ExcelBorderStyle.Dashed;
            row.Style.Border.Right.Style = ExcelBorderStyle.Dashed;
            row.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // 表格内容居中
    
    
            sheet.Row(rowIndex + 1).Height = 22.5;
            sheet.Row(rowIndex + 1).CustomHeight = true;
    
    
            rowIndex++;
            actualRowCount++;
        }
    
    
        // 计算需要补充的空行数
        int emptyRowsNeeded = 8 - actualRowCount;
        if (emptyRowsNeeded > 0)
        {
            for (int i = 0; i < emptyRowsNeeded; i++)
            {
                var emptyRow = sheet.Cells[rowIndex + 1, 1, rowIndex + 1, 7];
    
    
                for (int col = 1; col <= 7; col++)
                {
                    emptyRow[rowIndex + 1, col].Value = "";
                }
    
    
                emptyRow.Style.Border.Top.Style = ExcelBorderStyle.Dashed;
                emptyRow.Style.Border.Bottom.Style = ExcelBorderStyle.Dashed;
                emptyRow.Style.Border.Left.Style = ExcelBorderStyle.Dashed;
                emptyRow.Style.Border.Right.Style = ExcelBorderStyle.Dashed;
                emptyRow.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // 空行内容也居中
    
    
                sheet.Row(rowIndex + 1).Height = 22.5;
                sheet.Row(rowIndex + 1).CustomHeight = true;
    
    
                rowIndex++;
            }
        }
    
    
        // 为表单体添加边框    
        var tableBody = sheet.Cells[startRow, 1, rowIndex, 7];
        tableBody.Style.Border.Top.Style = ExcelBorderStyle.Dashed;
        tableBody.Style.Border.Bottom.Style = ExcelBorderStyle.Dashed;
        tableBody.Style.Border.Left.Style = ExcelBorderStyle.Dashed;
        tableBody.Style.Border.Right.Style = ExcelBorderStyle.Dashed;
        tableBody.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // 确保整个表格内容居中
    
    
        // 补充空行(空一行)
        for (int i = 0; i < 1; i++)
        {
            rowIndex++;
        }
    
    
        // 设置列宽
        sheet.Column(1).Width = 9.45;
        sheet.Column(2).Width = 24.13;
        sheet.Column(3).Width = 12.25;
        sheet.Column(4).Width = 5;
        sheet.Column(5).Width = 9.5;
        sheet.Column(6).Width = 9.25;
        sheet.Column(7).Width = 10.88;
    
    
        return rowIndex;
    }
    

    主要修改点:

    1. 标题"生产领料单"已经设置为14号字体(原代码已有此设置)
    2. CreateMaterialRows方法中,为表格内容和空行添加了居中设置:row.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center
    3. 在列标题行也添加了居中设置
    4. 确保整个表格区域的内容都居中显示

    这些修改将确保生产领料单的标题字体为14号,且表格主体框的各列内容都居中显示。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 6月25日
  • 已采纳回答 6月17日
  • 创建了问题 6月17日