none
您有遇過這樣的問題嗎?..匯出Excel後,WindowForm會關閉 RRS feed

  • 問題

  •  

    我將Datagrid內的資料匯出到Excel..可是為什麼匯出後原始的Form會關閉呢??...

     

    程式碼區塊

    private void btnExport_Click(object sender, EventArgs e)
            {
               this.GenerateReport(this.dgData);
            }

     

     

    程式碼區塊

    public void GenerateReport(DataGrid dg)
            {        
                Excel.Application oXL;
                Excel._Workbook oWB;
                Excel._Worksheet oSheet;

                try
                {
                    Cursor.Current = Cursors.WaitCursor;

                    //Start Excel and get Application object.
                    oXL = new Excel.ApplicationClass();
                    oXL.Visible = false;

                    //Get a new workbook.
                    oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));

                    oSheet = (Excel._Worksheet)oWB.ActiveSheet;
                    oXL.ActiveWindow.Zoom = 75;

                    oSheet.Name = "Test";

                    Int32 ColorIndex = 19;

                    // Add table headers going cell by cell.
                    oSheet.get_Range("A1", "A1").Value = dg.TableStyles[0].GridColumnStyles["Test1"].HeaderText;
                    oSheet.get_Range("B1", "B1").Value = dg.TableStyles[0].GridColumnStyles["Test2"].HeaderText;
                    oSheet.get_Range("C1", "C1").Value = dg.TableStyles[0].GridColumnStyles["Test3"].HeaderText;
                    oSheet.get_Range("D1", "D1").Value = dg.TableStyles[0].GridColumnStyles["Test4"].HeaderText;
                    oSheet.get_Range("E1", "E1").Value = dg.TableStyles[0].GridColumnStyles["Test5"].HeaderText;
                    oSheet.get_Range("F1", "F1").Value = dg.TableStyles[0].GridColumnStyles["Test6"].HeaderText;
                    oSheet.get_Range("G1", "G1").Value = dg.TableStyles[0].GridColumnStyles["Test7"].HeaderText;
                   
                    String LastColumn = "G";            

                    Int32 RowIndex = 2;

                    for (Int32 i = 0; i < ((DataTable)dg.DataSource).Rows.Count; i++)
                    {
                        oSheet.Cells[RowIndex, 1] = dg[i, 0].ToString();
                        oSheet.Cells[RowIndex, 2] = dg[i, 1].ToString();
                        oSheet.Cells[RowIndex, 3] = dg[i, 2].ToString();
                        oSheet.Cells[RowIndex, 4] = dg[i, 3].ToString();
                        oSheet.Cells[RowIndex, 5] = dg[i, 4].ToString();
                        oSheet.Cells[RowIndex, 6] = dg[i, 5].ToString();
                        oSheet.Cells[RowIndex, 7] = dg[i, 6].ToString();
                       
                        RowIndex++;
                    }

                    if (((DataTable)dg.DataSource).Rows.Count > 0)
                        RowIndex--;

                    oSheet.get_Range("A1", LastColumn + RowIndex.ToString()).Font.Name = "Tahoma";
                    oSheet.get_Range("A1", LastColumn + RowIndex.ToString()).Font.Size = 10;

                    oSheet.get_Range("A1", LastColumn + "1").Font.Bold = true;
                    oSheet.get_Range("A1", LastColumn + "1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;

                    oSheet.get_Range("A1", LastColumn + "1").Interior.ColorIndex = ColorIndex;
                    oSheet.get_Range("A1", LastColumn + "1").Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;
                    oSheet.get_Range("A1", LastColumn + RowIndex.ToString()).Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;

                    oSheet.get_Range("A1", "D" + RowIndex.ToString()).Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThin;

                    if (((DataTable)dg.DataSource).Rows.Count > 0)
                        oSheet.get_Range("A1", "A1").AutoFilter(1, Type.Missing, Excel.XlAutoFilterOperator.xlAnd, Type.Missing, true);
                                   
                    oSheet.get_Range("C2", "C" + RowIndex.ToString()).NumberFormat = "#############";
             
                    oSheet.get_Range("A1", LastColumn + "1").EntireColumn.AutoFit();

                    oSheet.get_Range("A1", "A1").Select();

                    //Make sure Excel is visible and give the user control of Microsoft Excel's lifetime.
                    oXL.Visible = true;
                    oXL.UserControl = true;
                }
                catch (Exception ex)
                {
                    Cursor.Current = Cursors.Default;

                    throw ex;
                }
                finally
                {
                    Cursor.Current = Cursors.Default;
                }

     

     

    2007年10月22日 上午 06:50

所有回覆

  • HI,

     

    您可以偵錯看看執行到那一行才發生FORM被關閉的情形?

     

    tihs

    2007年10月22日 上午 10:14
  • 已經執行到finally了..form依然是開啟的...

    可是執行完後就關到了..並沒有哪行程式有寫到Form.close()的阿..

     

    2007年10月22日 上午 10:31
  • HI,

     

    請問您是用VS2003還是VS2005?

     

    tihs

    2007年10月23日 上午 03:46
  • 之前是說在.net1.1時就有的程式...現在是用vs2005

    在管理前輩的程式..這一部份弄半天還是不懂...

    2007年10月24日 下午 03:18
  • HI,

     

    我用VS2005加入參考到Microsoft Excel 11.0 Object Library, 無法成功, 您的程式是用什麼方法參考到EXCEL的?

     

    tihs

    2007年10月27日 下午 03:06
  • this 大,你可以到微軟下載中心抓 vsto 2005 se 來更新看看。

    不過話又說回來,我自己是不參照,因為參照會綁 office 版本,很討厭...

     

    2007年10月28日 上午 02:19