0

我正在研究 VS 2003 C# WINFORM。

我想要单击按钮的位置,数据网格(不是 datagridView)将被复制到 Excel 文件中并打开(当然是数据),然后用户将选择是否要在分析后保存它。

                try
                { 
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel files (*.xls)|*.xls|All files (*.*)|*.*"  ;
                    sfd.FilterIndex = 1 ;
                    sfd.RestoreDirectory = true ;
                    sfd.Title = "Liste des différés";
                    sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                    if(sfd.ShowDialog() == DialogResult.OK)
                    {
                        if(sfd.FileName.Length > 0)
                        {
                            string data = null;
                            int i = 0;
                            int j = 0; 

                            Excel.Application xlApp ;
                            Excel.Workbook xlWorkBook ;
                            Excel.Worksheet xlWorkSheet ;
                            object misValue = System.Reflection.Missing.Value;

                            xlApp = new Excel.ApplicationClass();
                            xlWorkBook = xlApp.Workbooks.Add(misValue);
                            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                            for (i = 0; i <= dsSelectionListeDiffere.Tables[0].Rows.Count - 1; i++)
                            {
                                for (j = 0; j <= dsSelectionListeDiffere.Tables[0].Columns.Count - 1; j++)
                                {
                                    data = dsSelectionListeDiffere.Tables[0].Rows[i].ItemArray[j].ToString();
                                    xlWorkSheet.Cells[i + 1, j + 1] = data;
                                }
                            }


                            // Pass the full path to the SaveAs method
    //                      string fullPathName = Path.Combine(folderName, "csharp.xls");
                            xlWorkBook.SaveAs(sfd.FileNames, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                            xlWorkBook.Close(true, misValue, misValue);
                            xlApp.Quit();

                            releaseObject(xlWorkSheet);
                            releaseObject(xlWorkBook);
                            releaseObject(xlApp);


                        }

                    }
                }
                catch(Exception ex )
                {
                    MessageBox.Show(ex.Message);
                }



private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }
4

0 回答 0