C#用二维数组写Excel

                //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();

转载于:https://www.cnblogs.com/jackywork/articles/1102683.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值