刘红伟 發表於 2026-1-1 11:35:00

使用C#代码在Excel中插入行和列的操作指南

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>安装 Spire.XLS for .NET</li><li>在 C# 中向 Excel 插入行和列</li><li>在 C# 中向 Excel 插入多行和多列</li></ul></div><p>在处理 Excel 电子表格时,随着数据量的增加或项目范围的扩大,通常需要添加新的行或列。通过插入行和列,你可以快速调整工作表的结构,以容纳新的信息。本文将介绍如何使用 <strong>Spire.XLS for .NET</strong> 在 <strong>C#</strong> 中实现 Excel 行和列的插入操作。</p>
<p class="maodian"></p><h2>安装 Spire.XLS for .NET</h2>
<p>首先,你需要将 <strong>Spire.XLS for .NET</strong> 包中包含的 DLL 文件添加为 .NET 项目的引用。这些 DLL 文件可以通过该链接下载,或者通过 <strong>NuGet</strong> 进行安装。</p>
<div class="jb51code"><pre class="brush:bash;">PM&gt; Install-Package Spire.XLS</pre></div>
<p class="maodian"></p><h2>在 C# 中向 Excel 插入行和列</h2>
<p><strong>Spire.XLS for .NET</strong> 提供了 <code>Worksheet.InsertRow(int rowIndex)</code> 和 <code>Worksheet.InsertColumn(int columnIndex)</code> 方法,用于在 Excel 工作表中插入空白行和空白列。</p>
<p><strong>具体示例代码如下:</strong></p>
<div class="jb51code"><pre class="brush:csharp;">using Spire.Xls;

namespace InsertRowColumn
{
    class Program
    {
      static void Main(string[] args)
      {
            // 创建 Workbook 实例
            Workbook workbook = new Workbook();

            // 加载 Excel 文档
            workbook.LoadFromFile("input.xlsx");

            // 获取指定的工作表
            Worksheet worksheet = workbook.Worksheets;

            // 在工作表中插入第 5 行空白行
            worksheet.InsertRow(5);

            // 在工作表中插入第 4 列空白列
            worksheet.InsertColumn(4);

            // 保存结果文件
            workbook.SaveToFile("InsertRowAndColumn.xlsx", ExcelVersion.Version2016);
      }
    }
}</pre></div>
<p class="maodian"></p><h2>在 C# 中向 Excel 插入多行和多列</h2>
<p>要在工作表中插入多行和多列,可以使用 <code>Worksheet.InsertRow(int rowIndex, int rowCount)</code> 和 <code>Worksheet.InsertColumn(int columnIndex, int columnCount)</code> 方法。</p>
<p><strong>具体示例代码如下:</strong></p>
<div class="jb51code"><pre class="brush:csharp;">using Spire.Xls;

namespace InsertRowColumn
{
    class Program
    {
      static void Main(string[] args)
      {
            // 创建 Workbook 实例
            Workbook workbook = new Workbook();

            // 加载 Excel 文档
            workbook.LoadFromFile(@"E:\PythonExcel\input1.xlsx");

            // 获取指定的工作表
            Worksheet worksheet = workbook.Worksheets;

            // 在工作表中插入 3 行空白行
            worksheet.InsertRow(5, 3);

            // 在工作表中插入 2 列空白列
            worksheet.InsertColumn(4, 2);

            // 保存结果文件
            workbook.SaveToFile("InsertRowsAndColumns.xlsx", ExcelVersion.Version2016);
      }
    }
}</pre></div>
<p>到此这篇关于使用C#代码在Excel中插入行和列的操作指南的文章就介绍到这了,更多相关C# Excel中插入行和列内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>C#高效实现冻结Excel行和列的完整代码</li><li>C#实现冻结Excel窗口以锁定行列或解除冻结</li><li>C#使用Npoi导出Excel并合并行列</li><li>详解C#设置Excel数据自适应行高、列宽的2种情况</li><li>C#基于NPOI生成具有精确列宽行高的Excel文件的方法</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: 使用C#代码在Excel中插入行和列的操作指南