Excel活动是可见的

时间:2010-09-20 08:35:17

标签: c# excel interop

我正在使用以下代码使用Microsoft.Interop.Excel生成Excel文件。问题是我实际上可以看到工作簿生成,当代码执行时,屏幕上打开了Excel文档,文档开始填充数据。这可以在后台以某种方式完成吗?

由于

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);
                }
            }

1 个答案:

答案 0 :(得分:2)

您的代码为:

 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;

不要这样做。它将保持不可见。