QA@IT
«回答へ戻る

回答を投稿

久しぶりにいつもの鬼インデントのコードを書いてみました。




    private static void MosaMosaAA() {
        Excel.Application xlApplication = null;

        try {
            xlApplication = new Excel.Application();
            xlApplication.Visible = true;
            xlApplication.DisplayAlerts = false;
            Excel.Workbooks xlWorkbooks = null;

            try {
                xlWorkbooks = xlApplication.Workbooks;
                Excel.Workbook xlWorkbook = null;

                try {
                    xlWorkbook = xlWorkbooks.Add(System.Reflection.Missing.Value);
                    Excel.Sheets xlSheets = null;

                    try {
                        xlSheets = xlWorkbook.Worksheets;
                        Excel._Worksheet xlWorksheet = null;

                        try {
                            xlWorksheet = (Excel._Worksheet)xlSheets[1];
                            Excel.Pictures xlPictures = null;

                            try {
                                xlPictures = (Excel.Pictures)xlWorksheet.Pictures(System.Reflection.Missing.Value);
                                Excel.Picture xlPicture = null;

                                try {
                                    xlPicture = xlPictures.Insert(@"C:\駅のホームですっぽんぽん!.jpg", System.Reflection.Missing.Value);
                                    xlWorkbook.SaveAs(
                                        @"C:\MakiMakiLove.xls",
                                        System.Reflection.Missing.Value,
                                        System.Reflection.Missing.Value,
                                        System.Reflection.Missing.Value,
                                        System.Reflection.Missing.Value,
                                        System.Reflection.Missing.Value,
                                        Excel.XlSaveAsAccessMode.xlNoChange,
                                        System.Reflection.Missing.Value,
                                        System.Reflection.Missing.Value,
                                        System.Reflection.Missing.Value,
                                        System.Reflection.Missing.Value,
                                        System.Reflection.Missing.Value
                                    );

                                    System.Windows.Forms.MessageBox.Show("んっ!! ちゃんと表示されたから閉じるゾ!");
                                } finally {
                                    if (xlPicture != null) {
                                        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlPicture);
                                    }
                                }
                            } finally {
                                if (xlPictures != null) {
                                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlPictures);
                                }
                            }
                        } finally {
                            if (xlWorksheet != null) {
                                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorksheet);
                            }
                        }
                    } finally {
                        if (xlSheets != null) {
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets);
                        }
                    }
                } finally {
                    if (xlWorkbook != null) {
                        try {
                            xlWorkbook.Close(
                                System.Reflection.Missing.Value,
                                System.Reflection.Missing.Value,
                                System.Reflection.Missing.Value
                            );
                        } finally {
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook);
                        }
                    }
                }
            } finally {
                if (xlWorkbooks != null) {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbooks);
                }
            }
        } finally {
            if (xlApplication != null) {
                try {
                    xlApplication.Quit();
                } finally {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApplication);
                }
            }
        }        
    }

たぶんこれで画像が Worksheet の左上のところに挿入されるハズです。

こんなコードを書くより COM に適した言語 (VB | VBScript) などを利用した方が賢明ですね。

_________________C# と VB.NET の入門サイト
じゃんぬねっと日誌

投稿者:じゃんぬねっと

久しぶりにいつもの鬼インデントのコードを書いてみました。



~~~



    private static void MosaMosaAA() {
        Excel.Application xlApplication = null;

        try {
            xlApplication = new Excel.Application();
            xlApplication.Visible = true;
            xlApplication.DisplayAlerts = false;
            Excel.Workbooks xlWorkbooks = null;

            try {
                xlWorkbooks = xlApplication.Workbooks;
                Excel.Workbook xlWorkbook = null;

                try {
                    xlWorkbook = xlWorkbooks.Add(System.Reflection.Missing.Value);
                    Excel.Sheets xlSheets = null;

                    try {
                        xlSheets = xlWorkbook.Worksheets;
                        Excel._Worksheet xlWorksheet = null;

                        try {
                            xlWorksheet = (Excel._Worksheet)xlSheets[1];
                            Excel.Pictures xlPictures = null;

                            try {
                                xlPictures = (Excel.Pictures)xlWorksheet.Pictures(System.Reflection.Missing.Value);
                                Excel.Picture xlPicture = null;

                                try {
                                    xlPicture = xlPictures.Insert(@"C:\駅のホームですっぽんぽん!.jpg", System.Reflection.Missing.Value);
                                    xlWorkbook.SaveAs(
                                        @"C:\MakiMakiLove.xls",
                                        System.Reflection.Missing.Value,
                                        System.Reflection.Missing.Value,
                                        System.Reflection.Missing.Value,
                                        System.Reflection.Missing.Value,
                                        System.Reflection.Missing.Value,
                                        Excel.XlSaveAsAccessMode.xlNoChange,
                                        System.Reflection.Missing.Value,
                                        System.Reflection.Missing.Value,
                                        System.Reflection.Missing.Value,
                                        System.Reflection.Missing.Value,
                                        System.Reflection.Missing.Value
                                    );

                                    System.Windows.Forms.MessageBox.Show("んっ!! ちゃんと表示されたから閉じるゾ!");
                                } finally {
                                    if (xlPicture != null) {
                                        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlPicture);
                                    }
                                }
                            } finally {
                                if (xlPictures != null) {
                                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlPictures);
                                }
                            }
                        } finally {
                            if (xlWorksheet != null) {
                                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorksheet);
                            }
                        }
                    } finally {
                        if (xlSheets != null) {
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets);
                        }
                    }
                } finally {
                    if (xlWorkbook != null) {
                        try {
                            xlWorkbook.Close(
                                System.Reflection.Missing.Value,
                                System.Reflection.Missing.Value,
                                System.Reflection.Missing.Value
                            );
                        } finally {
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook);
                        }
                    }
                }
            } finally {
                if (xlWorkbooks != null) {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbooks);
                }
            }
        } finally {
            if (xlApplication != null) {
                try {
                    xlApplication.Quit();
                } finally {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApplication);
                }
            }
        }        
    }
~~~

たぶんこれで画像が Worksheet の左上のところに挿入されるハズです。

こんなコードを書くより COM に適した言語 (VB | VBScript) などを利用した方が賢明ですね。

_________________<a href="http://jeanne.wankuma.com/" target="_blank">C# と VB.NET の入門サイト</a>
<a href="http://blogs.wankuma.com/jeanne/" target="_blank">じゃんぬねっと日誌</a>


投稿者:じゃんぬねっと