Last active
May 31, 2018 01:42
-
-
Save otakijae/9052a45ba3d5ca44699d79ec072761ea to your computer and use it in GitHub Desktop.
C#ReadExcelFile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Excel = Microsoft.Office.Interop.Excel; | |
namespace LecturePractice | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
// Excel Application 객체 생성 | |
Excel.Application ExcelApp = new Excel.Application(); | |
// Workbook 객체 생성 및 파일 오픈 | |
Excel.Workbook workbook = ExcelApp.Workbooks.Open(Environment.CurrentDirectory + "\\excelFile.xlsx"); //현재 프로그램 경로에 있는 Lectures엑셀파일을 읽어옴 | |
//sheets에 읽어온 엑셀값을 넣기 | |
Excel.Sheets sheets = workbook.Sheets; | |
// 특정 sheet의 값 가져오기 | |
Excel.Worksheet worksheet = sheets["sheet"] as Excel.Worksheet; | |
// 범위 설정 | |
Excel.Range cellRange = worksheet.get_Range("A1", "C3") as Excel.Range; | |
// 설정한 범위만큼 데이터 담기 | |
Array data = cellRange.Cells.Value2; | |
// 데이터 출력 | |
Console.WriteLine(data.GetValue(1, 1)); | |
ExcelApp.Workbooks.Close(); | |
ExcelApp.Quit(); | |
} | |
catch (SystemException e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment