Language/C#
엑셀파일 생성 및 저장
Linuxias
2013. 11. 21. 15:28
반응형
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ExcelTest { class Program { static void Main(string[] args) { ExcelModule Exl = new ExcelModule(); Exl.CreateExcel(); Exl.SaveFile(@"C:\Users\Son\Desktop", @"test"); Exl.QuitExcel(); } } } /////////////////////////// ExcelModule.cs /////////////////////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; using Excel = Microsoft.Office.Interop.Excel; namespace ExcelTest { class ExcelModule { Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue; public ExcelModule() { xlApp = new Excel.Application(); //Excel 파일 생성 } /* * 함수명 : CreateExcel * Excel 파일을 생성하게 된다. */ public void CreateExcel() { misValue = System.Reflection.Missing.Value; xlWorkBook = xlApp.Workbooks.Add(misValue); } /* * 함수명 : SaveFile * 입력인자 : 저장할 Excel 파일의 경로와 파일명 * 반환값 : 생성 유무 * 입력된 경로와 파일명으로 Excel 파일을 저장하게 된다. */ public void SaveFile(string filepath, string filename) { string savefile = filepath + "\\" + filename; try { xlWorkBook.SaveAs(@savefile, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); } catch (System.Runtime.InteropServices.COMException ex) { return; } } public void QuitExcel() { xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); } /* 함수명 releaseObject * 입력인자 : 해제 원하는 Obj */ private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; } finally { GC.Collect(); } } } } |
기본적으로 엑셀파일을 생성하고, 저장하기 위한 코드이다.
반응형