views:

30

answers:

3

Hi!

I'm using the following code to generate an Excel file using Microsoft.Interop.Excel. The problem is that I can actually see the workbook generation, when the code is executed, an excel document is open on the screen and the doc starts filling out with data. Can this be done somehow in the background?

Thanks

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlApp.Visible = false;


            if (xlApp == null)
            {
                MessageBox.Show("EXCEL could not be started. Check that your office installation and project references are correct.");
                return false;
            }
            xlApp.Visible = true;

            Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

            try
            {

                if (details != false)
                {
                    //Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
                    wb.Worksheets.Add();
                    Worksheet detailsWs = (Worksheet)wb.Worksheets[2];

                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        detailsWs.Cells[1, i + 1] = dt.Columns[i].Caption;
                    }

                }

                Worksheet ws = (Worksheet)wb.Worksheets[1];
                if (ws == null)
                {
                    MessageBox.Show("Worksheet could not be created. Check that your office installation and project references are correct.");
                }
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    ws.Cells[1, i + 1] = dt.Columns[i].Caption;
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        ws.Cells[i + 2, j + 1] = dt.Rows[i].ItemArray[j];
                    }
                    worker.ReportProgress((i * 100) / dt.Rows.Count);
                }




                wb.SaveAs(filename, XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlShared, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                wb.Close(true, filename);

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                // Cleanup
                GC.Collect();
                GC.WaitForPendingFinalizers();

                GC.Collect();
                GC.WaitForPendingFinalizers();



                if (wb != null)
                {
                    wb.Close(Type.Missing, Type.Missing, Type.Missing);
                    Marshal.FinalReleaseComObject(wb);
                }

                if (xlApp != null)
                {
                    xlApp.Quit();
                    Marshal.FinalReleaseComObject(xlApp);
                }
            }
A: 

Your code reads:

 xlApp.Visible = false;


 if (xlApp == null)
    {
        MessageBox.Show("EXCEL could not be started. Check that your office installation and project references are correct.");
        return false;
    }

then you write:

xlApp.Visible = true;

Leave this out. It will stay not visible.

Richard
A: 

Thanks! I didn't notice the second visible = true; Another question : how could i easily modify this code to generate a csv file ?

Thanks!

maephisto
You should look at FileHelpers library instead for csv file generation. Much quicker and simpler.
Richard
http://www.filehelpers.com/
Richard
Oh and it's free
Richard
A: 

Just below:

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

insert:

xlApp.Windows.Application.WindowState = XlWindowState.xlMinimized;

if you want excel open but still be accessible from the taskbar.

Koekiebox