C# export to Excel 2003. Autofilter not enabled


Not certain regarding difficulty but as unable to find answer on EE or by googling presume semi hard to hard.

Visual Studio 2008, Excel 2003

The following code works fine except for when excel opens and you try to select the filter, the arrow buttons don't work. Look enabled but act disabled. When manually toggled off then on, they work.

private void openInExcelToolStripMenuItem_Click(object sender, EventArgs e)
            Excel.ApplicationClass excel = new Excel.ApplicationClass(); 
            System.Data.DataTable dt = ((System.Data.DataTable)dataGridView1.DataSource);

            int ColumnIndex = 0; 
            foreach (DataColumn col in dt.Columns) 
                excel.Cells[1, ColumnIndex] = col.ColumnName; 

            // apply the filter
            DataRow[] filteredRows;
            filteredRows = dt.Select(dt.DefaultView.RowFilter);
            int rowIndex = 0;
            foreach (DataRow row in filteredRows) 
                ColumnIndex = 0;
                for (int j = 0; j <= row.ItemArray.GetUpperBound(0); j++)
                    excel.Cells[rowIndex + 1, ColumnIndex] = row[j]; 

            //Fix first row         
            excel.Application.ActiveWindow.SplitRow = 1;
            excel.Application.ActiveWindow.FreezePanes = true;         
            // Now apply autofilter         
            ((Excel.Worksheet)excel.Application.ActiveSheet).EnableAutoFilter = true;
            Excel.Range firstRow = excel.get_Range("A1",excel.get_Range("A1",Type.Missing).get_Offset(0,dt.Columns.Count-1));
            firstRow.AutoFilter("1", Type.Missing, Excel.XlAutoFilterOperator.xlAnd, Type.Missing, true);

            excel.Visible = true;
            excel = null;            

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
Hi, this code is very useful to you

public static void CreateWorkbook(DataSet ds, String path)
   XmlDataDocument xmlDataDoc = new XmlDataDocument(ds);
   XslTransform xt = new XslTransform();
   StreamReader reader =new StreamReader(typeof (WorkbookEngine).Assembly.GetManifestResourceStream(typeof (WorkbookEngine), “Excel.xsl”));
   XmlTextReader xRdr = new XmlTextReader(reader);
   xt.Load(xRdr, null, null);

   StringWriter sw = new StringWriter();
   xt.Transform(xmlDataDoc, null, sw, null);

   StreamWriter myWriter = new StreamWriter (path + “\\Report.xls“);
   myWriter.Write (sw.ToString());
   myWriter.Close ();
japa62Author Commented:
Sorry Sam, but this doesn't address the autofilter problem. In this situation, the end user is responsible to saving the data as it is rarely required to be kept.
japa62Author Commented:
Sorry again Sam, also meant to say Thank You.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Is this an Aspnet project? If it is:
"Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment."

In any case, it's much more easy to program, fast  and maintainable to use xslt and xml to generate excell documents.
Find an example here:

Best regards.
japa62Author Commented:
Thankyou cubaman, No this is a desktop application. I would prefer to asp.net/xslx and do so else where but in this case the user does not want the file saved first. Might try a xlst today (try as in see what the end user says, might be able to trick him).
Hello again japa62:
You don't need to save any file first. Your xslt template will be an embedded resource, and transform take place in memory. And it's much more faster than instantiating excell, creating a workbook, etc..
Best regards.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
japa62Author Commented:
Still not the result I needed but will do the job better than current results.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.