일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- java
- compare
- aduino
- Barcode
- Read
- digitalRead
- memory
- APP
- UNO
- length
- atmega328
- wpf
- Class
- file access
- Encapusulation
- Overloading
- Pointer
- preprocessing
- Contour
- mfc
- inheritance
- Binary
- parameter
- stream
- public
- flutter
- sensor
- Android
- Unity
- SERIAL
- Today
- Total
폴크(FOLC)
WF 테크닉 - xls 파일 연동 본문
# Excel 파일로 데이터를 연동
> Excel 관련 Reference 를 연결시키면 파일 저장 및 열기를 손 쉽게 진행 한다.
# 솔루션 탐색기 - 참조 - COM - Excel 를 검색
> 설치되어있는 Excel 버전에 따라서 Object Library 번호가 다르다.
# using Excel = Microsoft.Office.Interop.Excel;
# SaveFileDialog saveFile = new SaveFileDialog();
> saveFile.Filter = "Excel Files|*.xls";
> saveFile.Title = "Save Excel File";
> if (saveFile.ShowDialog() == DialogResult.OK)
{
Excel.Application excelApp = new Excel.Application();
Excel.Workbook wb = excelApp.Workbooks.Add(true);
Excel._Worksheet workSheet = wb.Worksheets.get_Item(1) as Excel._Worksheet;
workSheet.Name = "Data";
for (int i = 0; i < dataGridView1.Columns.Count; i++)
workSheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
for (int r = 0; r < dataGridView1.Rows.Count; r++)
for (int i = 0; i < dataGridView1.Columns.Count; i++)
workSheet.Cells[r + 2, i + 1] = dataGridView1.Rows[r].Cells[i].Value;
wb.SaveAs(fileName, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wb.Close(Type.Missing, Type.Missing, Type.Missing);
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
}
'C#, WF, WPF(.NET) > C#, WF, WPF(.NET) 테크닉' 카테고리의 다른 글
C# 테크닉 - xls 파일에 Local Image 추가하기 (0) | 2021.10.11 |
---|---|
C# 테크닉 - Image 파일 다운로드 (0) | 2021.09.27 |
C# 테크닉 - xls 파일에 Image 추가 (0) | 2021.09.27 |
C# 테크닉 - TrayIcon (0) | 2021.09.07 |
C# 테크닉 - 크롤링 (0) | 2021.07.27 |