Link to home
Start Free TrialLog in
Avatar of japa62
japa62

asked on

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

Avatar of SAMIR BHOGAYTA
SAMIR BHOGAYTA
Flag of India image

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 ();
}
Avatar of japa62
japa62

ASKER

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.
Avatar of japa62

ASKER

Sorry again Sam, also meant to say Thank You.
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.
 
Avatar of japa62

ASKER

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).
ASKER CERTIFIED SOLUTION
Avatar of cubaman_24
cubaman_24
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of japa62

ASKER

Still not the result I needed but will do the job better than current results.