블로그 이미지
Every unexpected event is a path to learning for you. blueasa

카테고리

분류 전체보기 (2797)
Unity3D (853)
Programming (479)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (185)
협업 (61)
3DS Max (3)
Game (12)
Utility (68)
Etc (98)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (55)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday

xls와 xlsx 둘 다 되도록 예제를 약간 수정해서 테스트 완료.


P.s. 무슨 이유인지 모르지만 VS2013에서는 Microsoft.Office.Interop.Excel 이 없어서, 부득이하게 VS2010으로 작업함.

       (VS2012는 있는지 확인은 안해봤지만.. 얼핏 인터넷에서 보기로는 VS2012도 없는 듯..)

        
        private void CreateExcelFile()
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.Application();
            // 생성할 때, 기본 Sheet 1개.
            xlWorkBook = xlApp.Workbooks.Add(misValue);

            // Sheet 추가.(필요한 Sheet 개수에 따라 루프 돌아야 될 듯)
            xlWorkBook.Worksheets.Add(misValue, misValue, misValue, misValue);

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            // Sheet Name 지정.
            xlWorkSheet.Name = "SheetName1";
            xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
            xlWorkSheet.Name = "SheetName2";
            xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";

            // 테스트용으로 파일명 직접 입력.
            string createFilePath = Directory.GetCurrentDirectory() + @"\" + "csharp-Excel.xlsx";

            // 파일 있으면 삭제.
            if (true == File.Exists(createFilePath))
            {
                File.Delete(createFilePath);
            }

            // 확장자만 검사하기 위해..
            string strExtension = Path.GetExtension(createFilePath);

            if (0 == strExtension.CompareTo(".xlsx"))
            {
                xlWorkBook.SaveAs(createFilePath, Excel.XlFileFormat.xlOpenXMLWorkbook, misValue, misValue, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlUserResolution, true, misValue, misValue, misValue);
            }
            else if (0 == strExtension.CompareTo(".xls"))
            {
                xlWorkBook.SaveAs(createFilePath, 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);

            MessageBox.Show("Excel file created , you can find the file " + createFilePath);
        }

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



엑셀 생성 참조 : http://csharp.net-informations.com/excel/csharp-create-excel.htm

xlsx 참조 : http://stackoverflow.com/questions/9769703/exporting-to-xlsx-using-microsoft-office-interop-excel-saveas-error


반응형
Posted by blueasa
, |