위의 원폼을 사용하여 코드를 구현하였습니다.
using System; using System.Runtime.InteropServices; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; |
private void button1_Click(object sender, EventArgs e) { } |
4. Excel의 요소객체들을 생성합니다.
Excel.Application : Excel 프로그램private void button1_Click(object sender, EventArgs e) { Excel.Application App = new Excel.Application(); Excel.Workbook WorkBook; Excel.Worksheet WorkSheet; } |
private void button1_Click(object sender, EventArgs e) { Excel.Application App = new Excel.Application(); Excel.Workbook WorkBook; Excel.Worksheet WorkSheet; if (App == null) { MessageBox.Show("Excel이 설치되어 있지 않습니다."); return; } object Value = System.Reflection.Missing.Value; WorkBook = App.Workbooks.Add(Value); WorkSheet = (Excel.Worksheet)WorkBook.Worksheets.get_Item(1); } |
private void button1_Click(object sender, EventArgs e) { Excel.Application App = new Excel.Application(); Excel.Workbook WorkBook; Excel.Worksheet WorkSheet; if (App == null) { MessageBox.Show("Excel이 설치되어 있지 않습니다."); return; } object misValue = System.Reflection.Missing.Value; WorkBook = App.Workbooks.Add(misValue); WorkSheet = (Excel.Worksheet)WorkBook.Worksheets.get_Item(1); xlWorkSheet.Cells[1, 1] = "ID"; xlWorkSheet.Cells[1, 2] = "Name"; xlWorkSheet.Cells[2, 1] = "1"; xlWorkSheet.Cells[2, 2] = "One"; xlWorkSheet.Cells[3, 1] = "2"; xlWorkSheet.Cells[3, 2] = "Two"; }
이런 방식으로 저장됩니다. |
private void button1_Click(object sender, EventArgs e) { Excel.Application App = new Excel.Application(); Excel.Workbook WorkBook; Excel.Worksheet WorkSheet; if (App == null) { MessageBox.Show("Excel이 설치되어 있지 않습니다."); return; } object misValue = System.Reflection.Missing.Value; WorkBook = App.Workbooks.Add(misValue); WorkSheet = (Excel.Worksheet)WorkBook.Worksheets.get_Item(1); xlWorkSheet.Cells[1, 1] = "ID"; xlWorkSheet.Cells[1, 2] = "Name"; xlWorkSheet.Cells[2, 1] = "1"; xlWorkSheet.Cells[2, 2] = "One"; xlWorkSheet.Cells[3, 1] = "2"; xlWorkSheet.Cells[3, 2] = "Two"; WorkBook.SaveAs(@"d:\Excel_Test.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); } |
private void button1_Click(object sender, EventArgs e) { Excel.Application App = new Excel.Application(); Excel.Workbook WorkBook; Excel.Worksheet WorkSheet; if (App == null) { MessageBox.Show("Excel이 설치되어 있지 않습니다."); return; } object misValue = System.Reflection.Missing.Value; WorkBook = App.Workbooks.Add(misValue); WorkSheet = (Excel.Worksheet)WorkBook.Worksheets.get_Item(1); xlWorkSheet.Cells[1, 1] = "ID"; xlWorkSheet.Cells[1, 2] = "Name"; xlWorkSheet.Cells[2, 1] = "1"; xlWorkSheet.Cells[2, 2] = "One"; xlWorkSheet.Cells[3, 1] = "2"; xlWorkSheet.Cells[3, 2] = "Two"; WorkBook.SaveAs(@"d:\Excel_Test.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); Marshal.ReleaseComObject(xlWorkSheet); Marshal.ReleaseComObject(xlWorkBook); Marshal.ReleaseComObject(xlApp); MessageBox.Show("Excel 파일을 생성했습니다!"); } |
위의 원폼을 사용하여 코드를 구현하였습니다.
using System; using System.Runtime.InteropServices; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; |
private void button1_Click(object sender, EventArgs e) { } |
4. Excel의 요소객체들을 생성합니다.
Excel.Application : Excel 프로그램private void button1_Click(object sender, EventArgs e) { Excel.Application App = new Excel.Application(); Excel.Workbook WorkBook; Excel.Worksheet WorkSheet; } |
'프로그래밍 언어 > C#' 카테고리의 다른 글
[C# - WPF] 음악파일 재생 방법 (0) | 2018.10.30 |
---|---|
[ C#- WPF ] 기초 이론 (0) | 2018.10.29 |
[C# - WCF] WCF란? (0) | 2018.10.29 |
[C# - WinForm ] 이미지 불러오기, 저장하기 (1) | 2018.10.21 |
댓글