根据列值删除 excel 行 C#
delete excel rows based on column values C#
我正在创建 excel VSTO ,其中我已经有了根据列值(如果它大于零)删除 excel 行的宏,现在需要将其转换为 c# ,通过查看 Internet 上的文档,我已经提出了到目前为止,但仍然存在三个错误,我无法解决它们,请帮助我:
原始宏:
Sub delete2()
'
' delete2 Macro
'
'
Rows("1:1").Select
Selection.AutoFilter
ActiveSheet.Range("$A:$S1").AutoFilter Field:=10, Criteria1:=">0", _
Operator:=xlAnd
Rows("8:1382").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.delete Shift:=xlUp
ActiveSheet.Range("$A:$S1").AutoFilter Field:=10
Selection.AutoFilter
End Sub
我修改到现在:
using Microsoft.Office.Tools.Ribbon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
namespace CpCpk
{
public partial class Ribbon1
{
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
var excelappp = new Excel.Application();
Excel.Worksheet excelApp = Globals.ThisAddIn.Application.ActiveSheet;
excelappp.Rows["1:1"].Select();
excelappp.Selection.AutoFilter();
excelappp.ActiveSheet.Range["$A:$S1"].AutoFilter(Field: 10,Criteria1:">0",Operator: Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlAnd);
excelappp.Rows["8:1382"].Select();
excelappp.Range[excelappp.Selection, excelappp.Selection.End(Microsoft.Office.Interop.Excel.XlDirection.xlDown)].Select();
excelappp.Selection.delete(Shift: Microsoft.Office.Interop.Excel.XlDirection.xlUp);
excelappp.ActiveSheet.Range["$A:$S1"].AutoFilter(Field:10);
excelappp.Selection.AutoFilter();
}
虽然没有语法错误,但我在执行过程中遇到以下错误:
System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'
请帮我解决这个问题
查看您的代码,我认为您需要删除以下行
var excelappp = new Excel.Application();
改为使用下面的行
var excelappp = Globals.ThisAddIn.Application;
我正在创建 excel VSTO ,其中我已经有了根据列值(如果它大于零)删除 excel 行的宏,现在需要将其转换为 c# ,通过查看 Internet 上的文档,我已经提出了到目前为止,但仍然存在三个错误,我无法解决它们,请帮助我:
原始宏:
Sub delete2()
'
' delete2 Macro
'
'
Rows("1:1").Select
Selection.AutoFilter
ActiveSheet.Range("$A:$S1").AutoFilter Field:=10, Criteria1:=">0", _
Operator:=xlAnd
Rows("8:1382").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.delete Shift:=xlUp
ActiveSheet.Range("$A:$S1").AutoFilter Field:=10
Selection.AutoFilter
End Sub
我修改到现在:
using Microsoft.Office.Tools.Ribbon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
namespace CpCpk
{
public partial class Ribbon1
{
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
var excelappp = new Excel.Application();
Excel.Worksheet excelApp = Globals.ThisAddIn.Application.ActiveSheet;
excelappp.Rows["1:1"].Select();
excelappp.Selection.AutoFilter();
excelappp.ActiveSheet.Range["$A:$S1"].AutoFilter(Field: 10,Criteria1:">0",Operator: Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlAnd);
excelappp.Rows["8:1382"].Select();
excelappp.Range[excelappp.Selection, excelappp.Selection.End(Microsoft.Office.Interop.Excel.XlDirection.xlDown)].Select();
excelappp.Selection.delete(Shift: Microsoft.Office.Interop.Excel.XlDirection.xlUp);
excelappp.ActiveSheet.Range["$A:$S1"].AutoFilter(Field:10);
excelappp.Selection.AutoFilter();
}
虽然没有语法错误,但我在执行过程中遇到以下错误:
System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'
请帮我解决这个问题
查看您的代码,我认为您需要删除以下行
var excelappp = new Excel.Application();
改为使用下面的行
var excelappp = Globals.ThisAddIn.Application;