【问题标题】:Removing empty rows in excel using C#使用C#删除excel中的空行
【发布时间】:2015-11-24 18:54:43
【问题描述】:

我想在程序执行结束时删除空行。具体来说,我在预定义的 Excel 表中使用 C# 计算后插入一些结果。最后,我需要以编程方式删除/删除空行。任何人都可以提出解决方案。我的代码有点大,所以我无法在这里包含。为了您的理解,我给出了一些excel的输入和输出视图。在输出 Excel 下面的行 DE 有空行我想以编程方式删除而不提及范围。

输入 Excel /预定义的 Excel 文件

A   1   2   3   4
B
C
D
E

输出 Excel

A   1   2   3   4
B   ABc cde nAC 123
C   cdf fed x2  123
D
E

【问题讨论】:

  • 你甚至没有提到你使用什么库来创建你的 excel 文件。
  • 我只是将数据插入到预定义的模板 excel 中,并且我使用 Microsoft.Office.Interop.Excel 参考来导入 excel。
  • 你能知道该行在运行时输入的数量吗?
  • 不知道行号,只需要编程查找空行即可。

标签: c# excel null delete-row


【解决方案1】:

您可以使用 Range 对象来实现。我在这里假设您使用的是 Excel 互操作。

假设你打开了你的书,然后设置范围然后删除它应该看起来像这样

ApplicationClass excel = new ApplicationClass();
//...

Microsoft.Office.Interop.Excel.Range cel = (Range)excel.Cells[rowIndex, columnIndex];
cel.Delete();

您也可以尝试使用:

for(int i = 1; i <=20; i++)
{
   excelRange = (Excel.Range)excelWorkSheet.Cells[i, 1];
   if (!string.IsNullOrEmpty(excelRange.Text.ToString()))
   {
       ((Range)excelWorkSheet.Rows[i]).Delete(excelRange);
   }
}

查看下面的链接

https://social.msdn.microsoft.com/Forums/office/en-US/469fdf10-35cc-46b2-a875-7b974deb5659/how-to-delete-all-empty-rows-from-a-excel-sheet-using-microsoftofficeinteropexcel?forum=exceldev

https://stackoverflow.com/a/9952004/4373895 这里的“Something”是你的空值。

希望这会有所帮助。

【讨论】:

  • 同时包括 (Range)excel.Cells[rowIndex, columnIndex];我面临错误,你能帮我解决这个问题吗?在第二个解决方案中,我需要找到空值或某个字符串。你是想说 somestring 为 null 吗?
  • 我尝试了社交 msdn,但是当我尝试以下行时,我收到此错误“方法 'CountA' 没有重载需要 '1' 参数”如果 (application.WorksheetFunction.CountA(exlwooksheet .Rows[i]) == 0) (exlwooksheet.Rows[i] as Microsoft.Office.Interop.Excel.Range).Delete();
  • VikrantMore 你能帮我解决这个问题吗?
【解决方案2】:

您可以使用以下代码删除 Excel 文件中的空行

 xlApp = new Microsoft.Office.Interop.Excel.Application();
 Microsoft.Office.Interop.Excel.Workbook excelWorkbook = xlApp.Workbooks.Open(fileName);
 Microsoft.Office.Interop.Excel._Worksheet sheet = excelWorkbook.Sheets[1];
 var LastRow = sheet.UsedRange.Rows.Count;
 LastRow = LastRow + sheet.UsedRange.Row - 1;
 for (int i = 1; i <= LastRow; i++)
 {
   if (application.WorksheetFunction.CountA(sheet.Rows[i]) == 0)
       (sheet.Rows[i] as Microsoft.Office.Interop.Excel.Range).Delete();
 }

【讨论】:

    【解决方案3】:

    以下代码完美运行,并为指定的行号和列号创建一个新的空白行:

    Excel.Range rng = (Excel.Range)xlWorkSheet1.Cells[RowNumber, ColumnNumber];
                                Excel.Range Row1 = rng.EntireRow;
                                Row1.Insert(Excel.XlInsertShiftDirection.xlShiftDown, false);
    

    【讨论】:

      【解决方案4】:

      从 Excel 工作表中删除空行和空列的扩展方法。

      /// <summary>
      /// Deletes empty rows and columns from the end of the given worksheet
      /// </summary>
      public static void Trim(this Excel.Worksheet worksheet)
      {
          worksheet.TrimColumns();
          worksheet.TrimRows();
      }
      
      /// <summary>
      /// Deletes empty rows from the end of the given worksheet
      /// </summary>
      public static void TrimRows(this Excel.Worksheet worksheet)
      {
          Excel.Range range = worksheet.UsedRange;
          while(worksheet.Application.WorksheetFunction.CountA(range.Rows[range.Rows.Count]) == 0)
              (range.Rows[range.Rows.Count] as Excel.Range).Delete();
      }
      
      /// <summary>
      /// Deletes empty columns from the end of the given worksheet
      /// </summary>
      public static void TrimColumns(this Excel.Worksheet worksheet)
      {
          Excel.Range range = worksheet.UsedRange;
          while(worksheet.Application.WorksheetFunction.CountA(range.Columns[range.Columns.Count]) == 0)
              (range.Columns[range.Columns.Count] as Excel.Range).Delete();
      }
      

      【讨论】:

        猜你喜欢
        • 2020-03-22
        • 1970-01-01
        • 1970-01-01
        • 2013-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-20
        • 1970-01-01
        相关资源
        最近更新 更多