//Excel行の総数
                int rowCount = dtExcel.Rows.Count;
                //Excel列の総数
                int colCount = dtExcel.Columns.Count;

                //Application対象
                Excel.Application excelApp = null;
                //Workbooks対象
                Excel.Workbooks excelBooks = null;
                //Workbook対象
                Excel.Workbook excelBook = null;
                //Excel入力
                Excel.Worksheet excelSheet = null;

                excelApp = new Excel.ApplicationClass();
                excelBooks = excelApp.Workbooks;

                //ファイルが存在します
                if (System.IO.File.Exists(excelPath) == true)
                {

                    excelBook = excelBooks.Add(excelPath);
                }
                else
                {
                    excelBook = excelBooks.Add(Type.Missing);
                }

                //Excel入力
                excelSheet = (Excel.Worksheet)excelBook.ActiveSheet;

               
                //二維数組
                object[,] dataArray = new object[rowCount, colCount];

               
                for (int i = 0; i < rowCount; i++)
                {
                    for (int j = 0; j < colCount; j++)
                    {
                        if (j == 0 || j == 2 || j == 3 || j == 4 || j == 5)
                        {
                            dataArray[i, j] = dtExcel.Rows[i][j];
                        }
                        else
                        {
                            dataArray[i, j] ="'" + dtExcel.Rows[i][j];
                        }
                    }
                }

                //製作Excel、データを導入します
                excelSheet.get_Range("A1", excelSheet.Cells[rowCount, colCount]).Value2 = dataArray;

                //ファイルが存在します
                if (System.IO.File.Exists(excelPath) == true)
                {
                   
                    excelApp.DisplayAlerts = false;
                    excelApp.AlertBeforeOverwriting = false;

                    //Excelファイルの保存します
                    excelSheet.SaveAs(excelPath, missing, missing, missing, missing, missing, missing, missing, missing);
                    excelBook.Save();
                }
                else
                {
                    //ファイルを作成、コピー、削除、移動、および開くためのインスタンス メソッドを提供し
                    System.IO.FileInfo fileInfo = new System.IO.FileInfo(excelPath);
                    System.IO.StreamWriter streamWriter = null;

                    try
                    {
                        //創立してあるいはひとつの公文書を開きます
                        streamWriter = fileInfo.CreateText();
                        //指定しようとするデ-タが標準 に書きこんで輸出して流れます
                        streamWriter.WriteLine(excelPath);
                        //現在の StreamWriter オブジェクトと基になるストリームを閉じます
                        streamWriter.Close();

                        excelApp.DisplayAlerts = false;
                        excelApp.AlertBeforeOverwriting = false;

                        //Excelファイルの保存します
                        excelSheet.SaveAs(excelPath, missing, missing, missing, missing, missing, missing, missing, missing);
                        excelBook.Save();
                    }
                    catch
                    {
                        //メセッジ表示(入力のパスは正しくありません)
                        JinjiCorpInfo.ShowXmlMessage("c2886c83-21ed-4026-951d-3d79f0483cea", "");
                    }
                }

                //閉鎖します
                excelApp.DisplayAlerts = false;
                excelApp.AlertBeforeOverwriting = false;
                //清空excelSheet対象
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheet);
                excelSheet = null;
                //清空excelBook対象
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelBook);
                excelBook = null;
                //清空excelApp対象
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
                excelApp = null;
                excelBooks.Close();
                //清空excelBooks対象
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelBooks);
                excelBooks = null;
                //清空ごみ箱
                GC.Collect();

posted on 2008-03-12 20:01  人人人人人  阅读(842)  评论(0编辑  收藏  举报