添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
// 示例:在第 10 行(rowToInsertBefore = 10)的上方插入 5 行
int rowToInsertBefore = 10;
int numberOfRowsToInsert = 5;
_worksheet.InsertRow(rowToInsertBefore, numberOfRowsToInsert);
// 重要提示:插入行操作会使被插入位置及其下方的所有行号向下移动。
// 在执行插入操作后,需要特别注意更新后续操作中涉及的行号索引。
/// 根据模板行,为指定范围内的行复制单元格格式、行高,并执行单元格合并。 /// </summary> /// <param name="row">需要开始应用格式的起始行号(基于1)。</param> /// <param name="rowCount">需要处理的总行数。</param> /// <param name="_worksheet">目标Excel工作表对象。</param> public void ApplyRowFormattingAndMerging ( int row , int rowCount , ExcelWorksheet _worksheet ) // 基本参数校验 if ( _worksheet == null || row <= 0 || rowCount <= 0 ) error ( "为行格式化提供的参数无效。" ) ; return ; ExcelRange _cells = _worksheet. Cells ; // 获取单元格集合对象 int templateRow = row + rowCount; // 定义模板行的行号 // 可选:初步检查模板行是否可能在工作表范围内 if ( templateRow > _worksheet. Dimension ? . End . Row ) // 使用?.安全访问Dimension warning ( strFmt ( "模板行 %1 可能超出了工作表范围。" , templateRow ) ) ; // 此处可根据业务逻辑决定是否继续,或采用备用模板策略 // 外层循环处理每一行 for ( int i = 0 ; i < rowCount; i ++ ) int currentRow = row + i; // 当前正在处理的行号 // --- 步骤1:复制行格式 (单元格样式) --- int maxColumns = _worksheet. Dimension . Columns ; // 获取最大列数,?? 提供备用值 for ( int col = 1 ; col <= maxColumns; col ++ ) if ( _cells. get_Item ( templateRow , col ) . Style != null ) _cells. get_Item ( currentRow , col ) . StyleID = _cells. get_Item ( templateRow , col ) . StyleID ; _cells. get_Item ( currentRow , col ) . Formula = _cells. get_Item ( templateRow , col ) . Formula ; // --- 步骤2:复制行高 --- if ( _worksheet. Row ( templateRow ) != null ) // 确保模板行对象有效 _worksheet. Row ( currentRow ) . Height = _worksheet. Row ( templateRow ) . Height ; // --- 可选步骤:对合并后单元格或特定单元格赋值与单独格式化 --- // 例如,给合并后的第一个单元格赋值并居中 // _cells[currentRow, 3].Value = "合并区域值"; // _cells[currentRow, 3].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // _cells[currentRow, 3].Style.VerticalAlignment = ExcelVerticalAlignment.Center; } // 单行处理循环结束 catch ( Exception ex ) error ( strFmt ( "在Excel行格式化与合并过程中发生错误:%1" , ex. Message ) ) ; // 强烈建议记录更详细的异常信息,或进行更完善的错误处理
// (上述代码片段中已包含合并单元格的实现)
// 合并 currentRow 行的 C列 到 E列 (第3列到第5列)
_worksheet.Cells[currentRow, 3, currentRow, 5].Merge = true;
// 合并 currentRow 行的 F列 到 I列 (第6列到第9列)
_worksheet.Cells[currentRow, 6, currentRow, 9].Merge = true;
// ... 以此类推 ...
// 提示:合并后,通常只需要对合并区域左上角的单元格进行赋值和设置格式。
// EPPlus (OfficeOpenXml库的核心) 的行号和列号索引都是从 1 开始的。
// C=3, E=5, F=6, I=9, J=10, N=14, O=15, P=16
cell. Value = 12345.67 ; // cell.Value = "已完成"; // cell.Value = DateTimeUtil::utcNow(); // 使用D365FO的日期时间函数 // 2. 设置数字或日期格式 cell. Style . Numberformat . Format = "#,##0.00_);[Red](#,##0.00)" ; // 会计格式,负数红色显示 // cell.Style.Numberformat.Format = "yyyy/m/d h:mm AM/PM"; // 自定义日期时间格式 // cell.Style.Numberformat.Format = "@"; // 设置为文本格式 // 3. 设置对齐方式 cell. Style . HorizontalAlignment = ExcelHorizontalAlignment. Center ; // 水平居中 cell. Style . VerticalAlignment = ExcelVerticalAlignment. Center ; // 垂直居中 // 4. 设置字体 cell. Style . Font . Bold = true ; // 加粗 cell. Style . Font . Italic = false ; // 不倾斜 cell. Style . Font . Size = 11 ; // 字号 cell. Style . Font . Name = "等线" ; // 字体名称 // cell.Style.Font.Color.SetColor(System.Drawing.Color::Blue); // 设置字体颜色为蓝色 // 5. 设置背景填充色 // cell.Style.Fill.PatternType = ExcelFillStyle.Solid; // 设置填充模式为纯色 // cell.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color::FromArgb(217, 217, 217)); // 浅灰色背景 (使用ARGB) // 6. 设置单元格边框 // cell.Style.Border.Top.Style = ExcelBorderStyle.Thin; // 上边框细线 // cell.Style.Border.Bottom.Style = ExcelBorderStyle.Medium; // 下边框中等粗细 // cell.Style.Border.Left.Style = ExcelBorderStyle.Thin; // 左边框细线 // cell.Style.Border.Right.Style = ExcelBorderStyle.Thin; // 右边框细线 // cell.Style.Border.Top.Color.SetColor(System.Drawing.Color::Black); // 设置上边框颜色为黑色 // ... 为其他边框设置颜色 ...
  • D365FO技术分享:使用X++优雅地处理Excel 新增行、合并单元格与格式化技巧
  • 分步指南:在 Dynamics 365 F&O 中构建自定义工作流(Step-by-Step Guide: Building a Custom Workflow in Dynamics 365 F&O)
  • 如何在 D365FO 的现有量查询中增加字段和数据源(Add the new field or datasource in Inventory On hand form using X++ in D365FO)
  • 如何在D365FO使用视图在窗体上显示某一个财务维度(Financial dimensions)
  • Tax is regulated on purchase ID xxx. Purchase orders cannot be rearranged when individual purchase orders are tax regulated.
  •