Udi Goldstein

Udi Goldstein

  • NA
  • 22
  • 63.6k

C# and Excel advanced filter

Dec 15 2011 11:58 AM

I'm trying to filter data from one workbook to another obne, but I get an unpsecified exception.

I can't find many examples for adavnced filtering in excel using c# and I don't know Where's exactly my error.

Here's my code:




public
void excel()
   
{
       
Excel.Application xlApp;
       
Excel.Workbook xlWorkBookImport;
       
Excel.Workbook xlWorkBookOriginal;
       
Excel.Worksheet xlWorkSheetImport;
       
Excel.Worksheet xlWorkSheetOriginal;

        xlApp
= new Excel.ApplicationClass();
        xlWorkBookImport
= xlApp.Workbooks.Open("c:/open.xls");
        xlWorkBookOriginal
= xlApp.Workbooks.Open("c:/open1.xls");
        xlWorkBookOriginal
.Activate();
        xlWorkSheetOriginal
= (Excel.Worksheet)xlWorkBookOriginal.Worksheets.get_Item(1);
        xlWorkSheetOriginal
.get_Range("1:7").Delete();
        xlWorkBookImport
.Activate();
        xlWorkSheetImport
= (Excel.Worksheet)xlWorkBookImport.Worksheets.get_Item(1);
        xlWorkSheetOriginal
.Cells.AdvancedFilter(Excel.XlFilterAction.xlFilterCopy, Type.Missing, CopyToRange: "a1:a6", Unique: true);

        xlApp
.Visible = true;
   
}

Anyone can help me with a solution?

Thanks,

Udi



Answers (3)