• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 799
  • Last Modified:

C# export to Excel 2003. Autofilter not enabled

Hi,

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(); 
            excel.Application.Workbooks.Add(true);
            
            System.Data.DataTable dt = ((System.Data.DataTable)dataGridView1.DataSource);

            int ColumnIndex = 0; 
            foreach (DataColumn col in dt.Columns) 
            { 
                ColumnIndex++;
                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) 
            { 
                rowIndex++;
                ColumnIndex = 0;
                for (int j = 0; j <= row.ItemArray.GetUpperBound(0); j++)
                {
                    ColumnIndex++; 
                    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.Columns.AutoFit();

            ((Excel.Worksheet)excel.Application.ActiveSheet).Activate();
            excel.Visible = true;
            excel = null;            
}

Open in new window

0
japa62
Asked:
japa62
  • 4
  • 2
1 Solution
 
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 ();
}
0
 
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.
0
 
japa62Author Commented:
Sorry again Sam, also meant to say Thank You.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
cubaman_24Commented:
Hello:
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."
http://support.microsoft.com/kb/257757

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:
http://www.ksaelen.be/wordpress/2009/08/using-c-xml-xslt-to-create-excel-spreadsheet/

Best regards.
 
0
 
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).
0
 
cubaman_24Commented:
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.
0
 
japa62Author Commented:
Still not the result I needed but will do the job better than current results.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now